Home Forums Chart Support Question about Multi Series Graph CSV Export Reply To: Question about Multi Series Graph CSV Export

#35175

@sptrainee31,

It is possible to convert chart data into comma-separated CSV and download it with a few lines of code. Please find the code snippet for the same below,

function convertChartDataToCSV(args) {
  var result = '',
      ctr, keys, columnDelimiter, lineDelimiter, data;

  data = args.data || null;
  if (data == null || !data.length) {
    return null;
  }

  columnDelimiter = args.columnDelimiter || ',';
  lineDelimiter = args.lineDelimiter || '\n';

  var mergedData = mergeData(data);

  keys = Object.keys(mergedData[0]);
  result = '';
  result += keys.join(columnDelimiter);
  result += lineDelimiter;

  mergedData.forEach(function (item) {
    ctr = 0;
    keys.forEach(function (key) {
      if (ctr > 0) result += columnDelimiter;
      result += (typeof (item[key]) != undefined ? item[key] : "");
      ctr++;
    });
    result += lineDelimiter;
  });
  return result;
}

Please take a look at this JSFiddle for working code. Also take a look at this 3rd party plugin, which seems to be working with multi-series chart as well.


Adithya Menon
Team CanvasJS