我只是在基于rails 3.1.1和HAML的测试应用程序中使用Highcharts(http://www.highcharts.com)。我还是js的新手,我试图完成高级图表的良好集成。
在我的控制器中,我设置了一些json数组,以便在highcharts中使用。
@category_ids_json = Category.all(:conditions => { :income => false},:select => "id").to_json
@categories_json = Category.all(:conditions => { :income => false}, :select => "id,title,income").to_json
@transactions_json = Transaction.all(:select => "date,title,amount,category_id").to_json
在这些实例变量中,我过滤了一些值并创建了一个新数组,我将其用于highcharts数据数组:
var category_transactions_sum = new Array();
category_transactions_sum.push({title:categories[c].title, amount: transactions_sum})
数组的内容看起来像这样:
[{title: "Salary", amount: 50},{title: "Food", amount: 25},{title: "Recreation", amount: 10}]
现在是时候初始化Highcharts了。这就是我现在初始化它的方式:
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
series: [{
type: 'pie',
name: 'Expenses',
data: [
[category_transactions_sum[0].title, category_transactions_sum[0].amount],
[category_transactions_sum[1].title, category_transactions_sum[1].amount],
[category_transactions_sum[2].title, category_transactions_sum[2].amount],
[category_transactions_sum[3].title, category_transactions_sum[3].amount],
[category_transactions_sum[4].title, category_transactions_sum[4].amount],
[category_transactions_sum[5].title, category_transactions_sum[5].amount],
[category_transactions_sum[6].title, category_transactions_sum[6].amount],
[category_transactions_sum[7].title, category_transactions_sum[7].amount],
[category_transactions_sum[8].title, category_transactions_sum[8].amount],
[category_transactions_sum[9].title, category_transactions_sum[9].amount],
[category_transactions_sum[10].title, category_transactions_sum[10].amount],
[category_transactions_sum[11].title, category_transactions_sum[11].amount],
[category_transactions_sum[12].title, category_transactions_sum[12].amount],
[category_transactions_sum[13].title, category_transactions_sum[13].amount],
[category_transactions_sum[14].title, category_transactions_sum[14].amount],
[category_transactions_sum[15].title, category_transactions_sum[15].amount],
[category_transactions_sum[16].title, category_transactions_sum[16].amount],
[category_transactions_sum[17].title, category_transactions_sum[17].amount],
[category_transactions_sum[18].title, category_transactions_sum[18].amount],
[category_transactions_sum[19].title, category_transactions_sum[19].amount],
[category_transactions_sum[20].title, category_transactions_sum[20].amount],
[category_transactions_sum[21].title, category_transactions_sum[21].amount],
[category_transactions_sum[22].title, category_transactions_sum[22].amount],
[category_transactions_sum[23].title, category_transactions_sum[23].amount],
[category_transactions_sum[24].title, category_transactions_sum[24].amount],
[category_transactions_sum[25].title, category_transactions_sum[25].amount],
[category_transactions_sum[26].title, category_transactions_sum[26].amount],
]
}]
});
我的问题:
我如何遍历category_transactions_sum数组以摆脱highcharts的“data”声明中的那一行。我试过一个for循环,但它不起作用。
有没有更好的方法将数据插入highcharts? Highcharts需要这种格式的数据:
数据:[ ['Firefox',45.0], ['IE',26.8], ['Safari',8.5], ['Opera',6.2], ['其他人',0.7] ]
是否可以做这样的事情?
data: [
myArrayWithPreparedData
]
如果是,我将如何构建这个数组?
非常感谢帮助新手。
答案 0 :(得分:1)
我相信你想要这样的东西:
data: $.map(category_transactions_sum, function(i, c) { return [c.title, c.amount]; })
答案 1 :(得分:1)
你可以尝试这个(适合我):
temp = $.map(json, function(i) { return [[i.in_time, i.status]]; })
showData = JSON.parse(JSON.stringify(temp, null, ""));
然后在系列中使用:
chart.addSeries({
data: showData
});
答案 2 :(得分:0)
应该是:
someVar = [{title: "Salary", amount: 50},{title: "Food", amount: 25},{title: "Recreation", amount: 10}]
data = $.map(someVar, function(i) { return [[i.title, i.amount]]; })
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
series: [{
type: 'pie',
name: 'Expenses',
data: data
}]
});