Skip to content

Chart Serializer

Use ChartSerializer to serialize the whole chart objects into JSON which can be stored and parsed back into a functional object.

The chart serializer is part of the JSON plugin, which needs to be loaded.

You can import those in your TypeScript / ES6 application as JavaScript modules:

import * as am5plugins_json from "@amcharts/amcharts5/plugins/json";

For vanilla JavaScript applications and web pages, you can use “script” version:

<script src="https://cdn.amcharts.com/lib/5/plugins/json.js"></script>

To create a serializer, we’ll need to instantiate a ChartSerializer object:

const serializer = am5plugins_json.ChartSerializer.new(root, {});

Use the serializer’s serializeAll() method, passing in the top-level object like a chart or a Container:

const serializer = am5plugins_json.ChartSerializer.new(root, {});
let json = serializer.serializeAll(chart)

There is a number of settings, that serializer supports:

Setting Type Default Comment
removeEmptyObjects boolean true Remove the empty objects from the resulting JSON.
maxDepth number 10 Maximum depth of recursion when traversing the target object.
includeStates boolean true Include element and template states.
includeAdapters boolean true Include adapters in the output.
includeRoot boolean false Include settings and exportable properties from the related Root element.

NOTE: This option will not include themes.
functionsAs "string" | "function" "function" Export functions as references or stringify them.
resources object Geodata and locales to write by name, e.g. { am5geodata_worldLow }. See “Maps and locales” below.

The below instructs serializer to also include the Root object’s settings, and to output functions as strings:

const serializer = am5plugins_json.ChartSerializer.new(root, {
includeRoot: true,
functionsAs: "string"
});

The serializer will detect Legend or HeatLegend and include it in the output if it’s a child of one of the chart’s built-in containers, like rightAxesContainer, its series or the chart itself.

Custom elements, like labels and the like, will not be included in the output unless they have a "serialize" in their themeTags:

pieSeries.children.push(am5.Label.new(root, {
text: "Hello",
x: am5.p50,
y: am5.p50,
themeTags: ["serialize"]
});

The tag is only required for elements added to a plain Container. Elements added to something the library builds — a series, an axis, or the chart itself — are included automatically, as long as they are your own additions and not data-driven (like bullets) or the element’s built-in parts.

A map’s geodata is written by the name it was loaded under, e.g. "geoJSON": "am5geodata_worldLow", so the config stays small. So is the root’s locale, with includeRoot. With script tags, the serializer finds the names among the globals. With npm imports, give it the same objects in resources:

import am5geodata_worldLow from "@amcharts/amcharts5-geodata/worldLow";
const serializer = am5plugins_json.ChartSerializer.new(root, {
resources: { am5geodata_worldLow }
});

Without it, the whole geodata is copied into the config. To build a chart from such a config, give the parser the same resources (see “Maps and locales”).

A color taken from the root’s interface colors is saved as a reference to it, not as a fixed value:

polygonSeries.mapPolygons.template.setAll({
stroke: root.interfaceColors.get("background")
});

The above is saved as:

"stroke": "@root.interfaceColors.get('background')"

When the config is parsed into a chart with a different theme, e.g. a dark one, the color comes from that theme.

A color taken from the theme’s series palette with getIndex() is saved the same way, so it follows the theme’s palette:

series.set("fill", chart.get("colors").getIndex(2));

The above is saved as:

"fill": "@colors.getIndex(2)"
  • XY
  • Percent (Pie, Funnel, Pyramid)
  • Radar
  • Curve charts (Serpentine, Spiral)
  • Venn
  • Hierarchy (Tree, Pack, Partition, Force-directed)
  • Flow (Sankey, Chord, Arc diagram)
  • WordCloud
  • MapChart
  • Gantt
  • Bullets (series or axis): No support for bullets that reference outside objects or context-based bullets (where bullet code depends on context to determine its appearance)
  • Bullets or adapters that rely on global variables. Parser runs in a different scope, thus global variables might not be available.
  • Bullets: Full charts in bullets
  • Axes: Legend inside axis header
  • Templates: setup function
  • Custom draw functions
  • Events
  • StockChart

You can create and edit JSON configs using Chart Maker.

For more info, refer to “JSON Editor”.