Export Multi-Series Chart Data as CSV from Export-Options - CanvasJS JavaScript Charts

Export Multi-Series Chart Data as CSV from Export-Options - CanvasJS JavaScript Charts

by canvasjs

HTML

<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/><!-- Just so that JSFiddle's Result label doesn't overlap the Chart -->
<div id="chartContainer" style="height: 360px; width: 100%;"></div>

JavaScript

var chart = new CanvasJS.Chart("chartContainer", {
  title: {
    text: "Exporting Multi-Series Chart Data as CSV"
  },
  zoomEnabled: true,
  exportEnabled: true,
  data: [
    {
      type: "column",
      dataPoints: [
        { x: 10, y: 71 },
        { x: 20, y: 55 },
        { x: 30, y: 50 },
        { x: 40, y: 65 },
        { x: 50, y: 95 },
        { x: 60, y: 68 },
        { x: 70, y: 28 },
        { x: 80, y: 34 },
        { x: 90, y: 14 }
      ]
    }, {
      type: "column",
      dataPoints: [
        { x: 10, y: 171 },
        { x: 20, y: 155 },
        { x: 30, y: 150 },
        { x: 40, y: 165 },
        { x: 50, y: 195 },
        { x: 60, y: 168 },
        { x: 70, y: 128 },
        { x: 80, y: 134 },
        { x: 90, y: 114 }
      ]
    }					
  ]
});

chart.render();

var toolBar = document.getElementsByClassName("canvasjs-chart-toolbar")[0];
if(chart.get("exportEnabled")){
  var exportCSV = document.createElement('div');
  var text = document.createTextNode("Save as CSV");
  exportCSV.setAttribute("style", "padding: 12px 8px; background-color: white; color: black")
  exportCSV.appendChild(text);

  exportCSV.addEventListener("mouseover", function(){
    exportCSV.setAttribute("style", "padding: 12px 8px; background-color: #2196F3; color: white")
  });
  exportCSV.addEventListener("mouseout", function(){
    exportCSV.setAttribute("style", "padding: 12px 8px; background-color: white; color: black")
  });
  exportCSV.addEventListener("click", function(){
    downloadCSV({ filename: "chart-data.csv", chart: chart })
  });
  toolBar.lastChild.appendChild(exportCSV);
}
else {
  var exportCSV = document.createElement('button');	
  var text = document.createTextNode("Save as CSV");	
  exportCSV.appendChild(text);
  exportCSV.addEventListener("click", function(){
    downloadCSV({ filename: "chart-data.csv", chart: chart })
  });
  document.body.appendChild(exportCSV)
}


function mergeData(data) {
  var mergedDps = [],
      result = [];
  for...