[Chartjs]-Chart.js not initiating

2👍

ChartJS does not work on div tag so you need to use canvas instead of div tag. Here you can find chart examples.

<canvas id="sample-chart" height="300" width="500">

<script>
var areaData = {
 labels: ["1", "2", "3", "4", "5"],
 datasets: [{
     data: [60, 63, 68, 53, 52],
     backgroundColor: [
       '#D6EEF3'
     ],
     borderColor: [
       '#1DBFD3'
     ]
   }
 ]
};
var areaOptions = {
 responsive: true,
 maintainAspectRatio: false
}
var sampleChartCanvas = $("#sample-chart").get(0).getContext("2d");
var sampleChart = new Chart(sampleChartCanvas, {
 type: 'line',
 data: areaData,
 options: areaOptions
});
</script>

1👍

Put it into document ready context

$(function () {
 var areaData = {
   labels: ["1", "2", "3", "4", "5"],
   datasets: [{
       data: [60, 63, 68, 53, 52],
       backgroundColor: [
         '#D6EEF3'
       ],
       borderColor: [
         '#1DBFD3'
       ]
     }
   ]
 };
 var areaOptions = {
   responsive: true,
   maintainAspectRatio: false
 }
 var sampleChartCanvas = $("#sample-chart").get(0).getContext("2d");
 var sampleChart = new Chart(sampleChartCanvas, {
   type: 'line',
   data: areaData,
   options: areaOptions
 });
})

0👍

There’s no canvas in your html.

Add it into the sample-chart div like:

<div id="sample-chart">
<canvas id="myChart"></canvas>
</div>

0👍

You either need to change the div to a canvas or have jQuery convert it from a div to a canvas using a replacer function.

var areaData = {
  labels: ["1", "2", "3", "4", "5"],
  datasets: [{
    data: [60, 63, 68, 53, 52],
    backgroundColor: [ '#D6EEF3' ],
    borderColor: [ '#1DBFD3' ]
  }]
};

var areaOptions = {
  responsive: true,
  maintainAspectRatio: false
}

var sampleChartCanvas = $("#sample-chart").get(0).getContext("2d");
var sampleChart = new Chart(sampleChartCanvas, {
  type: 'line',
  data: areaData,
  options: areaOptions
});
#sample-chart {
  height: 300px;
  width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

<!-- Needs to be a canvas, not a div -->
<canvas id="sample-chart"></canvas>

Replacer

(function($) {
  $.fn.applyAttributes = function(attributes) {
    var self = this;
    $.each(attributes, function() {
      self.attr(this.name, this.value);
    });
    return self;
  };
  $.fn.replaceTag = function(tagName) {
    this.each((i, e) => {
      this.replaceWith($('<' + tagName + '>').applyAttributes(this.prop('attributes')));
    });
  };
})(jQuery);

var areaData = {
  labels: ["1", "2", "3", "4", "5"],
  datasets: [{
    data: [60, 63, 68, 53, 52],
    backgroundColor: [ '#D6EEF3' ],
    borderColor: [ '#1DBFD3' ]
  }]
};

var areaOptions = {
  responsive: true,
  maintainAspectRatio: false
};

$("#sample-chart").replaceTag('canvas'); // Replace tag name with canvas

// Modified the DOM, we will need to re-query it.
var sampleChartCanvas = $("#sample-chart").get(0).getContext("2d");
var sampleChart = new Chart(sampleChartCanvas, {
  type: 'line',
  data: areaData,
  options: areaOptions
});
#sample-chart {
  height: 300px;
  width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>

<!-- Needs to be a canvas, not a div -->
<div id="sample-chart"></div>

Leave a comment