Serializing and Deserializing

Overview

If you want to save annotations drawn by a user or to load them to a chart, you can use the getAnnotationAt() and getAnnotationsCount() methods to go through all annotations and gather the data you need. But there is an easier way: you can also use built-in methods for serializing and deserializing annotations, which allow to work with the data either in JSON or in XML format. To learn more, see this article.

Methods

JSON

To create a list of annotations in JSON format, use the toJson() method. It has an optional parameter: stringify, the parameter determines whether the JSON list is returned as a string or as an object.

To load the list of annotations, call the fromJson() method.

In the following sample, these two methods are used to serialize annotations drawn (as well as hardcoded) on the first plot and then deserialize them to the second plot:

// serialize annotations from the first plot
var json = chart.plot(0).annotations().toJson();

// deserialize the annotations to the second plot
chart.plot(1).annotations().fromJson(json);

Playground

XML

To create a list of annotations in XML format, use the toXml() method. It has an optional parameter: asXmlNode, the parameter determines whether the JSON list is returned as an XML node or as a string.

To load the list of annotations, call the fromXml() method.

In the sample below, these two methods are used to serialize annotations drawn (as well as hardcoded) on the first plot and then deserialize them to the second plot:

// serialize annotations from the first plot
var xml = chart.plot(0).annotations().toXml();

// deserialize the annotations to the second plot
chart.plot(1).annotations().fromXml(xml);

Playground