/** * A Highcharts plugin for exporting data from a rendered chart as CSV, XLS or HTML table * * Author: Torstein Honsi * Licence: MIT * Version: 1.3.1 */ /*global Highcharts, window, document, Blob */ (function (Highcharts) { 'use strict'; var each = Highcharts.each, downloadAttrSupported = document.createElement('a').download !== undefined; /** * Get the data rows as a two dimensional array */ Highcharts.Chart.prototype.getDataRows = function () { var options = (this.options.exporting || {}).csv || {}, xAxis = this.xAxis[0], rows = {}, rowArr = [], dataRows, names = [], i, x, // Options dateFormat = options.dateFormat || '%Y-%m-%d %H:%M:%S'; // Loop the series and index values i = 0; each(this.series, function (series) { if (series.options.includeInCSVExport !== false) { names.push(series.name); each(series.points, function (point) { if (!rows[point.x]) { rows[point.x] = []; } rows[point.x].x = point.x; // Pies, funnels etc. use point name in X row if (!series.xAxis) { rows[point.x].name = point.name; } rows[point.x][i] = point.y; }); i += 1; } }); // Make a sortable array for (x in rows) { if (rows.hasOwnProperty(x)) { rowArr.push(rows[x]); } } // Sort it by X values rowArr.sort(function (a, b) { return a.x - b.x; }); // Add header row dataRows = [[xAxis.isDatetimeAxis ? 'DateTime' : 'Category'].concat(names)]; // Transform the rows to CSV each(rowArr, function (row) { // Add the X/date/category row.unshift(row.name || (xAxis.isDatetimeAxis ? Highcharts.dateFormat(dateFormat, row.x) : xAxis.categories ? Highcharts.pick(xAxis.categories[row.x], row.x) : row.x)); dataRows.push(row); }); return dataRows; }; /** * Get a CSV string */ Highcharts.Chart.prototype.getCSV = function (useLocalDecimalPoint) { var csv = '', rows = this.getDataRows(), options = (this.options.exporting || {}).csv || {}, itemDelimiter = options.itemDelimiter || ',', // use ';' for direct import to Excel lineDelimiter = options.lineDelimiter || '\n'; // '\n' isn't working with the js csv data extraction // Transform the rows to CSV each(rows, function (row, i) { var val = '', j = row.length, n = useLocalDecimalPoint ? (1.1).toLocaleString()[1] : '.'; while (j--) { val = row[j]; if (typeof val === "string") { val = '"' + val + '"'; } if (typeof val === 'number') { if (n === ',') { val = val.toString().replace(".", ","); } } row[j] = val; } // Add the values csv += row.join(itemDelimiter); // Add the line delimiter if (i < rows.length - 1) { csv += lineDelimiter; } }); return csv; }; /** * Build a HTML table with the data */ Highcharts.Chart.prototype.getTable = function (useLocalDecimalPoint) { var html = '