Highcharts .getJSON - chart not plotting

2019-08-04 08:58发布

问题:

Previous help here has got me to the current stage with my Highcharts project, however I am still hitting a major stumbling block. Highcharts chart is not loading data retrieved using .getJSON. I am at a loss as to why the html is not rendering the chart and load the data from data.php. All code below, does anyone have any ideas? @PawełFus

php

<?php

 $con = mysql_connect("ip_address","root","");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

$result = array();

$sql = mysql_query("SELECT unix_timestamp(DATETIMES), TEST FROM PR");

$result['name'] = 'TEST';
while($r = mysql_fetch_array($sql)) {
     $datetime = $r[0]*1000;
     $result['category'][] = $datetime;
     $result['data'][] = round($r[1],2) ;
}

$json = array();
array_push($json,$result);
print json_encode($json);


?>

php return

[{"name":"CCGT","category":[1387791900000,1387792200000,1387792500000,1387792800000],"data":[8389,8478,8761,8980,9050]}]

html

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Highcharts Example</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: {
                    renderTo: 'container',
                    type: 'line',
                    marginRight: 130,
                    marginBottom: 25
                },
                title: {
                    text: 'test',
                    x: -20 //center
                },
                subtitle: {
                    text: '',
                    x: -20
                },
                xAxis: {
                    categories: []
                },
                yAxis: {
                    title: {
                        text: 'test'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    formatter: function() {
                            return '<b>'+ this.series.name +'</b><br/>'+
                            this.x +': '+ this.y;
                    }
                },
                legend: {
                    layout: 'vertical',
                    align: 'right',
                    verticalAlign: 'top',
                    x: -10,
                    y: 100,
                    borderWidth: 0
                },
                series: []
            }

            $.getJSON("data.php", function(json) {
                options.xAxis.categories = json['category'];
                options.series[0].name = json['name'];
                options.series[0].data = json['data'];
                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: 400px; height: 400px; margin: 0 auto"></div>
    </body>
</html>

回答1:

Your series array (in options) has no object, so you cannot use series[0].name. Secondly your json is not used correct, but try to replace this lines:

options.xAxis.categories = json['category'];
options.series[0].name = json['name'];
options.series[0].data = json['data'];

with

options.xAxis.categories = json[0]['category'];
options.series[0] = {};
options.series[0].name = json[0]['name'];
options.series[0].data = json[0]['data'];