Search code examples
javascriptphpmysqljsoncanvasjs

canvasJS : the chart isnt loaded


it's actually my first time to use the canvajs but it doenst really work :( could someone please check my code

data.php

<?php 

header('Content-Type: application/json');

include './traitement.php';

$data =array();

$data=getListTemp();

$point=array();

$data_points=array();

foreach($data as $value){ foreach($value as $l){

        $s= explode( 'T', $l->date) ;

        $t=explode('-',$s[0]);

        $f=explode(':',$s[1]);

        $io=explode('+',$f[2]);

        $o=mktime($f[0],$f[1],$io[1],$t[1],$t[2],$t[0]);

        $i= date ("D M Y H:i:s U", $o);

        $point = array("x" => $i, "y" => $l->valeur);

        array_push($data_points, $point);        
}}

echo json_encode($data_points, JSON_NUMERIC_CHECK);

?>

with the result:

[{"x":"Sat Jul 2017 23:20:01","y":30},{"x":"Sat Jul 2017 23:10:01","y":30},{"x":"Sat Jul 2017 23:00:01","y":30},{"x":"Sat Jul 2017 22:50:01","y":25},{"x":"Sat Jul 2017 22:40:01","y":30},{"x":"Sat Jul 2017 22:30:01","y":25},{"x":"Sat Jul 2017 22:20:01","y":30},{"x":"Sat Jul 2017 22:10:01","y":40},{"x":"Sat Jul 2017 22:00:01","y":23}]

curve.html

<!DOCTYPE html>
<html>


<head>
    <title></title>

    <script src="canvasjs.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-latest.js" > </script>

    <script type="text/javascript">

$(document).ready(function () {
$.getJSON("data.php", function (result) {

 var dps=[]; 
 dps = result;

     var chart = new CanvasJS.Chart("chartContainer",{
        title :{
            text: "Data "
        },
        axisX: {                        
            title: "time",

        },
        axisY: {                        
             title: "temperature",

        },
        data: [{
            type:"spline",

            dataPoints : dps
        }]
      });

     chart.render();    


});

});

    </script>
</head>
<body>

    <div id="chartContainer" style="width: 800px; height: 380px;"></div>

</body>
</html>

And thats what i have as result !! enter image description here


Solution

  • Replace x with label. As per documentation and things i tried x should be a number only then it will work and in your case it's string 'Sat jul ..' so mark x as label

    (below is copy paste of my above comment) In-place of 'x' use 'label' in response , I read the documentation and tried this jsfiddle.net/p7w58naq with your response