0

我一直在以这种方式通过 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 }}
            }]

我宁愿只是朝着正确的方向前进,而不是有人只是为我编写代码。任何帮助表示赞赏!

4

1 回答 1

2
 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 %}
 ]
于 2013-03-07T18:58:20.927 回答