简体   繁体   中英

Highchart display of more than 1 series is not showing

I'm using JSON to fetch data from the database but when I try to display data of 2 series to highcharts, the result of the graph is not showing correctly.

fetchdata.php

while($row = sqlsrv_fetch_array($weekly2)) {
    $BF1[] = $row['VoltageA'];
    $BF2[] = $row['VoltageB'];
    $wv3[] = $row['VoltageC'];

    $wi4[] = $row['IA'];
    $wi5[] = $row['IB'];
    $wi6[] = $row['IC'];

}

echo json_encode(array($BF1, $BF2));

The data I get from json_encode:

[[0.80299997329712,0.80599999427795,0.80599999427795,0.80800002813339,0.80800002813339,0.80699998140335,0.80099999904633,0.80099999904633,0.78700000047684,0.77799999713898],[0.80599999427795,0.80900001525879,0.80800002813339,0.81000000238419,0.80900001525879,0.81000000238419,0.80299997329712,0.80299997329712,0.78899997472763,0.78299999237061]]

Script

$.when(
    $.getJSON('fetchdata.php')).then(function(BF1, BF2) {  
        Highcharts.chart('container', {
            chart: {
                type: 'line'
            },
            title: {
                text: 'Daily Voltage'
            },
            subtitle: {
                text: ''
            },
            xAxis: {
                categories: ['12:00 AM', '1:00 AM', '2:00 AM', '3:00 AM', '4:00 AM', '5:00 AM', '6:00 AM', '7:00 AM', '8:00 AM', '9:00 AM', '10:00 AM', '11:00 AM','12:00 PM', '1:00 PM', '2:00 PM', '3:00 PM', '4:00 PM', '5:00 PM', '6:00 PM', '7:00 PM', '8:00 PM', '9:00 PM', '10:00 PM', '11:00 PM']
            },
            yAxis: {
                title: {
                    text: 'FEEDER 1'
                }
            },
            plotOptions: {
                line: {
                    dataLabels: {
                        enabled: true
                    },
                    enableMouseTracking: true
                }
            },
            series: [{
                name: 'Voltage A',
                data: BF1
            },
            {
                name:'Voltage B',
                data: BF2
            }]
        });
    });

Result displayed

The problem occurs because of wrong usage of your $.getJSON() call. You've passed the two arguments to function defined in .then() , but should be just one, because you've received the array with two arrays, eg:

$.getJSON('fetchdata.php')).then(function(data) {

...

}

Then you'll be able to refer to data[0] and data[1] in your series definition:

series: [{
      name: 'Voltage A',
      data: data[0]
    },
    {
      name: 'Voltage B',
      data: data[1]
    }
  ]

Live example: https://jsfiddle.net/abx6cepw/

Kind regards!

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