Page 1 of 1

load annotation from localstorage

Posted: Mon Oct 18, 2021 6:57 pm
by jahnvi25
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

Re: load annotation from localstorage

Posted: Tue Oct 19, 2021 5:15 pm
by jahnvi25
any reply to this one ?

Re: load annotation from localstorage

Posted: Wed Oct 20, 2021 7:18 am
by mateusz.b
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!

Re: load annotation from localstorage

Posted: Wed Oct 20, 2021 1:57 pm
by jahnvi25
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});

Re: load annotation from localstorage

Posted: Thu Oct 21, 2021 8:01 am
by mateusz.b
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!

Re: load annotation from localstorage

Posted: Thu Oct 21, 2021 12:43 pm
by jahnvi25
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

Re: load annotation from localstorage

Posted: Mon Oct 25, 2021 12:40 pm
by mateusz.b
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!

Re: load annotation from localstorage

Posted: Mon Oct 25, 2021 3:46 pm
by jahnvi25
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 ?

Re: load annotation from localstorage

Posted: Tue Nov 02, 2021 2:18 pm
by mateusz.b
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!