amonm
Posts: 7
Joined: Thu Jun 10, 2021 9:11 pm

Network diagram react state update

Hi

I am using react and network diagram and i have a problem to change some specific field in my component state.

Code: Select all

    this.state = {
      options: {
        chart: {
          type: "networkgraph",
          marginTop: 80
        },
        title: {
          text: "Network graph"
        },
        plotOptions: {
          networkgraph: {
            keys: ["from", "to"],
            layoutAlgorithm: {
              enableSimulation: true,
              linkLength: 33,
              integration: "verlet",
              approximation: "barnes-hut",
              gravitationalConstant: 0.8
            }
          }
        },
        series: [
          {
            events: {
              click: e => {
                this.onClick(e);
              }
            },
            marker: {
              radius: 10
            },
            dataLabels: {
              enabled: true,
              linkFormat: "",
              allowOverlap: true
            },
            data:
              [
               {
                from: 'Node1',
                to: 'Node2'
               }
              ],
            nodes:
              [
               {
                  id: 'Node2',
                  color: 'green'
               }
              ]
          }
        ]
      }
    };
    
    //the div i am using in render function elsewhere in the code if it matters.
    
        <HighchartsReact 
          highcharts={Highcharts}
          ref={this.chart}
          options={this.state.options} />
    
    

Here is the first modification i try to apply without any problem.
It changes color of the 2 nodes:

Code: Select all

    let myserie = this.state.options.series;
    myserie[0].color = 'red';
    this.setState({ options: { series: myserie }});
And my problem happens here with this second modification
I try to change color from one node :

Code: Select all

    let myserie = this.state.options.series;
    myserie[0].nodes[0].color = 'red';
    this.setState({ options: { series: myserie }});
This code does not produce error but color does not change.
When i print node color value in console, i get 'red' despite node is remained green.

How could i change this color ?
Subsidiary question, i would also be interested in changing node color identifying it with its name ('Node2') in place of its index (0).

Thanks for your help
amonm
Posts: 7
Joined: Thu Jun 10, 2021 9:11 pm

Re: Network diagram react state update

To sum up the problem :

This line works :

Code: Select all

myserie[0].color = "red";
This line does not :

Code: Select all

myserie[0].nodes[0].color = "red";

I know that the color attribute exists at node level and if i print its value in console, i can see it is red but the node still remains green.
Maybe it comes from my setstate command which does not update at node level ?
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Network diagram react state update

Hi,
Welcome to our forum and thanks for contacting us with your question!

Sorry for the late reply because you wrote a new message, your post was at the end of the reply queue.
At this moment I don't find where is a problem, I need make some tests.

Are you familiar with this documentation from our react wrapper?
https://github.com/highcharts/highchart ... -to-update

I will back to you when I check.
Best regards.
Sebastian Hajdus
Highcharts Developer
amonm
Posts: 7
Joined: Thu Jun 10, 2021 9:11 pm

Re: Network diagram react state update

Hi
Thanks for your response.

Following your link, i did some test changing allowChartUpdate, immutable, updateArgs but it looks like they already have the good value for accurate diagram update.
I also test some forceupdate and some setState with the whole object but without success.

My problem come perhaps from this "the options are overwritten and only the new ones are passed" but can't figure out what the problem is.
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Network diagram react state update

Hi,
Thanks for the message.

Have you tried to use update options? In the example below I using the update to change node color.
https://jsfiddle.net/BlackLabel/dnyfat6x/

API References:
https://api.highcharts.com/class-refere ... int#update

Let me know how are you doing with this.
Best regards.
Sebastian Hajdus
Highcharts Developer
amonm
Posts: 7
Joined: Thu Jun 10, 2021 9:11 pm

Re: Network diagram react state update

Yes i already tried update options.
My code was more like

Code: Select all

chart.series[0].nodeLookup["Node2"].update({ color: 'red' });
but i was also able to change the color of one node.

I am now looking for a clean react state modification way.
I try to check if it is possible to interact with chart object but this object does not exist in my react code.
still searching.
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Network diagram react state update

Hi,
Thanks for the message.

Regarding the status update, I'm unable to propose anything other than what you find in the documentation.
https://github.com/highcharts/highchart ... -to-update

If you have more questions about charts you can reach me any time.
Best regards.
Sebastian Hajdus
Highcharts Developer
amonm
Posts: 7
Joined: Thu Jun 10, 2021 9:11 pm

Re: Network diagram react state update

Hi
Thanks for your answer,

I tried to reproduce as closer as possible your example here :

https://codesandbox.io/s/highcharts-rea ... rked-6zg31

you can see that when you click, a node is added (i.e. data array modification works) but the node array modification does not change.
In the contructor, my blue color property has an effect on the node but when it comes to setstate (same example as yours) it does no work (not changing to red).

I guess there is a difference in the way node update are done is the state.

I am also trying to use an example such as this one
https://codesandbox.io/s/highcharts-react-demo-kd6iq
allowing me to use update method you suggest but the problem is that i cannot reach the inside method ("load" in the example) from outside the state.
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Network diagram react state update

Hi,
Thanks for the details.

Could you check this example?

Demo with updated state and nodes according to the documentation.
https://stackblitz.com/edit/react-yunt2x

API References:
https://api.highcharts.com/highcharts/s ... raph.nodes
https://github.com/highcharts/highchart ... -to-update

Best regards.
Sebastian Hajdus
Highcharts Developer
amonm
Posts: 7
Joined: Thu Jun 10, 2021 9:11 pm

Re: Network diagram react state update

Hi
Thanks for your example,
The last remaining problem is that the modification requires a full redraw of the diagram.
It does not only change color or marker size of one element.
Could you possibly do that in your example ?

I developed a code which allow me to do this using the example i mention before (https://codesandbox.io/s/highcharts-react-demo-kd6iq)
but it is copying the state to an external variable thru the timer and then accessing the state thru this variable (i will try to post an example).
the stanger thing for me is that using the intermediate object i can access update method which i cannot directly accessing the state.

To sum up, so far :
- using update method (i.e. "regular" javascript event) it is possible to update one node https://jsfiddle.net/BlackLabel/dnyfat6x/
- using state it is possible to redraw everything ( https://stackblitz.com/edit/react-yunt2x ) or partial redraw but changing data in the serie not node (https://codesandbox.io/s/highcharts-rea ... rked-6zg31)

but not to update one node using state ? (my first code) https://codesandbox.io/s/highcharts-rea ... rked-ir31m
User avatar
sebastian.h
Posts: 1734
Joined: Fri Aug 07, 2020 7:08 am

Re: Network diagram react state update

Hi amonm,

Using setState will be the best option for update a single point, you can see it on this example.
https://stackblitz.com/edit/react-be7guk

Best regards.
Sebastian Hajdus
Highcharts Developer

Return to “Highcharts Usage”