javascript - React 在 body 外渲染 d3 图表

标签 javascript d3.js reactjs ref

我正在尝试生成一个测试环境以使用 React 在 D3 上工作。

我正在使用以下代码:

componentDidMount(){
    d3.select(this._testChart)
        .data(this.state.data)
        .enter().append('div')
        .style('background-color','blue')
        .style('color', 'white')
        .style('width', d=>d*10 + 'px')
        .text(d=>d)
}

render(){
    return (<div className='TestChart' ref={(c) => this._testChart = c}>
        </div>)
}

我正在获取 divref,然后在 componentDidMount() 中。我附加了生成基本条形图的 div。现在,当我运行这段代码时,我看到了条形图,但它呈现在 HTML 主体之外。我想我没有正确使用 ref 。如何获取体内的图表?

最佳答案

你的 d3 选择器应该通过类名找到元素,而不是 React ref。

componentDidMount(){
    d3.select(".TestChart" )
        .data(this.state.data)
        .enter().append('div')
        .style('background-color','blue')
        .style('color', 'white')
        .style('width', d=>d*10 + 'px')
        .text(d=>d)
}

render(){
    return (<div className='TestChart' 
        </div>)
}

关于javascript - React 在 body 外渲染 d3 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905760/

相关文章:

javascript - Uncaught Error : Minified exception occurred REACT and BACKBONE integration

javascript - 向服务器发出 POST 请求以插入记录并在客户端检索生成的 ID 的方法

javascript - 连接函数

javascript - Select 在 TypeScript 中没有选项属性

javascript - 如何在不出错的情况下评论 EJS 代码(JS Node )

javascript - D3 Sunburst 图上的同心颜色

javascript - 将 Angular 组件从许多输入/输出重构为单个配置对象

d3.js - 标准的 d3 工作流程和托管解决方案

google-chrome - d3 滚动缩放在 Chrome 上不再起作用

reactjs - React 中的样式化组件 : TypeError: Cannot read property 'color' of undefined