简体   繁体   中英

Highcharts json php multiple series

I have a simple question but for some reason can't find the solution. As described here: highcharts documentation , I made the following script:

<script type="text/javascript">
$(document).ready(function() {

    var options = {
            chart: {
                renderTo: 'container',
                type: 'column',
            },
            title: {
                text: 'Dagelijks waterverbruik'
            },
            subtitle: {
                text: 'Waterverbruik in liters'
            },
            xAxis: {
                categories: [
                    'Zondag',
                    'Maanag',
                    'Dinsdag',
                    'Woensdag',
                    'Donderdag',
                    'Vrijdag',
                    'Zaterdag'
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Waterverbruik (Liter)'
                }
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: [{}]
    };

    $.getJSON('testdata.php', function(data) {
        options.series[0].data = data;
        var chart = new Highcharts.Chart(options);
    });

});
</script>

my testdata.php is as follows:

 <?php
header('Content-Type: application/json');
$data[] = array('Zondag',11);
$data[] = array('Maandag',10);
$data[] = array('Dinsdag',9);
$data[] = array('Woensdag',8);
$data[] = array('Donderdag',12);
$data[] = array('Vrijdag',2);
$data[] = array('Zaterdag',18);
$serie1[] = array('name' => 'serie 1', 'data' => $data);
$serie1[] = array('name' => 'serie 2', 'data' => $data);
echo json_encode($serie1);
?>

For some reason the charts don't render. What am I doing wrong? One series is working this way, but multiple series don't.

As you can see I would expect two bars with the same value. The output of testdata.php is:

[{"name":"serie 1","data":[["Zondag",11],["Maandag",10],["Dinsdag",9],["Woensdag",8],["Donderdag",12],["Vrijdag",2],["Zaterdag",18]]},{"name":"serie 2","data":[["Zondag",11],["Maandag",10],["Dinsdag",9],["Woensdag",8],["Donderdag",12],["Vrijdag",2],["Zaterdag",18]]}]

You make the array like this and do not use categories in the $data array because you are using static categories in chart.following is a hint

$data = array(11,10,9,8,12,81);
$serie1[] = array('name' => 'serie 1', 'data' => $data);
$serie1[] = array('name' => 'serie 2', 'data' => $data);
echo json_encode($serie1);

In this part:

$.getJSON('testdata.php', function(data) {
    options.series[0].data = data;
    var chart = new Highcharts.Chart(options);
});

Change it to this

$.getJSON('testdata.php', function(data) {
    options.series = data;
    var chart = new Highcharts.Chart(options);
});

You current code can only accomodate one series since you specified an index to the series ie [0].

在JSON中,您需要设置JSON_NUMERIC_CHECK标志,因为在您的示例中,似乎数据上没有numebr值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM