看看docs,有一些演示和解釋如何做到這一點。
第一個,創建csv。
Categories,Apples,Pears,Oranges,Bananas
John,8,4,6,5
Jane,3,4,2,3
Joe,86,76,79,77
Janet,3,16,13,15
二,定義基本圖表選項。
var options = {
chart: {
renderTo: 'container',
defaultSeriesType: 'column'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Units'
}
},
series: []
};
第三,處理數據。
$.get('data.csv', function(data) {
// Split the lines
var lines = data.split('\n');
// Iterate over the lines and add categories or series
$.each(lines, function(lineNo, line) {
var items = line.split(',');
// header line containes categories
if (lineNo == 0) {
$.each(items, function(itemNo, item) {
if (itemNo > 0) options.xAxis.categories.push(item);
});
}
// the rest of the lines contain data with their name in the first position
else {
var series = {
data: []
};
$.each(items, function(itemNo, item) {
if (itemNo == 0) {
series.name = item;
} else {
series.data.push(parseFloat(item));
}
});
options.series.push(series);
}
});
// Create the chart
var chart = new Highcharts.Chart(options);
});
和here is the result。
謝謝傑里米。我得到了所有這些,但是你的示例在你的函數中使用了手工製作的數據。我如何將上面的數據放在CSV中?您是否有適當格式的CSV示例。我真的在這個黑暗中:( – Daveable 2013-02-15 22:38:30
請看看裏卡多的解決方案 - 他提供了一個jQuery示例讀取CSV,然後使用它作爲數據數組。 – Jeremy 2013-02-19 04:48:02
Sooo,我嘗試了不同的東西。使用你的數據以上: 在data.json文件中,只是指向它。然而,它仍然鎖定圖表。我看到(通過控制檯)它拉動data.json文件就好了,但圖表本身如果Stockchart可以直接處理這些數據,它不應該在.json文件中工作嗎? – Daveable 2013-02-19 15:35:27