在反应中动态更新 Highcharts 图表

use*_*089 7 javascript highcharts reactjs react-redux react-highcharts

我在 react-redux 中使用 highcharts-react-official 创建一个钻取图表。

然而,当我点击一个栏进行钻取时,我也会更新一些导致组件重新渲染的道具 - 这似乎阻止了钻取事件。

在 react-highcharts 中Change series data 中动态收集,没有重新渲染图表,我应该使用 shouldComponentUpdate 和 getChart() 来防止重新渲染并动态更新数据。

我的问题是 getChart() 似乎不适用于官方的 highcharts react 包。我收到 Uncaught TypeError: this.refs.chart.getChart is not a function

有没有我打算用来获取和动态更新图表的替代方法?或者一些我可以看的例子?

在这里只包括 render 和 shouldComponentUpdate 部分:

shouldComponentUpdate(nextProps, nextState) {
    let chart = this.refs.chart.getChart();
    //dynamically update data using nextProps
    return false;
}

render () {

    const options = {
        chart: {
            type: 'column',
            height: 300,
            events: {
                drillup: (e) => {this.drilledUp(e)}
            }
        },
        plotOptions: {
            series: {
                events:{
                      click: (e) => {this.categoryClicked(e)}
                }
            }
        },
        xAxis: {type: "category"},
        yAxis: {title: {text: 'Amount Spent ($)'}},
        series: [{
            name: 'weekly spending',
            showInLegend: false,
            data: this.props.transactionChartData.series_data,
            cursor: 'pointer',
            events: {
                click: (e)=> {this.weekSelected(e)}
            }
        }],
        drilldown: {
            series: this.props.transactionChartData.drilldown_data

        }
    };
    return (
        <HighchartsReact
        highcharts={Highcharts}
        options={options}
        ref="chart"
        />
    )
}
Run Code Online (Sandbox Code Playgroud)

ppo*_*zek 5

highcharts-react-official v2.0.0已添加allowChartUpdate的选项,这应该在你的情况下,工作的伟大。通过使用此选项,您可以通过更新组件来阻止更新图表:

categoryClicked() {
  this.allowChartUpdate = false;
  this.setState({
    ...
  });
}

...

  <HighchartsReact
    ref={"chartComponent"}
    allowChartUpdate={this.allowChartUpdate}
    highcharts={Highcharts}
    options={...}
  />
Run Code Online (Sandbox Code Playgroud)

此外,要获取图表实例,请使用refs

componentDidMount(){
  const chart = this.refs.chartComponent.chart;
}
Run Code Online (Sandbox Code Playgroud)

现场演示:https : //codesandbox.io/s/98nl4pp5r4