Skip to content

Exporting data

This tutorial looks at configuration options for data export with Exporting plugin.

In order for exporting plugin to be able to export data, we need to set its dataSource setting.

It can be any array of objects, but most probably we’ll want to set it to the same value we are using to feed data to series.

let exporting = am5plugins_exporting.Exporting.new(root, {
menu: am5plugins_exporting.ExportingMenu.new(root, {}),
dataSource: data
});

If dataSource is set, export menu will automatically include data-related exports, like CSV and XLSX.

Normally, the plugin will export values as they are in data.

We can specify which fields in data hold numeric values and what format to use to format them using numericFields setting, which is an array of keys, as well as numberFormat setting which specify number format to use.

let exporting = am5plugins_exporting.Exporting.new(root, {
menu: am5plugins_exporting.ExportingMenu.new(root, {}),
dataSource: data,
numericFields: ["value", "value2"],
numberFormat: "#,###.00"
});

For available number formatting syntax, refer to “Formatting numbers” tutorial.

Similarly, we can also use dataFields to specify keys in data holding date/time information, and dateFormat setting to use when formatting those values.

let exporting = am5plugins_exporting.Exporting.new(root, {
menu: am5plugins_exporting.ExportingMenu.new(root, {}),
dataSource: data,
dateFields: ["date"],
dateFormat: "yyyy-MM-dd"
});

For available date/time formatting syntax, refer to “Formatting dates” tutorial.

We can also control which columns and in what order data will be exported, as well as override column names using two settings dataFields and dataFieldsOrder.

dataFields is an object which identifies which columns to include (object key) and how to name their column (value).

If dataFields is set, only those columns that are included in it, will be exported. If it’s not set, all columns from source data will be exported.

dataFieldsOrder is an array of keys. When exporting, the columns will be sorted in the same order as the keys in the dataFieldsOrder.

Contrary to dataFields, not including a column in dataFieldsOrder will not omit it from export, just place it at the end.

let exporting = am5plugins_exporting.Exporting.new(root, {
menu: am5plugins_exporting.ExportingMenu.new(root, {}),
dataSource: data,
dataFields: {
value: "Value (USD)",
value2: "Value (EUR)",
date: "Date"
},
dataFieldsOrder: ["value", "value", "date"]
});
Open in:New tabChart Maker (opens in a new tab)
Exporting data

When exporting data, plugin will pre-process it: apply number/date formatting, sorting, column selection, etc.

Once that is done, plugin will invoke dataprocessed event, passing in processed data to possible event handlers.

At this point, we still have a chance to modify the data any way we want, e.g.:

exporting.events.on("dataprocessed", function(ev) {
for(var i = 0; i < ev.data.length; i++) {
ev.data[i].sum = ev.data[i].value + ev.data[i].value2;
}
});

The above will add an additional column “sum” to exported data, with automatically-calculated sum from two other columns in data.

Open in:New tabChart Maker (opens in a new tab)
Exporting data with custom menu

If we need to considerably alter data that is also used for series, we need to build a new array in dataprocessed handler, then assign it back to event.data:

exporting.events.on("dataprocessed", function(ev) {
// This will override export data, but will not modify original data source
ev.data = [{
foo: "bar"
}, {
moo: "boo"
}]
});

Before the XLSX file is written, the plugin dispatches the workbookready event. Its handler can still change the document.

exporting.events.on("workbookready", function(event) {
// Modify event.workbook which is an xlsx workbook
// ...
});

The event object has:

  • workbook: the workbook, in the SheetJS workbook format.
  • workbookOptions: options for writing it, as in SheetJS.
  • xlsx: the library that writes the file, with the SheetJS API.

This adds a second sheet:

exporting.events.on("workbookready", function(event) {
event.workbook.SheetNames.push("Data 2");
event.workbook.Sheets["Data 2"] = event.xlsx.utils.aoa_to_sheet([
[ "Hello", "world" ],
[ "Hello", "world" ],
[ "Hello", "world" ],
]);
});
Open in:New tabChart Maker (opens in a new tab)
Custom XLSX export

Since 5.25.0, XLSX files are written by amCharts’ own module, which follows the SheetJS API but only writes XLSX. Earlier versions included SheetJS.

Supported Logs a console warning and does nothing
write, writeFile (XLSX only) Other bookType values, compression, macros, writeFileAsync
utils: aoa_to_sheet, json_to_sheet, sheet_add_aoa, sheet_add_json, sheet_new, book_new, book_append_sheet, encode_*, decode_*, cell_set_number_format, cell_set_hyperlink, cell_set_internal_link read, readFile, sheet_to_*, table_to_*, sheet_add_dom, format_cell, book_set_sheet_visibility, cell_add_comment, sheet_set_array_formula
Cells: values, types (including dates), number formats (z), formulas (f), hyperlinks (l) Comments (c), array formulas
Sheets: !ref, !cols, !rows, !merges !autofilter, !protect, !margins
Workbook: SheetNames, Sheets, Props Workbook (defined names, views, sheet visibility), Custprops

For anything the built-in writer doesn’t do, load SheetJS yourself and pass it in the xlsxLib setting. event.xlsx is then SheetJS.

import * as XLSX from "xlsx";
let exporting = am5plugins_exporting.Exporting.new(root, {
menu: am5plugins_exporting.ExportingMenu.new(root, {}),
dataSource: data,
xlsxLib: XLSX
});

To load SheetJS only when XLSX is first exported, set xlsxLib to a function that returns it:

let exporting = am5plugins_exporting.Exporting.new(root, {
menu: am5plugins_exporting.ExportingMenu.new(root, {}),
dataSource: data,
xlsxLib: () => import("xlsx")
});