This tutorial will show how you can dynamically add and remove series to the chart.
Adding a series to the chart is as simple as pushing a series object into chart’s series list:
let series = new am4charts.LineSeries();
chart.series.push(series);
var series = new am4charts.LineSeries();
chart.series.push(series);
The chart will take care of the rest. No further action is necessary.
Here’s a working example:
* --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
/* Create chart instance */
var chart = am4core.create("chartdiv", am4charts.XYChart);
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "year";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var seriesId = chart.series.length + 1;
for (var i = 0; i < chart.data.length; i++) {
chart.data[i]["value" + seriesId] = Math.random() * 300;
var series = new am4charts.LineSeries();
series.data = chart.data;
series.dataFields.valueY = "value" + seriesId;
series.dataFields.categoryX = "year";
series.name = "Series #" + seriesId;
series.fillOpacity = 0.2;
series = chart.series.push(series);
chart.legend = new am4charts.Legend();
chart.cursor = new am4charts.XYCursor();
<div id="chartdiv"></div>
<input type="button" value="Add Series" onclick="addSeries()" />
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
Adding seriesadding-series
When we were adding series to the chart, we were pushing series objects directly to chart’s series list.
Predictably, removing series will involve removing it from series.
Since it’s a List (Array on steroids), we can use its method removeIndex():
chart.series.removeIndex(0);
chart.series.removeIndex(0);
The above will remove the first series from the chart, and will return the removed item.
At this point removed series should disappear from the map without any further action.
What if don’t know the index of particular series, but have a reference to series object?
We can use List’s indexOf() method to find the index, then proceed as per above:
chart.series.removeIndex(
chart.series.indexOf(series_to_remove)
chart.series.removeIndex(
chart.series.indexOf(series_to_remove)
Important thing to note, that while series is removed from the chart, it continues its (orphaned) existence.
Removing a lot of series might result in memory leaks.
As a good housekeepers we need to dispose stuff that is no longer needed.
Since removeIndex() returns the actual object, it’s easy to just call dispose() on it and call it a day:
chart.series.removeIndex(
chart.series.indexOf(series_to_remove)
chart.series.removeIndex(
chart.series.indexOf(series_to_remove)
* --------------------------------------------------------
* This demo was created using amCharts V4 preview release.
* V4 is the latest installement in amCharts data viz
* library family, to be released in the first half of
* For more information and documentation visit:
* https://www.amcharts.com/docs/v4/
* --------------------------------------------------------
/* Create chart instance */
var chart = am4core.create("chartdiv", am4charts.XYChart);
var categoryAxis = chart.xAxes.push(new am4charts.CategoryAxis());
categoryAxis.dataFields.category = "year";
var valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
var seriesId = chart.series.length + 1;
for (var i = 0; i < chart.data.length; i++) {
chart.data[i]["value" + seriesId] = Math.random() * 300;
var series = chart.series.push(new am4charts.LineSeries());
series.dataFields.valueY = "value" + seriesId;
series.dataFields.categoryX = "year";
series.name = "Series #" + seriesId;
series.fillOpacity = 0.2;
function removeSeries() {
if (chart.series.length > 1) {
chart.series.removeIndex(0).dispose();
chart.legend = new am4charts.Legend();
chart.cursor = new am4charts.XYCursor();
<div id="chartdiv"></div>
<input type="button" value="Remove Series" onclick="removeSeries()" />
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
Removing seriesremoving-series