jahnvi25
Posts: 284
Joined: Thu Oct 04, 2012 12:16 pm

load annotation from localstorage

I know highstock -> stock tool has 'Save' chart button which saves chart in localstorage.

i just want to save annotations in localstorage.. which i am doing by below code

Code: Select all

localStorage.setItem('customChart', JSON.stringify({
                        annotations: annotations});
but my question is how can i load these annotations back to chart ?

Thanks
jahnvi25
Posts: 284
Joined: Thu Oct 04, 2012 12:16 pm

Re: load annotation from localstorage

any reply to this one ?
mateusz.b
Posts: 2006
Joined: Tue Jul 13, 2021 11:34 am

Re: load annotation from localstorage

Hello jahnvi25,

Thanks for contacting us with your question.

As you have already noticed, in stock tools you can find save chart option. Unfortunatelly, we do not have native for highcharts function to load data from local storage. In you case you can retrieve your saved annotations with localStorage.getItem() and then add these annotations to chart with chart.update() method.
API reference: https://api.highcharts.com/class-refere ... art#update

Let me know if you have any further questions!
Best regards!
Mateusz Bernacik
Highcharts Developer
jahnvi25
Posts: 284
Joined: Thu Oct 04, 2012 12:16 pm

Re: load annotation from localstorage

Thanks a lot. for reply.. currently i am doing..
//get the annotation array from local storage.. and loop through the array and do chart.addAnnotation(ann).
which is working.. do you think better approach would be

chart.update({annotations:annAry});
mateusz.b
Posts: 2006
Joined: Tue Jul 13, 2021 11:34 am

Re: load annotation from localstorage

Hi jahnvi25,

The only thing to consider here is updating performance. As long as you don't add too many annotations at once, there should be no difference, but if you really want to min-max then I would suggest to either use addAnnotation with disabled chart redraw (second method parameter) and then redraw chart only once in last iteration of the loop, or use chart.update once with whole array of annotations. The effect should be exactly the same so basically it is up to you.

Feel free to ask any further questions.
Best regards!
Mateusz Bernacik
Highcharts Developer
jahnvi25
Posts: 284
Joined: Thu Oct 04, 2012 12:16 pm

Re: load annotation from localstorage

Thanks a lot for reply.. but when i try chart.update({annotations:annAry}); this seems to be not working..
only addAnnotation is working... i found the old post about method being not there..

viewtopic.php?t=39748

is this still valid ?
Thanks
mateusz.b
Posts: 2006
Joined: Tue Jul 13, 2021 11:34 am

Re: load annotation from localstorage

Hello jahnvi25,

This is still the case, annotations should be managed via addAnnotation and removeAnnotation methods.
API reference: https://api.highcharts.com/class-refere ... Annotation,
https://api.highcharts.com/class-refere ... Annotation

Best regards!
Mateusz Bernacik
Highcharts Developer
jahnvi25
Posts: 284
Joined: Thu Oct 04, 2012 12:16 pm

Re: load annotation from localstorage

one more question on same topic.. is there a way to know points on which annotation has drawn.. like startTime(x axis) and end time(x -axis).. that will help us to load the annotation only for specific timeframe...

FYI -- i am only storing userOptions from annotation in localstorage and loading that.. in addition to that i would like to know what time frame annotation is spanned ?
mateusz.b
Posts: 2006
Joined: Tue Jul 13, 2021 11:34 am

Re: load annotation from localstorage

Hello jahnvi25,

It depends on the used annotation but in general it should be possible, and you can always add additional annotation properies which you can later access with chart.annotations path.
Demo: https://jsfiddle.net/BlackLabel/7x0ruycv/

Regards!
Mateusz Bernacik
Highcharts Developer

Return to “Highcharts Stock”