我一直在以这种方式通过Django创建一些Highcharts:
...get rows from database
rows = cursor.fetchall()
months = list()
avg_class = list()
for i in range(0, len(rows)):
months.append(rows[i][2])
avg_class.append(rows[i][5])
months_j = json.dumps(months, cls=DecimalandDateEncoder)
avg_class_j = json.dumps(avg_class, cls=DecimalandDateEncoder)
send to my template....
其中包含图表的javascript。
xAxis: {
type: 'datetime',
categories: {{ months|safe }},
etc...........
},
series: [{
name: 'Series Name',
data: {{ avgclassdata }}
}]
这很好用,有这样的数据。
Month Avg Class Data
1/1/2012 17.600493
2/1/2012 18.114341
3/1/2012 16.246443
4/1/2012 16.09489
现在,我有这样的数据:
Location Month Avg Class Data
Location 1 1/1/2012 17.600493
Location 1 2/1/2012 18.114341
Location 1 3/1/2012 16.246443
Location 1 4/1/2012 16.09489
Location 2 1/1/2012 21.56584
Location 2 2/1/2012 19.54654
Location 2 3/1/2012 17.54654
Location 2 4/1/2012 20.54551
这可以是任意数量的地点。我想创建一个使用每个位置组作为系列的Highchart。我有点不知所措,如何遍历我的行结果,并将该位置用作系列名称,然后将相应的平均类数据用作系列数据。
我的最终目标是从我的数据库结果中创建:
xAxis: {
type: 'datetime',
categories: {{ months|safe }},
etc...........
},
series: [{
name: Here would go the name of Location 1,
data: {{ location 1 data }}
}, {
name: Here would go the name of Location 2,
data: {{ location 2 data }}
}]
我宁愿只是朝着正确的方向努力,而不是只为我编写代码。任何帮助表示赞赏!
答案 0 :(得分:0)
xAxis: {
type: 'datetime',
categories: {{ months|safe }},
etc...........
},series: [
{% for location in locations %}
{
name: Here would go the name of {{location.name}},
data: [
{% for data in location_data %}
{{data}},
{% endfor %}
]
},
{% endfor %}
]