-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhighcharts-export-csv.js
executable file
·75 lines (67 loc) · 2.8 KB
/
highcharts-export-csv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* A small plugin for getting the CSV of a categorized chart
*/
(function (Highcharts) {
var each = Highcharts.each;
Highcharts.Chart.prototype.getCSV = function () {
var columns = [],
line,
tempLine,
csv = "",
row,
col,
options = (this.options.exporting || {}).csv || {},
// Options
dateFormat = options.dateFormat || '%Y-%m-%d %H:%M:%S',
itemDelimiter = options.itemDelimiter || ',', // use ';' for direct import to Excel
lineDelimiter = options.lineDelimeter || '\n';
each (this.series, function (series) {
if (series.options.includeInCSVExport !== false) {
if (series.xAxis) {
var xData = series.xData.slice(),
xTitle = 'X values';
if (series.xAxis.isDatetimeAxis) {
xData = Highcharts.map(xData, function (x) {
return Highcharts.dateFormat(dateFormat, x)
});
xTitle = 'DateTime';
} else if (series.xAxis.categories) {
xData = Highcharts.map(xData, function (x) {
return Highcharts.pick(series.xAxis.categories[x], x);
});
xTitle = 'Category';
}
columns.push(xData);
columns[columns.length - 1].unshift(xTitle);
}
columns.push(series.yData.slice());
columns[columns.length - 1].unshift(series.name);
}
});
// Transform the columns to CSV
for (row = 0; row < columns[0].length; row++) {
line = [];
for (col = 0; col < columns.length; col++) {
line.push(columns[col][row]);
}
csv += line.join(itemDelimiter) + lineDelimiter;
}
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
if (Highcharts.getOptions().exporting) {
Highcharts.getOptions().exporting.buttons.contextButton.menuItems.push({
//text: Highcharts.getOptions().lang.downloadCSV || "Download CSV",
textKey: 'downloadCSV',
onclick: function () {
Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', {
csv: this.getCSV()
});
}
});
}
}(Highcharts));