【问题标题】:Highcharts update large arrayHighcharts 更新大数组
【发布时间】:2020-08-18 15:00:10
【问题描述】:

在 React Redux 应用程序中,我有一个包含时间序列数据(大约 10k 数据点)的大型数组。数据由 redux 存储提供。该应用程序使用highcharts-react-official 2.x.x。大数组经常通过追加新数据点来更新(仅追加,现有数组元素是不可变的)。

由于这些更新,图表似乎每次都完全呈现。我怎样才能避免这种情况?

const options = {
  series: [
    {
      type: 'line',
      data: objectFromReduxStore.map(p => ([p.time.toMillis(), p.value])),
    }
  ]
}
...

<HighchartsReact
  highcharts={Highcharts}
  options={options}
  containerProps={{style: {height: '300px', width: '100%'}}}
/>

【问题讨论】:

    标签: reactjs redux highcharts react-highcharts


    【解决方案1】:

    highcharts-react-official 包装器使用chart.update(options) 方法来应用更改。在您的情况下,更有效的解决方案是获取图表参考并直接在系列上调用addPoint 方法。

    constructor(props) {
      super(props);
      this.afterChartCreated = this.afterChartCreated.bind(this);
    }
    
    afterChartCreated(chart) {
      this.internalChart = chart;
    }
    
    componentDidMount() {
      setInterval(() => {
        this.internalChart.series[0].addPoint(Math.random() * 5);
      }, 1500);
    }
    
    render() {
      return (
        <HighchartsReact
          highcharts={Highcharts}
          options={options}
          callback={this.afterChartCreated}
        />
      );
    }
    

    现场演示: https://codesandbox.io/s/highcharts-react-demo-gp6n6?file=/demo.jsx

    API 参考: https://api.highcharts.com/class-reference/Highcharts.Series#addPoint

    【讨论】:

    • 感谢您的回答!似乎在 2.1.1 版本中没有属性 callback (打字稿给出了编译错误)。这是后来添加的吗?
    • 嗨@Sebastian,这可能只是打字稿的一些问题,请检查一些较新的版本。作为替代方案,您可以通过其他方式获取参考:github.com/highcharts/…
    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多