1

I have some troubles to create a graph using chart.js and json data generated by me (php).

I wrote this code :

test.php

<script type="text/javascript" src="js/Chart.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<canvas id="myChart" width="740" height="200"></canvas>


<script type="text/javascript">

$.getJSON("json.php", function (result) {
    var labels = [], data = [];

    for (var i = 0; i < result.lenght ; i++){
            labels.push(result[i].time);
            data.push(result[i].mms);
            console.log("result");
    }

    var ctx = document.getElementById("myChart").getContext("2d");
    var myChart = new Chart(ctx, {
            type: 'line',
            data: {
                    labels: labels,
            datasets: [
            {
                    label: "My First dataset",
                    fillColor: "rgba(220,220,220,0.2)",
                    strokeColor: "rgba(220,220,220,1)",
                    pointColor: "rgba(220,220,220,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: data
            }
            ]}
});
});
</script>

json.php

[
{
"time": "2016-09-01 00:00:07",
"mms": 1430
},
{
"time": "2016-09-01 00:05:08",
"mms": 2237
}
]

Json.php is file generated since a mysql query. If I replace labels by

labels: ["January", "February", "March", "April", "May", "June", "July"],

and data by

data: [65, 59, 80, 81, 56, 55, 40]

It works !

Can you help me please. I'm sure that I miss a little thing. Thanks

1 Answer 1

2

for (var i = 0; i < result.lenght ; i++){

I think you mistake 'length' the word...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.