From 3d41ebaee957feeabec137c56f83e038ae5fc8e0 Mon Sep 17 00:00:00 2001 From: Ben Brian Date: Mon, 12 Dec 2016 19:07:24 -0500 Subject: [PATCH] Adds new option exporting.csv.dataFormatter, which allows overriding the default number format for HTML data table and exported CSV --- export-csv.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/export-csv.js b/export-csv.js index 99bfe20..3aa119b 100644 --- a/export-csv.js +++ b/export-csv.js @@ -151,23 +151,23 @@ var csv = '', rows = this.getDataRows(), options = (this.options.exporting || {}).csv || {}, + dataFormatter = options.dataFormatter || function (val) { + return Highcharts.numberFormat(val, -1, useLocalDecimalPoint ? (1.1).toLocaleString()[1] : '.', ''); + }, 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] : '.'; + j = row.length; while (j--) { val = row[j]; if (typeof val === "string") { val = '"' + val + '"'; } if (typeof val === 'number') { - if (n === ',') { - val = val.toString().replace(".", ","); - } + val = dataFormatter(val); } row[j] = val; } @@ -187,24 +187,24 @@ */ Highcharts.Chart.prototype.getTable = function (useLocalDecimalPoint) { var html = '', - rows = this.getDataRows(); + rows = this.getDataRows(), + options = (this.options.exporting || {}).csv || {}, + dataFormatter = options.dataFormatter || function (val) { + return Highcharts.numberFormat(val, -1, useLocalDecimalPoint ? (1.1).toLocaleString()[1] : '.', ''); + }; // Transform the rows to HTML each(rows, function (row, i) { var tag = i ? 'td' : 'th', val, - j, - n = useLocalDecimalPoint ? (1.1).toLocaleString()[1] : '.'; + j; html += ''; for (j = 0; j < row.length; j = j + 1) { val = row[j]; // Add the cell if (typeof val === 'number') { - val = val.toString(); - if (n === ',') { - val = val.replace('.', n); - } + val = dataFormatter(val); html += '<' + tag + ' class="number">' + val + ''; } else {