如何在高图中传递数组?

时间:2014-04-07 05:41:54

标签: javascript php highcharts

我刚刚开始使用highcharts,在这里我被卡住了 -

我有两个名为 - js_platform, js_totalTest的数组,我从数据库中获取它。

生成图表的代码是 -

<!DOCTYPE html>
    <body>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
    <script src="http://code.highcharts.com/modules/exporting.js"> </script>
    <script>
$(function () {
    js_platform = <?php echo json_encode($platform); ?>;
    js_totalTest = <?php echo json_encode($totalTest); ?>;
        $('#container').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: 'Overall avalibility of testcases on each platform'
            },
            xAxis: {
                categories: js_platform
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Total fruit consumption'
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            legend: {
                align: 'right',
                x: -70,
                verticalAlign: 'top',
                y: 20,
                floating: true,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                borderColor: '#CCC',
                borderWidth: 1,
                shadow: false
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +'</b><br/>'+
                        this.series.name +': '+ this.y +'<br/>'+
                        'Total: '+ this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                        style: {
                            textShadow: '0 0 3px black, 0 0 3px black'
                        }
                    }
                }
            },
            series: [{
                name: 'John',
                data: [5, 3, 4, 7, 2]
            }, {
                name: 'Jane',
                data: [2, 2, 3, 2, 1]
            }, {
                name: 'Joe',
                data: [3, 4, 4, 2, 5]
            }]
        });
    });
    </script>

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

</body>
</html>

现在我想发布 js_totalTest 的数组值。

如果我执行**series: [{data: [js_totalTest]}]**之类的操作,则不会显示任何内容。

我怎么能这样做? 请指导。

2 个答案:

答案 0 :(得分:2)

做类似的事情 -

series: 
    [{
        data:  [<?php echo join($totalTest, ',') ?>]
    }]

请参阅链接(http://www.highcharts.com/docs/working-with-data/preprocessing-data-from-a-database

答案 1 :(得分:1)

尝试这样的方式。 您的数组js_totalTest包含数据名称

循环数组数据

var arr = [];
for(var i=0;i<js_totalTest.length;i++)
{
    arr[i] = {};
    arr[i]['name'] = '';  //Here push your series name field in your example "John, Jane etc"

    for(var j=0;j<s.length;j++)
    {
            arr_data.push(parseInt(s[j]));
    }
    arr[i]['data'] = parseInt('') ;  // Here push your series data (Eg: 5 ,3 , etc.)
}

串联arr

series: arr