hcm
Posts: 6
Joined: Thu Sep 29, 2022 2:56 am

Keeping indicators and drawings as graph updates

Hello,

I have a graph that updates periodically. Every X second it redraws all the points on the graph. The problem is that when I turn on indicators such as simple moving average or when I draw a line on the graph, when the graph updates all of this is erased. I'm wondering if there is any way of keeping indicators and drawings on the graph when it updates. This is more of a general question, I don't have a link to the code as there are many dependencies for it to work.

Thanks
hubert.k
Posts: 1164
Joined: Mon Apr 11, 2022 11:48 am

Re: Keeping indicators and drawings as graph updates

Hi hcm!
Thanks for contacting us with your question!

I was trying to reproduce your problem, but in my case, everything is working correctly. When I update the chart and add a simple moving average indicator dynamically from the button, after adding a new point and redrawing a chart nothing is erased. Also, if I draw a line or text annotation it is also staying in the correct place. You can check it in the demo below. I have a question, how are you updating the chart? Maybe you are destroying the chart and that's why all indicates are also destroyed. It's just my thoughts, let me know :)

Demo: https://jsfiddle.net/BlackLabel/7Lnvju5o/

I am looking for your response.
Regards!
Hubert Kozik
Highcharts Developer
hcm
Posts: 6
Joined: Thu Sep 29, 2022 2:56 am

Re: Keeping indicators and drawings as graph updates

Thank you for the clear explanation Hubert. Indeed it turns out I was destroying the graph and re-initiating it, that explains why indicators weren't showing up after update. Now after integrating your solution to my code, it works. But there is another issue now: the browser on which my code runs has a lot of lag, it even freezes at times. Any chance you can help me out?
Here is my code: https://jsfiddle.net/j3210938120/tr2anzq6/32/

Thanks again!
hubert.k
Posts: 1164
Joined: Mon Apr 11, 2022 11:48 am

Re: Keeping indicators and drawings as graph updates

hcm,
I am not sure if that was a problem, but you were looping through not existing items (raw_json and there was an error in the console. I have changed the condition in the for loop and everything seems to work without any freezing.

Demo: https://jsfiddle.net/BlackLabel/dzn64b8t/

Regards!
Hubert Kozik
Highcharts Developer
hcm
Posts: 6
Joined: Thu Sep 29, 2022 2:56 am

Re: Keeping indicators and drawings as graph updates

I found the problem: the data keeps adding on top of itself. If you look at the volume, it keeps increasing after every iteration of setInterval. This causes the chart to be super laggy and choppy. I have an ajax function in my original code that gets the data, I tried reproducing it here https://jsfiddle.net/j3210938120/tr2anzq6/67/ . As you can see, there is a global variable called chartdata. In this code editor, I can set it to empty as you can see on line 52, and there is no problem, the code works perfectly. However, if I try the same thing in my original code, I can't get it to work. Because the ajax function is asynchronous there, so somehow when I do chartdata = [] or chartdata.length = 0, wherever I put it in the code it doesn't erase the contents of the global array chartdata. What would be the solution here? Any help would be very appreciated,

Thank you
hubert.k
Posts: 1164
Joined: Mon Apr 11, 2022 11:48 am

Re: Keeping indicators and drawings as graph updates

hcm,

Well, I am not sure what you want to achieve. In your code, you are adding 20 points to the same X-value, I am not sure if that is the desired behaviour by you. I am also not sure, why you need to have a global variable chartdata. It seems to me like an algorithmic issue instead of a chart configuration. What I can suggest is that maybe use should use setData method (https://api.highcharts.com/class-refere ... es#setData), but I am not sure if that's what you are looking for. It's my thought because you are iterating over the "ajax response" and adding it to the chart data.

Regards!
Hubert Kozik
Highcharts Developer
hcm
Posts: 6
Joined: Thu Sep 29, 2022 2:56 am

Re: Keeping indicators and drawings as graph updates

Yes, setData was exactly what I needed. Thank you!
hubert.k
Posts: 1164
Joined: Mon Apr 11, 2022 11:48 am

Re: Keeping indicators and drawings as graph updates

That's great to hear! In case of any further questions, feel free to contact us again.
Hubert Kozik
Highcharts Developer

Return to “Highcharts Stock”