Search code examples
javascriptamcharts

Pie chart export to CSV / XLSX with amCharts.js


I can't find a way to export my data from a pie chart to CSV or XLSX. I checked this link from amCharts.js but I can manage to adapt it to my need.

Here's my code:

function getDataProviderForEventsByFlow(){

    $.ajax({
        url: restURI,
        success: function(data) {
            am4core.useTheme(am4themes_kelly);
            chartEventsByFLow = am4core.create("events-by-flow", am4charts.PieChart);
            chartEventsByFLow.hiddenState.properties.opacity = 0;
            chartEventsByFLow.legend = new am4charts.Legend();

            var marker = chartEventsByFLow.legend.markers.template.children.getIndex(0);
            marker.cornerRadius(12, 12, 12, 12);
            marker.strokeWidth = 2;
            marker.strokeOpacity = 1;
            marker.stroke = am4core.color("#ccc");

            chartEventsByFLow.data = data;
            var series = chartEventsByFLow.series.push(new am4charts.PieSeries());
            series.dataFields.value = "number";
            series.dataFields.category = "category";
            series.dataFields.color = "color";
            series.labels.template.disabled = true;
            series.ticks.template.disabled = true;
            series.slices.template.tooltipText = "";
        }
    });
}

And the export functions :

function exportCSVbyFlow() {
  chartEventsByFLow.export.toCSV({}, function(data) {
    this.download(data, this.defaults.formats.CSV.mimeType, "exportCSVvolumetryByFlow.csv");
  });
}

function exportXLSXbyFlow() {
  chartEventsByFLow.export.toXLSX({}, function(data) {
    this.download(data, this.defaults.formats.XLSX.mimeType, "exportXLSXvolumetryByFlow.xlsx");
  });
}

This is the output in console:

Cannot read property 'toCSV' of undefined

Cannot read property 'toXLSX' of undefined

Thank you !


Solution

  • The code you give is using v4 but the link you're trying to use as an example is using v3.

    Have you seen this page? It shows a way to add a menu to a chart with different export options. It also talks about programatically exporting using code like:

    chart.exporting.export("csv");