无法从Highcharts获得csv

时间:2013-02-07 09:07:22

标签: highcharts

我希望能够从我的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> 

1 个答案:

答案 0 :(得分:1)

您的代码已经在运行:供下载,因为csv添加了此代码块:

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