簡體   English   中英

Django的多系列Highchart圖表

[英]Multi-series Highchart chart from Django

我一直以這種方式通過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 }}
            }]

我寧願只是朝着正確的方向前進,也不願有人為我編寫代碼。 任何幫助表示贊賞!

 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 %}
 ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM