在HighChart Flask Python中动态绑定数据

时间:2018-07-17 00:47:23

标签: python flask highcharts

我试图通过使用Flask / Python从后端传递数据来将数据加载到高位图表中。我如何做得更好,而不是像现在这样传递每个值。这使得更改代码非常困难。我尝试使用for-in循环,但没有用,任何建议将不胜感激。先感谢您。

var barChartData = [
                        {% for item in bar_values %}
                            "{{item|safe}}",
                        {% endfor %}
                       ];

var barChartLabels = [
                        {% for item in bar_labels %}
                                "{{item|safe}}",
                        {% endfor %}
                    ];


    Highcharts.chart('statInfo', {
    chart: {
        type: 'column',
    },
    title: {
        text: 'Statistical Class'
    },
    xAxis: {
        type: 'category',
        labels: {
            rotation: -45,
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    },
    yAxis: {
        min: 0,
        title: {
            text: 'No. of People'
        }
    },
    legend: {
        enabled: false
    },
    tooltip: {
        pointFormat: 'Count: <b>{point.y:.1f} </b>'
    },
    series: [{
        name: 'Population',
        data: [
            [barChartLabels[0], parseInt(barChartData[0])],
            [barChartLabels[1], parseInt(barChartData[1])],
            [barChartLabels[2], parseInt(barChartData[2])],
            [barChartLabels[3], parseInt(barChartData[3])],
            [barChartLabels[4], parseInt(barChartData[4])],
            [barChartLabels[5], parseInt(barChartData[5])],
            [barChartLabels[6], parseInt(barChartData[6])],
            [barChartLabels[7], parseInt(barChartData[7])],
            [barChartLabels[8], parseInt(barChartData[8])],
            [barChartLabels[9], parseInt(barChartData[9])]
        ],
        dataLabels: {
            enabled: true,
            rotation: -90,
            color: '#FFFFFF',
            align: 'right',
            format: '{point.y:.1f}', // one decimal
            y: 10, // 10 pixels down from the top
            style: {
                fontSize: '13px',
                fontFamily: 'Verdana, sans-serif'
            }
        }
    }] });

0 个答案:

没有答案