Exporting data for visible series only

Normally, using amCharts 4 data exporting functionality, the chart dumps all available source data. This short tutorial will show code which assures that only currently visible series data is exported.

Limiting exported fields

By default all source data (as set in chart's data) is exported.

We can control what fields are exported using export's setting dataFields. It's a setting which holds object mapping data field name and column name.

Since we don't know what series will be visible at the moment of export, we can't just set up dataFields in advance.

So, we're going to use an export adapter formatDataFields which is applied to dataFields contents before export. The adapter will cycle through chart's series and will add fields only for those that are visible at the moment.

chart.exporting.adapter.add("formatDataFields", function(data, target) {
  data.dataFields = {};
  chart.series.each(function(series) {
    data.dataFields[series.dataFields.dateX] = series.dataFields.dateX;
    if (series.visible) {
      data.dataFields[series.dataFields.valueY] = series.dataFields.valueY;
    }
  });
  return data;
});
chart.exporting.adapter.add("formatDataFields", function(data, target) {
  data.dataFields = {};
  chart.series.each(function(series) {
    data.dataFields[series.dataFields.dateX] = series.dataFields.dateX;
    if (series.visible) {
      data.dataFields[series.dataFields.valueY] = series.dataFields.valueY;
    }
  });
  return data;
});
{
  // ...
  "exporting": {
    "adapter": {
      "formatDataFields": function(data, target) {
        data.dataFields = {};
        chart.series.each(function(series) {
          data.dataFields[series.dataFields.dateX] = series.dataFields.dateX;
          if (series.visible) {
            data.dataFields[series.dataFields.valueY] = series.dataFields.valueY;
          }
        });
        return data;
      })
    }
  }
}

Example

See the Pen amCharts 4: Exporting data for visible Series only by amCharts team (@amcharts) on CodePen.