Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js line chart is not displaying

Tags:

chart.js

I am built a line chart with chart.js. However the chart is not showing up. Here is my HTML

    <canvas id="myChart" width"600" height:"600"></canvas>

Here is my javascript. I get no errors with this approach but nothing shows up.

var c = $('#myChart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("myChart").getContext("2d").Line(data);


var data = {
labels: ["January", "February", "March", "April", "May", "June", "July","August", "November", "December"],
datasets: [
    {
        label: "Sodium intake",
        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: [165, 159, 180, 181, 156, 155, 140]
    },
    {
        label: "Sugar intake",
        fillColor: "rgba(151,187,205,0.2)",
        strokeColor: "rgba(151,187,205,1)",
        pointColor: "rgba(151,187,205,1)",
        pointStrokeColor: "#fff",
        pointHighlightFill: "#fff",
        pointHighlightStroke: "rgba(151,187,205,1)",
        data: [128, 148, 140, 119, 186, 127, 190]
    }
]

};

The other approach I used was similar to the documentation. I instantiated a new chart. However, this approach returns undefined for my line Chart.

var ctx = document.getElementById("myChart").getContext("2d");
var myLineChart = new Chart(ctx).line(data,{
belzierCurve : false

});

like image 802
Winnemucca Avatar asked Feb 08 '15 16:02

Winnemucca


1 Answers

On your html:

<canvas id="myChart" width"600" height:"600"></canvas>

It should be:

<canvas id="myChart" width="600" height="600"></canvas>

Then, Replace your code

var c = $('#myChart');
var ct = c.get(0).getContext('2d');
var ctx = document.getElementById("myChart").getContext("2d").Line(data);

With this code

var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);

So in your js

var data = {
    //your data here
}
enter code here

and then add this line after your data

var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);

So your code would look like this:

var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July","August", "November", "December"],
  datasets: [
      {
          label: "Sodium intake",
          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: [165, 159, 180, 181, 156, 155, 140]
      },
      {
          label: "Sugar intake",
          fillColor: "rgba(151,187,205,0.2)",
          strokeColor: "rgba(151,187,205,1)",
          pointColor: "rgba(151,187,205,1)",
          pointStrokeColor: "#fff",
          pointHighlightFill: "#fff",
          pointHighlightStroke: "rgba(151,187,205,1)",
          data: [128, 148, 140, 119, 186, 127, 190]
      }
  ]
}

var ctx = new Chart(document.getElementById("myChart").getContext("2d")).Line(data);
like image 67
Nb12se Avatar answered Oct 10 '22 19:10

Nb12se