JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://github.highcharts.com/highcharts.js"></script>
<script src="http://github.highcharts.com/modules/exporting.src.js"></script>
<div id="container" style="margin-top:20px;"></div>
<button id="getcsv" > Alert </button>
JavaScript
/**
* A small plugin for getting the CSV of a categorized chart
*/
(function (Highcharts) {
var each = Highcharts.each;
Highcharts.Chart.prototype.getCSV = function () {
var xAxis = this.xAxis[0],
columns = [],
line,
csv = "",
row,
col;
if (xAxis.categories) {
columns.push(xAxis.categories);
columns[0].unshift("");
}
each(this.series, function (series) {
columns.push(series.yData);
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(',') + '\n';
}
return csv;
};
}(Highcharts));
Highcharts.getOptions().exporting.buttons.exportButton.menuItems.push({
text: 'Download CSV',
onclick: function () {
Highcharts.post('http://www.highcharts.com/studies/csv-export/csv.php', {
csv: this.getCSV()
});
}
});
$(function () {
$(document).ready(function () {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'pie',
},
title: {
text: 'Top 5 Power consumer overall'
},
yAxis: {
title: {
text: ' ',
}
},
legend: {
enabled: false
},
series: [{
name: 'Work',
data: [1, 2, 3, 4, 5]
}],
});
});
});
$('#getcsv').click(function () {
alert(chart.getCSV());
});