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

use HTML5 download attribute rather than hitting a server #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions export-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
});
}
Expand Down