0

Now, As I previously discussed my issue with scatter chart (Inconsistent Data Points in HighCharts Scatter Plot with ReactJS) is we can't update data directly using setState method like other types in HighCharts...

So, I need to update data using ref only (In Scatter Chart).

My Scenario:

I am getting min and max value from parent component and I am passing the same in min and max in XAxis in child component which is responsible for showing plot.

Issue:

=> When the first time components mount - chartOptions is initialized in which series data is empty (As maindata is empty). Now, useEffect runs and calls an API which will getting data and I am passing that data into highcharts using ref method like this.....

useEffect(() => {
    const chart = chartRef.current.chart;
    if (chart) {
       chart.series[0].setData(mainData, true, true, false);
    }
}, [data]);

Now, when user clicks on refresh icon - I will fetch data again I can update series data by using above method. But, In order to set min and max in XAxis- I have to do this way (I need to set it again cause I want to range selector to be placed on same range):

useEffect(() => {
   setChartOptions((prevState) => ({
        ...prevState,
        xAxis: [
          {
            ...prevState.xAxis[0],
            min: xMax.maxX - 50,
            max: xMax.maxX,
          },
        ],
}));
}, [data]);

Steps to reproduce issue:

=> First time plot is not coming as data was empty, Than data comes from API and this will be set using ref, So now data is not null and we can see plot.

Than, setChartOptions will run and it will only change min and max and keep other things same as before, So again data will be null as it was null by default (mainData was null first time).

So, Plot is gone and can see just blank white screen.

Expectation:

=> I think, we can solve this problem like this:

Anyhow, we can set a mechanism to run setChartOptions first, so min and max is set and than and than only, We will update data using ref (chart.series[0].setData(mainData, true, true, false); ):

Till far what I have tried:

=> In simple terms, we can set Series data directly by set method like this:

setChartOptions((prevState) => ({
  ...prevState,
  series: [
    {
      ...prevState.series[0],
      data: mainData, // Update with the fetched data
    },
  ],
  xAxis: [
    {
      ...prevState.xAxis[0],
      min: xMax.maxX - 50,
      max: xMax.maxX,
    },
  ],
}));

But, This is not a working case in Scatter chart (Highcharts BUG)

But, I am not able to figured out how can i solve this with ReactJS.

Actually, I can't create a demo code as data is coming from API, but you can take this code as a reference in which I am updating data using ref:

https://stackblitz.com/edit/react-9wsnd9?file=index.js

======================================================

Update:

Hey, Thanks for your quick reply, But still I am not able to solve my problem:

Here is the code: https://stackblitz.com/edit/react-sgkrzf?file=index.js

Explanation: From parent - I am passing min={20} and max={40} to the child component.

Expected Behaviour: Min and Max should apply by default (Both together), but both are not working together (If min apply than max ain't and if max than min ignored)

I want initially to apply min and max 20 and 40 respectively, than user can change range selector and on refresh, that changed value should be preserved.

I.e.=> Changing range selector (20,40) to (34,75) should be preserved to (34,75) after refreshing data (I mean to say calling API)

How to do this? Please modify above code and help me with the same.

Thanks in advance.

1 Answer 1

1

You can use setChartOptions without prev state (the old options will not be removed).

useEffect(() => {
  setChartOptions(() => ({
    xAxis: [
      {
        min: xMax.maxX - 50,
        max: xMax.maxX
      }
    ]
  }));
}, [data]);

Docs: https://github.com/highcharts/highcharts-react#optimal-way-to-update

Or operate directly on the chart (similar to setData) and set xAxis extremes or update min/max.

API Reference: https://api.highcharts.com/class-reference/Highcharts.Axis#setExtremes

Sign up to request clarification or add additional context in comments.

4 Comments

Hello, I've included a new section labeled #update in the above question, where I've provided my code and additional information. Could you kindly take a look and provide your input? Thank you.
Thanks for the detailed description! We need to call chart.xAxis[0].setExtremes(min, max) after data is received for the first time. Please check this live example: stackblitz.com/edit/react-uhg6gb?file=Child.js
Thank you for the solution, it worked. I didn't know about this setExtremes method, Thanks for letting me know about the same. Marking your answer as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.