gpt4 book ai didi

highcharts - 无法从 Highcharts 获取 csv

转载 作者:行者123 更新时间:2023-12-02 03:50:00 25 4
gpt4 key购买 nike

我想让从我的 Highchart 下载 CSV 文件成为可能......就像在这个例子中一样。

Link to JSFiddle

我已经尝试实现它,但它在我的代码中不起作用....可能是什么原因? 我只是想提醒csv!稍后我想实现你可以下载它......

这是我的代码:

<script language="javascript" type="text/javascript" src="../js/jquery-1.8.3.js">   </script>
<script language="javascript" type="text/javascript" src="../js/jquery-ui-1.9.2.custom.js"</script>
<script src="../js/highcharts.js "></script>
<script src="../js/exporting.js "></script>
<script src="../js/exporting.src.js "></script>
<link rel="stylesheet" href="../css/jquery-ui-1.9.2.custom.css" type="text/css" />

<script type="text/javascript">

(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));



$(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());
});


</script>
</head>
<body>
<div id="container"></div>
<button id="getcsv" > Alert </button>
</body>
</html>

最佳答案

您的代码已经可以运行了:

要下载为 cvs 添加此代码块:

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()
});
}
});

YOUR CODE DEMO

关于highcharts - 无法从 Highcharts 获取 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14747311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com