score:9

Accepted answer

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);

score:0

the reason may be that you are using the latest chart.min.js but using the old code. for correct reference, follow the chartjs.org documentation.

the code configuration structure has changed in the latest release. (i guess from 2.3 onwards)

so, instead of

var countries= document.getelementbyid("countries").getcontext("2d");
var chart = new chart(countries).pie(piedata,pieoptions);

we are structuring like:

var countries= document.getelementbyid("countries").getcontext("2d");
var chart = new chart(countries,{
  type: 'pie',
  data: 
  {
    labels: ['india', 'germany', 'netherland'],
    datasets: 
        [{
        backgroundcolor: '#337ab7',
        data: ['100', '99' ,'98'],
        borderwidth:1,
        }]
  },
  options:
  {
      responsive: true,
      maintainaspectratio:false,
      legend: {
        display: false,
        position: 'top'
      }
  }
});

or

var countries= document.getelementbyid("countries").getcontext("2d");
var piedata = 
{
    labels: ['india', 'germany', 'netherland'],
    datasets: [{
      backgroundcolor: '#337ab7',
      data: ['100', '99' ,'98'],
      borderwidth:1,
      }]
};
var pieoptions = 
{
    responsive: true,
    maintainaspectratio:false,
    legend: {
      display: false,
      position: 'top'
    }
};

var chart = new chart(countries,{    
type: 'pie',
data: piedata,
options: pieoptions
});

score:2

ok, try this :

for a start correct your html :

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

then try this for your javascript :

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 chartdisplay = new chart(document.getelementbyid("mychart").getcontext("2d")).line(data);

and that should do ok. see a jsfiddle of it.


Related Query

More Query from same tag