Apexcharts - ReactJs - real time - Cannot read property 'filter' of undefined

Viewed 4235

I have a chart created in a composant (hooks) :

return (
<div id="chart-candlestick">
    <ReactApexChart options={optionsCandles} series={seriesCandles} type="candlestick"
                    height={290}/>
</div>)

and I tried to add values to test the realtime possibility. (Without it, it works fine !) My problem is that I tried for several hours and didn't find why I have this exception.

So, I have named my chart ("candle") in the options :

{
    chart: {
        type: 'candlestick',
        height: 290,
        id: 'candles'   
        {...}
    },
    {...}
}

and so, time to time I tried to used the exec method of ApexCharts to add more data manualy:

function fetchLastDatas() { 
    const date = dateAdd(candleDatas[candleDatas.length - 1].x,"minute",5)
    var obj = {
        x: date,
        y: candleDatas[candleDatas.length - 1].y
    }
    candleDatas.push(obj);

    ApexCharts.exec('candles', 'updateSeries', [{
        data:candleDatas
    }], true)

}

My array has datas but I have this exception on the line "ApexCharts.exec" and I don't know why :

Uncaught TypeError: Cannot read property 'filter' of undefined
    at Function.value (apexcharts.common.js:14)
    at Function.value (apexcharts.common.js:14)
    at fetchLastDatas (ChartCaneva.js:178)
    

I'm new to use this library (and in React in general) so thanks for the help !

Good night (or morning) !

4 Answers

Ran into a similar error a while back.

Seems that ApexCharts.exec is complaining because it's trying to update a component (chart with id='candles') that hasn't been mounted yet.

You could try adding a console.log(document.getElementById('chart-candlestick')) in the update function to check this.

Maybe try adding a condition that checks whether the component is mounted before trying to run any updates.

useRef?


function fetchLastDatas(chartIsMounted) {
  // ...
  // debug: console.log(document.getElementById('chart-candlesticks'));
  if(chartIsMounted !== null) {
    ApexCharts.exec('candles', 'updateSeries', [{ /* ... */}])
  }
}

function Parent() {
  let chartRef = React.useRef(null);

  React.useEffect(() => {
    fetchLastDatas(chartRef.current);
  }, []);

  return <Child chartRef={chartRef} /> // You could use React.forwardRef instead and pass a `ref` prop
}

function Child({ chartRef }) {
  return <ChartContainer chartRef={chartRef} />;
}

function ChartContainer({ chartRef }) {
  return (
    <div id="chart-candlestick">
      <ReactApexChart id='candles' ref={chartRef} />
    </div>
  );
}

Sorry, I had a lot of other problem so I didn't response this one. In React you normally don't need to use exec. The modification of the Hooks will perform the change automatically.

I have needed to change my resolve answers.

In React and Apex-chart, the chart will refresh the options only if you set another reference of your object. In my case I tried with the spread operator and Object.assign.

The problem was that the options of the chart are a complex object. So you need to do a deep copy with a library or a custom function (for performance, I had read "rfdc" is the best)

I have had this issue to fix it I imported both import ReactApexChart from 'react-apexcharts' and import ApexCharts from 'apexcharts' exactly as the doc

Then my char was brush-chart so I called ApexCharts.exec for both parts like the following

   ApexCharts.exec(`linechart`, 'updateSeries', [{data: this.state.graphData, }])

   ApexCharts.exec(`area-chart`, 'updateSeries', [{ data: this.state.graphData, }])
Related