在反应中动态更新 Highcharts 图表 [英] Dynamically update Highcharts chart in react

查看:39
本文介绍了在反应中动态更新 Highcharts 图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using highcharts-react-official in react-redux to create a drilldown chart.

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

However when I click on a bar to drilldown, I also update some props which causes the component to re-render - which seems to prevent the drilldown event.

我是从 在 react-highcharts 中动态更改系列数据而无需重新渲染图表 我应该使用 shouldComponentUpdate 和 getChart() 来防止重新渲染并动态更新数据.

I kind of gathered from Change series data dynamically in react-highcharts without re-render of the chart that I should use shouldComponentUpdate and getChart() to prevent re-render and instead dynamically update the data.

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

My issue is that getChart() doesn't seem to work for the official highcharts react package. I'm getting Uncaught TypeError: this.refs.chart.getChart is not a function

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

Is there an alternative I'm meant to be using to get and dynamically update the chart? Or some examples that I could look at?

这里只包含 render 和 shouldComponentUpdate 部分:

Just including render and shouldComponentUpdate parts here:

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"
        />
    )
}

推荐答案

highcharts-react-official v2.0.0 中添加了 allowChartUpdate 选项,应该很好用在你的情况下.通过使用此选项,您可以通过更新组件来阻止更新图表:

In highcharts-react-official v2.0.0 has been added allowChartUpdate option, which should work great in your case. By using this option you can block updating the chart with updating the component:

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

...

  <HighchartsReact
    ref={"chartComponent"}
    allowChartUpdate={this.allowChartUpdate}
    highcharts={Highcharts}
    options={...}
  />

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

Moreover, to get the chart instance use refs:

componentDidMount(){
  const chart = this.refs.chartComponent.chart;
}

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

这篇关于在反应中动态更新 Highcharts 图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆