Skip to content
This repository has been archived by the owner on May 29, 2018. It is now read-only.

Commit

Permalink
Adds new option exporting.csv.dataFormatter, which allows overriding …
Browse files Browse the repository at this point in the history
…the default number format for HTML data table and exported CSV
  • Loading branch information
historicbruno committed Dec 13, 2016
1 parent 14b78ac commit 3d41eba
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions export-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -187,24 +187,24 @@
*/
Highcharts.Chart.prototype.getTable = function (useLocalDecimalPoint) {
var html = '<table>',
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 += '<tr>';
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 + '</' + tag + '>';

} else {
Expand Down

0 comments on commit 3d41eba

Please sign in to comment.