From a7f2ce0e8df5ea2342890b3d5f64c3c2909f9d00 Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Tue, 12 Aug 2014 12:02:51 +1000 Subject: [PATCH 1/6] Fix typo for options.lineDelimiter Fixes #15 --- export-csv.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/export-csv.js b/export-csv.js index 5e32cc5..ba78340 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.lineDelimiter || '\n'; each (this.series, function (series) { if (series.options.includeInCSVExport !== false) { From 092e2ba8d0a0ab69c1c352137803020c7e38a192 Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Tue, 12 Aug 2014 15:23:31 +1000 Subject: [PATCH 2/6] 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 314e660e561948a2b4ec6f4e571deb7774b76f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torstein=20H=C3=B8nsi?= Date: Tue, 12 Aug 2014 09:12:43 +0200 Subject: [PATCH 3/6] Use length of longest series when differing lengths. Closes #10. Closes #19. --- README.md | 4 ++++ export-csv.js | 4 +++- manifest.json | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 09ce06c..4fb2fe1 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ This plugin allows the user to export the chart data to a CSV string. The contents of the plugin is located in the javascript file "export-csv.js". This plugin is published under the MIT license, and the license document is included in the repository. +### Demos +* [Categorized chart](http://jsfiddle.net/highcharts/cqjvD/) +* [Highstock with time axis](http://jsfiddle.net/highcharts/2Jyn5/) + ### Options * `exporting.csv.dateFormat` Which date format to use for exported dates on a datetime X axis. See [Highcharts.dateFormat](http://api.highcharts.com/highcharts#Highcharts.dateFormat\(\)). diff --git a/export-csv.js b/export-csv.js index ba78340..fb2a46f 100644 --- a/export-csv.js +++ b/export-csv.js @@ -12,6 +12,7 @@ csv = "", row, col, + maxRows, options = (this.options.exporting || {}).csv || {}, // Options @@ -44,7 +45,8 @@ }); // Transform the columns to CSV - for (row = 0; row < columns[0].length; row++) { + maxRows = Math.max.apply(this, Highcharts.map(columns, function (col) { return col.length; })); + for (row = 0; row < maxRows; row++) { line = []; for (col = 0; col < columns.length; col++) { line.push(columns[col][row]); diff --git a/manifest.json b/manifest.json index 5d26b50..99bda48 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "Export-CSV", - "version": "1.0.3", + "version": "1.0.4", "title": "Export Chart Data To CSV", "demo": [ "http://jsfiddle.net/highcharts/cqjvD/", From b40cdda1e66e9d19775723de0db7046f7b0b9584 Mon Sep 17 00:00:00 2001 From: tomascassidy Date: Mon, 18 Aug 2014 11:13:24 +1000 Subject: [PATCH 4/6] 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 5/6] 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 6/6] 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 }); } }