phpjsonajaxhighchartsseries

HighCharts Data with JavaScript PHP JSON


I'm traying to create an chart by points by points. Not live or someting special. It might be a basic thing but during 2 days I can't find or understand a solution.

PHP Code

for ($i = 0;$i < $numResults; $i++)
        {
            $row = $result->fetch_assoc();

            array_push($returnArray,array($row['powerWeek'] => $row['powerPower'] ));

        }
        echo json_encode($returnArray);

PHP Result: [{"1":"51"},{"2":"52"},{"3":"52"}]

JavaScipt Code:

 $.getJSON('getPower.php',function(getJSONText)
    {   
        var series = {};    
        $.each(getJSONText, function(key, value) {  
            series.data = value;
            option.series.push(series);

        });
        var chart = new Highcharts.Chart(option); 
}); 

However graph not been drawn. Thanks for all help.


Solution

  • I solved my problem. In php code i add intval fonction while building the array, and i changed it to multi-dimensiyon array:

    if  ($numResults != 0)
        {
            for ($i = 0;$i < $numResults; $i++)
            {
                $row = $result->fetch_assoc();
    
                array_push($returnArray,array(intval($row['powerWeek']) , intval($row['powerPower']) ));
    
            }
            echo json_encode($returnArray); 
        }
    }
    

    And I edit my JavaScript code to this :

    function requestData(){
        $.getJSON('getPower.php',function(getJSONText)
            {   
             chart.series[0].setData(getJSONText, true);
            });
    }