From 41aa88f3872b1e9027770a9506a00d00b06934f2 Mon Sep 17 00:00:00 2001 From: David Joerg Date: Fri, 7 Mar 2014 15:12:57 -0500 Subject: [PATCH 1/5] use HTML5 download attribute rather than hitting a server --- export-csv.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/export-csv.js b/export-csv.js index 5e32cc5..d6a847d 100644 --- a/export-csv.js +++ b/export-csv.js @@ -17,7 +17,7 @@ // Options dateFormat = options.dateFormat || '%Y-%m-%d %H:%M:%S', itemDelimiter = options.itemDelimiter || ',', // use ';' for direct import to Excel - lineDelimiter = options.lineDelimeter || '\n'; + lineDelimiter = options.lineDelimeter || "%0A"; each (this.series, function (series) { if (series.options.includeInCSVExport !== false) { @@ -55,18 +55,19 @@ return csv; }; - // Now we want to add "Download CSV" to the exporting menu. We post the CSV - // to a simple PHP script that returns it with a content-type header as a - // downloadable file. - // The source code for the PHP script can be viewed at - // https://raw.github.com/highslide-software/highcharts.com/master/studies/csv-export/csv.php + // Now we want to add "Download CSV" to the exporting menu. if (Highcharts.getOptions().exporting) { Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({ text: Highcharts.getOptions().lang.downloadCSV || "Download CSV", onclick: function () { - Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', { - csv: this.getCSV() - }); + // http://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction + var a = document.createElement('a'); + a.href = 'data:attachment/csv,' + this.getCSV(); + a.target = '_blank'; + a.download = this.title.text + '.csv'; + document.body.appendChild(a); + a.click(); + a.remove(); } }); } From 092e2ba8d0a0ab69c1c352137803020c7e38a192 Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Tue, 12 Aug 2014 15:23:31 +1000 Subject: [PATCH 2/5] Update export-csv.js Updates borrowed from raveren/export-csv --- export-csv.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/export-csv.js b/export-csv.js index d6a847d..51214b9 100644 --- a/export-csv.js +++ b/export-csv.js @@ -61,13 +61,22 @@ text: Highcharts.getOptions().lang.downloadCSV || "Download CSV", onclick: function () { // http://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction - var a = document.createElement('a'); - a.href = 'data:attachment/csv,' + this.getCSV(); - a.target = '_blank'; - a.download = this.title.text + '.csv'; - document.body.appendChild(a); - a.click(); - a.remove(); + var title = ((this.title || {}).text || 'chart') + '.csv', + a = document.createElement('a'); + + if ("download" in a) { // modern browser - no need to use backend script + a.href = 'data:attachment/csv,' + encodeURIComponent(csv); + a.target = '_blank'; + a.download = title; + document.body.appendChild(a); + a.click(); + a.remove(); + } else { + Highcharts.post(url, { + csv: csv, + title: title + }); + } } }); } From b40cdda1e66e9d19775723de0db7046f7b0b9584 Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Mon, 18 Aug 2014 11:13:24 +1000 Subject: [PATCH 3/5] Update code comment for CSV downloading. Change data URI mime type to "text/csv". Add UTF-8 as specific charset to data URI. Fix for returning CSV and setting default URL. Fixes #20 --- export-csv.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/export-csv.js b/export-csv.js index 610e5d6..79cbbab 100644 --- a/export-csv.js +++ b/export-csv.js @@ -58,6 +58,14 @@ }; // Now we want to add "Download CSV" to the exporting menu. + // If supported by the browser we use the download attribute + // on an anchor element to tell the browser to return the url + // data as a downloadable file. + // If the download attribute is not supported we post the CSV + // to a simple PHP script that returns it with a content-type + // header as a downloadable file. + // The source code for the PHP script can be viewed at + // https://raw.github.com/highslide-software/highcharts.com/master/studies/csv-export/csv.php if (Highcharts.getOptions().exporting) { Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({ text: Highcharts.getOptions().lang.downloadCSV || "Download CSV", @@ -67,15 +75,15 @@ a = document.createElement('a'); if ("download" in a) { // modern browser - no need to use backend script - a.href = 'data:attachment/csv,' + encodeURIComponent(csv); + a.href = 'data:text/csv;charset=UTF-8,' + encodeURIComponent(csv); a.target = '_blank'; a.download = title; document.body.appendChild(a); a.click(); a.remove(); } else { - Highcharts.post(url, { - csv: csv, + Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', { + csv: this.getCSV(), title: title }); } From ff01c93ed79775ff52f0279e7d218cdc4a648ee4 Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Mon, 18 Aug 2014 11:41:12 +1000 Subject: [PATCH 4/5] Fix for undefined csv variable. Fixes #20 --- export-csv.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/export-csv.js b/export-csv.js index 79cbbab..eb36ef1 100644 --- a/export-csv.js +++ b/export-csv.js @@ -70,6 +70,8 @@ Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({ text: Highcharts.getOptions().lang.downloadCSV || "Download CSV", onclick: function () { + var csv = this.getCSV(); + // http://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction var title = ((this.title || {}).text || 'chart') + '.csv', a = document.createElement('a'); @@ -83,7 +85,7 @@ a.remove(); } else { Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', { - csv: this.getCSV(), + csv: csv, title: title }); } From 38de34b700b9a29c5590f80d1395c2499dedaccd Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Mon, 18 Aug 2014 13:40:28 +1000 Subject: [PATCH 5/5] Fix setting custom filename Add option for specifying custom url parameter for csv.php as options.exporting.csv.url Add custom filename support to csv.php Fixes #20 Fixes #9 Fixes #12 --- csv.php | 17 +++++++++++++++++ export-csv.js | 12 ++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 csv.php diff --git a/csv.php b/csv.php new file mode 100644 index 0000000..b209749 --- /dev/null +++ b/csv.php @@ -0,0 +1,17 @@ + diff --git a/export-csv.js b/export-csv.js index eb36ef1..3ba9981 100644 --- a/export-csv.js +++ b/export-csv.js @@ -70,23 +70,27 @@ Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({ text: Highcharts.getOptions().lang.downloadCSV || "Download CSV", onclick: function () { + var title = (this.options.title || {}).text, + exportingCsvOptions = (this.options.exporting || {}).csv || {}; + + var url = exportingCsvOptions.url || 'http://www.highcharts.com/studies/csv-export/csv.php'; var csv = this.getCSV(); // http://stackoverflow.com/questions/17836273/export-javascript-data-to-csv-file-without-server-interaction - var title = ((this.title || {}).text || 'chart') + '.csv', + var filename = (title || 'chart') + '.csv', a = document.createElement('a'); if ("download" in a) { // modern browser - no need to use backend script a.href = 'data:text/csv;charset=UTF-8,' + encodeURIComponent(csv); a.target = '_blank'; - a.download = title; + a.download = filename; document.body.appendChild(a); a.click(); a.remove(); } else { - Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', { + Highcharts.post(url, { csv: csv, - title: title + filename: filename }); } }