ChartJS – Different color per data point

In updating to version 2.2.2 of ChartJS, I found that the accepted answer no longer works. The datasets will take an array holding styling information for the properties. In this case: var pointBackgroundColors = []; var myChart = new Chart($(‘#myChart’).get(0).getContext(‘2d’), { type: ‘line’, data: { datasets: [ { data: dataPoints, pointBackgroundColor: pointBackgroundColors } ] } … Read more

Destroy chart.js bar graph to redraw other graph in same

The correct method to use, in order to be able to draw another chart on the same canvas, is .destroy(). You must call it on the previously created chart object. You may also use the same variable for both charts. var grapharea = document.getElementById(“barChart”).getContext(“2d”); var myChart = new Chart(grapharea, { type: ‘bar’, data: barData, options: … Read more

Click events on Pie Charts in Chart.js

Update: As @Soham Shetty comments, getSegmentsAtEvent(event) only works for 1.x and for 2.x getElementsAtEvent should be used. .getElementsAtEvent(e) Looks for the element under the event point, then returns all elements at the same data index. This is used internally for ‘label’ mode highlighting. Calling getElementsAtEvent(event) on your Chart instance passing an argument of an event, … Read more

How to set max and min value for Y axis

For chart.js V2 (beta), use: var options = { scales: { yAxes: [{ display: true, ticks: { suggestedMin: 0, // minimum will be 0, unless there is a lower value. // OR // beginAtZero: true // minimum value will be 0. } }] } }; See chart.js documentation on linear axes configuration for more details.

How to display data values on Chart.js

There is an official plugin for Chart.js 2.7.0+ to do this: Datalabels Otherwise, you can loop through the points / bars onAnimationComplete and display the values Preview HTML <canvas id=”myChart1″ height=”300″ width=”500″></canvas> <canvas id=”myChart2″ height=”300″ width=”500″></canvas> Script var chartData = { labels: [“January”, “February”, “March”, “April”, “May”, “June”], datasets: [ { fillColor: “#79D1CF”, strokeColor: “#79D1CF”, … Read more