来自php json的highchart feed数据

时间:2014-12-10 18:25:24

标签: javascript php json graph highcharts

我正在尝试将json数据提供给高图,但图表显示为空白,这就是我需要的示例:http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/column-rotated-labels/

values.php生成json输出

[["10000164@example",4],["10000166@example",2],["10000173@example",1],["10000177@example",3]]

这里是index.html

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Bar Chart</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function() {
            var options = {
    chart: {
            type: 'column'
        },
        title: {
            text: 'World\'s largest cities per 2014'
        },
        subtitle: {
            text: 'Source: <a href="http://en.wikipedia.org/wiki/List_of_cities_proper_by_population">Wikipedia</a>'
        },
        xAxis: {
            type: 'category',
            labels: {
                rotation: -45,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif'
                }
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: 'Population (millions)'
            }
        },
        legend: {
            enabled: false
        },
        tooltip: {
            pointFormat: 'Population in 2008: <b>{point.y:.1f} millions</b>'
        },
        series: [{
            name: 'Population',
            data: [],  

            dataLabels: {
                enabled: true,
                rotation: -90,
                color: '#FFFFFF',
                align: 'right',
                x: 4,
                y: 10,
                style: {
                    fontSize: '13px',
                    fontFamily: 'Verdana, sans-serif',
                    textShadow: '0 0 3px black'
                }
            }
        }]
            $.getJSON("values.php", function(json) {
               options.series[0].data = json;
              chart = new Highcharts.Chart(options);
           });

        });
});  
        </script>
        <script src="http://code.highcharts.com/highcharts.js"></script>
        <script src="http://code.highcharts.com/modules/exporting.js"></script>
    </head>
    <body>
    <div id="container" style="min-width: 500px; height: 400px; margin: 0 auto"></div>    
   </body>
</html>

1 个答案:

答案 0 :(得分:2)

您可以在回调函数中构建图表,而不是先定义所有选项吗? 也许尝试这样的事情:

$.getJSON('values.php', function (json) {

        $('#container').highcharts({
            series: [{
              data: json
            ]},
            title: {
              text: 'Some Title'
            },
            subtitle: ...

我无法演示ajax请求,但是如果你将json直接放在数据选项中,那么图表会呈现如下: jsfiddle