使用python flask框架将x轴设置为highcharts plot的日期

时间:2016-04-05 03:29:10

标签: python highcharts flask

我正在使用python flask框架来生成像网页一样的仪表板。我使用highcharts作为图表工具。在test3.html模板中:

{% extends "base.html" %}
{% block head %}
   <script type="text/javascript" src="http://cdn.hcharts.cn/jquery/jquery-1.8.3.min.js"></script>  
   <script type="text/javascript" src="http://cdn.hcharts.cn/highcharts/4.0.1/highcharts.js"></script>  

<script type="text/javascript">
$(function () {
    $('#container').highcharts({
        chart: {
            zoomType: 'xy'
        },
        title: {
            text: 'Average Monthly Temperature and Rainfall in Tokyo'
        },
        subtitle: {
            text: 'Source: WorldClimate.com'
        },
        xAxis: [{
            categories: '{{chart_x}}',
            crosshair: true
        }],
        yAxis: [{ // Primary yAxis
            labels: {
                format: '{value}°C',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            },
            title: {
                text: 'Temperature',
                style: {
                    color: Highcharts.getOptions().colors[1]
                }
            }
        }, { // Secondary yAxis
            title: {
                text: 'Rainfall',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            labels: {
                format: '{value} mm',
                style: {
                    color: Highcharts.getOptions().colors[0]
                }
            },
            opposite: true
        }],
        tooltip: {
            shared: true
        },
        legend: {
            layout: 'vertical',
            align: 'left',
            x: 120,
            verticalAlign: 'top',
            y: 100,
            floating: true,
            backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
        },
        series: [{
            name: 'Rainfall',
            type: 'column',
            yAxis: 1,
            data: {{chart_y1}},
            tooltip: {
                valueSuffix: ' mm'
            }

        }, {
            name: 'Temperature',
            type: 'spline',
            data: {{chart_y2}},
            tooltip: {
                valueSuffix: '°C'
            }
        }]
    });
});
</script>
{% endblock %}
{% block content %}
<h2>first chart</h2>
<div id="container" style="height: 300px"></div>



{% endblock %}

并在view.py中:

@app.route('/test3')
def test3():
    return render_template("test3.html",
                           title='test3',
                           chart_x=['2014-04-11','2014-04-14','2014-04-15','2014-04-16','2014-04-17','2014-04-18','2014-04-21','2014-04-22','2014-04-23','2014-04-24','2014-04-25','2014-04-28'],
                           chart_y1=[49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
                           chart_y2=[7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
                           )

我得到的情节如下:

enter image description here

如您所见,x轴未标记为日期,我不确定如何正确设置。有人可以帮忙吗?

4 个答案:

答案 0 :(得分:1)

请尝试以下操作,它会正常工作。

xAxis: [{ categories: [ {% for item in chart_x %} "{{ item }}", {% endfor %} ], crosshair: true }]

答案 1 :(得分:0)

问题是你有:

    xAxis: [{
        categories: '{{chart_x}}',
        crosshair: true
    }],

应该是:

    xAxis: [{
        categories: {{chart_x}},
        crosshair: true
    }],

答案 2 :(得分:0)

xAxis:[{         类别:&#39; {{chart_x}}&#39;,         十字准线:真的     }], 在上面的xAxis中,您正在尝试获取chart_x的数组,但是您将在字符串中关闭它。因此,不是以字符串形式,只是用{{chart_x}}的语法编写,那么它将起作用。我已经尝试过了,它正在发挥作用。

答案 3 :(得分:0)

y轴工作正常,但是当我们尝试使用

xAxis: [{
        categories: {{chart_x}},
        crosshair: true
    }], 

图形消失。

xAxis: [{
        categories: '{{chart_x}}',
        crosshair: true
    }],

最初提供一些垃圾值

对我有用的是..

xAxis: [{
        categories: [
        '{{chart_x[0}}',
        '{{chart_x[1}}',
        '{{chart_x[2}}',
             ...
        '{{chart_x[11}}',
],
        crosshair: true
    }],

我不知道为什么,但这就是我所想的!