Moving vertical line when hovering over the chart using chart.js

Solution for ChartJS 2.6.0 ꜱᴄʀɪᴘᴛ (ᴇxᴛᴇɴᴅɪɴɢ ʟɪɴᴇ ᴄʜᴀʀᴛ) Chart.defaults.LineWithLine = Chart.defaults.line; Chart.controllers.LineWithLine = Chart.controllers.line.extend({ draw: function(ease) { Chart.controllers.line.prototype.draw.call(this, ease); if (this.chart.tooltip._active && this.chart.tooltip._active.length) { var activePoint = this.chart.tooltip._active[0], ctx = this.chart.ctx, x = activePoint.tooltipPosition().x, topY = this.chart.legend.bottom, bottomY = this.chart.chartArea.bottom; // draw line ctx.save(); ctx.beginPath(); ctx.moveTo(x, topY); ctx.lineTo(x, bottomY); ctx.lineWidth = 2; ctx.strokeStyle=”#07C”; ctx.stroke(); ctx.restore(); … Read more

ChartJS: datalabels: show percentage value in Pie piece

Updated fiddle with 2 decimal precision. You were not computing the sum, instead storing the current value in sum only for every value. Here is the working fiddle : https://jsfiddle.net/a1Lvn4eb/55/ var data = [{ data: [50, 55, 60, 33], labels: [“India”, “China”, “US”, “Canada”], backgroundColor: [ “#4b77a9”, “#5f255f”, “#d21243”, “#B27200” ], borderColor: “#fff” }]; var … Read more

Different color for each bar in a bar chart; ChartJS

As of v2, you can simply specify an array of values to correspond to a color for each bar via the backgroundColor property: datasets: [{ label: “My First dataset”, data: [20, 59, 80, 81, 56, 55, 40], backgroundColor: [“red”, “blue”, “green”, “blue”, “red”, “blue”], }], This is also possible for the borderColor, hoverBackgroundColor, hoverBorderColor. From … Read more

How to put rounded corners on a Chart.js Bar chart

As pointed out in https://stackoverflow.com/a/68778432/360067, this is natively implemented in v3. Original Answer Here is how you extend Chart.js to draw a bar chart with rounded corners. Chart.types.Bar.extend({ name: “BarAlt”, initialize: function (data) { Chart.types.Bar.prototype.initialize.apply(this, arguments); if (this.options.curvature !== undefined && this.options.curvature <= 1) { var rectangleDraw = this.datasets[0].bars[0].draw; var self = this; var radius … Read more

How can I create a horizontal scrolling Chart.js line chart with a locked y axis?

Scrollable Chart You’re pretty much on the right track. If you add another wrapper and the y axis you are done. Preview CSS .chartWrapper { position: relative; } .chartWrapper > canvas { position: absolute; left: 0; top: 0; pointer-events:none; } .chartAreaWrapper { width: 600px; overflow-x: scroll; } HTML <div class=”chartWrapper”> <div class=”chartAreaWrapper”> <canvas id=”myChart” height=”300″ … Read more

Chart.js – draw horizontal line

Here is the JavaScript code to draw a horizontal line. var data = { labels: [“JAN”, “FEB”, “MAR”, “APR”, “MAY”, “JUN”, “JUL”, “AUG”, “SEP”, “OCT”, “NOV”, “DEC”], datasets: [{ data: [12, 3, 2, 1, 8, 8, 2, 2, 3, 5, 7, 1] }] }; var ctx = document.getElementById(“LineWithLine”).getContext(“2d”); Chart.types.Line.extend({ name: “LineWithLine”, initialize: function () { … Read more