系列数组更改后,React 顶点图表不会呈现

Gam*_*ger 7 charts reactjs apexcharts

你会在这篇文章中找到答案

我是 React 新手,我使用了 apexcharts,它工作得很好,但是当我必须在同一个项目中的其他组件中使用这个图表组件时。这就是我在图表中遇到问题的地方。突然间,当系列发生变化时,我的图表开始不再更新。我花了一周的时间找出问题所在,并认为我可以在这里分享它,以便有人可以更早地找到此修复程序。

所以我发现了两件事;

  1. React-apexcharts 只是 apexcharts 的包装,每个图表都应该有一个您在选项中定义的唯一图表 id。因此,如果您想在多个地方使用组件,最好在 props 中接收图表 ID 并将其设置为:
...
this.state = {
      options:  {
        chart: {
          id: props.chartId,
...
Run Code Online (Sandbox Code Playgroud)
  1. 我们可以通过在componentDidMount()UseEffect()中使用全局 apexcharts 方法来调用方法来渲染图表或覆盖图表的系列数组,例如,
import apexchart from "apexcharts";
...
  componentDidUpdate(){
    const {chartId, seriesData} = this.props;
    //here it overrides the chart series with seriesData from props
    apexchart.exec(chartId,'updateSeries', seriesData);
  };
...
Run Code Online (Sandbox Code Playgroud)

我们可以从 apexcharts 中使用其他许多有用的方法,如文档中所述:https ://apexcharts.com/docs/methods/

我希望有人觉得它有帮助。

谢谢。