简体   繁体   English

在React中将D3图表调整为父级

[英]Resize D3 Chart to Parent in React

I was working through the tutorial from: http://ahmadchatha.com/writings/article1.html 我正在从以下网站浏览本教程: http : //ahmadchatha.com/writings/article1.html

And it sets you up with a React Component like this: 它为您设置了一个React组件,如下所示:

class SimplePie extends React.Component {
  render() {
    return (
      <div className="col-xs-4 chart"
        style={{background: this.props.data[1]}} >
        {this.props.title}
        <D3Chart
          width={200}
          height={200}>
          <DataSeries
            data={this.props.data[0]}
            colors={colors}
            width={200} height={200} />
        </D3Chart>
      </div>
    )
  }
}

But I want to change this so the D3Chart will resize based on the size of parent, and listen for resize events. 但是我想更改此设置,以便D3Chart将根据父对象的大小调整大小,并监听调整大小事件。 Is there any easy way to do this in React? 在React中有什么简单的方法可以做到这一点吗?

I am struggling with event listeners for resize events on a particular div. 我正在努力与事件侦听器在特定div上调整事件大小。

Measuring sizes of elements with React is not as straight forward as you might think, the main reason for this is that React doesn't really care about sizes, you must read them from the DOM, after they are drawn. 用React测量元素的大小并不像您想象的那样简单,主要原因是React并不真正在乎大小,绘制它们后必须从DOM中读取它们。

For your case here we can utilize the componentDidMount , componentDidUpdate lifecycle functions and a vanilla JavaScript resize event on the window element, like so: 对于您的情况,我们可以在window元素上利用componentDidMountcomponentDidUpdate生命周期函数和香草JavaScript resize event ,如下所示:

class SimplePie extends React.Component {
  constructor (props, context) {
    super(props, context);
    this.state = {
           width: 200,
           height: 200
        };
    this.measure = this.measure.bind(this);
  }
  componentWillMount () {
    window.addEventListener('resize', this.measure, false);
  }
  componentWillUnmount () {
    window.removeEventListener('resize', this.measure, false);
  }
  measure () {
    let rect = this.container.getBoundingClientRect();
    if(this.state.width !== rect.width || this.state.height !== rect.height){
       this.setState({
              width: rect.width, 
              height: rect.height
           });
    }
  }
  componentDidMount (){
     this.measure();
  }
  componentDidUpdate (){
     this.measure();
  }
  render() {
    return (
      <div ref={(container)=>{this.container = container}} style={{width: "100%", height: 300}} className="col-xs-4 chart"
        style={{background: this.props.data[1]}} >
        {this.props.title}
        <D3Chart
          width={this.state.width}
          height={this.state.height}>
          <DataSeries
            data={this.props.data[0]}
            colors={colors}
            width={this.state.width} height={this.state.height} />
        </D3Chart>
      </div>
    )
  }
}

Edit: The container must have a fixed width and height, if you want to use percentage based height you must set the height of the parent of this component. 编辑:容器必须具有固定的宽度和高度,如果要使用基于百分比的高度,则必须设置此组件父级的高度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM