score:14
You can use Chart.js plugins to do that. Plugins let you handle specific events such as beforeUpdate
or afterDraw
and are also easy to implement :
Chart.pluginService.register({
afterDraw: function(chart) {
// Code here will be triggered ... after the drawing
}
});
An easy way to do it is to simply draw a line like you would you on a simple canvas element, after everything is drawn in your chart, using the lineTo
method.
Here is a small example (and its related code) of how it would look like :
score:-1
With the answer from @tektiv, your yAxis
always starts at 0
.
This is a working example without the use of yAxe.min
, so
you can use it (for example, with beginAtZero:false
) and the yAxe
scales automatically with your data.
Line plugin:
var canvas = document.getElementById("barCanvas");
var ctx = canvas.getContext('2d');
Chart.pluginService.register({
afterDraw: function(chart) {
if (typeof chart.config.options.lineAt != 'undefined') {
var lineAt = chart.config.options.lineAt;
var ctxPlugin = chart.chart.ctx;
var xAxe = chart.scales[chart.config.options.scales.xAxes[0].id];
var yAxe = chart.scales[chart.config.options.scales.yAxes[0].id];
ctxPlugin.strokeStyle = "red";
ctxPlugin.beginPath();
lineAt = yAxe.getPixelForValue(lineAt);
ctxPlugin.moveTo(xAxe.left, lineAt);
ctxPlugin.lineTo(xAxe.right, lineAt);
ctxPlugin.stroke();
}
}
});
Chart:
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
lineAt: 14,
scales: {
yAxes: [{
ticks: {
beginAtZero:false
}
}]
},
}
});
Source: stackoverflow.com
Related Query
- How to draw Horizontal line on Bar Chart Chartjs
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- Draw stacked horizontal bar chart with Average line using ChartJS
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Chart.js - draw horizontal line in Bar Chart (type bar)
- ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- Chartjs 3 how to draw a horizontal line starting at a specific yAxis value
- How to order a horizontal bar chart - chartJS & Chart.HorizontalBar.js
- How to draw a horizontal line over bar columns in Chart.js that's started from X axis?
- How to add vertical line in bar chart using chartJs and ng2-charts?
- Draw horizontal line on chart in chart.js on v2
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- chartjs : how to set custom scale in bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- How to add second Y-axis for Bar and Line chart in Chart.js?
- Draw a horizontal and vertical line on mouse hover in chart js
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Line chart is showing under bar in combochart in chartjs
- How to dynamically set ChartJs line chart width based on dataset size?
- How do I make line charts overlay over bar charts in chartjs
- How to show data values in top of bar chart and line chart in chart.js 3
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- How to change Chart.js horizontal bar chart Width?
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Draw a horizontal bar chart from right to left
- Remove padding from chartJs horizontal bar chart
More Query from same tag
- Add multiple lines as Y axis title in chartJs
- (Angular Ionic) How can I stream my custom data with chartjs-plugin-streaming?
- How display straight corner in chartjs
- How can i launch a modal after clicking each bar in Bar chart in Chartjs and also how can i get its data?
- Angular2 + Chart.js and ng2-charts. Data binding not working as expected
- how to set color of label in chartjs?
- How to include Chart.js in my AngularJS project?
- Chart in Angular not displayed until windows resize
- Representing multiple subsections within a section of a doughnut chart Chart.js
- Format Chart.JS Data Element To Be Currency
- Does iFrame have any events suitable for when a JavaScript & HTML page are embedded in modal(Bootstrap)
- Chart.js with JSON data error
- Chart.js How can I embed additional values to each data point in radar chart
- chartjs data isn't passing to django webpage
- How to create stacked bar chart using react-chartjs-2?
- ChartJs (ver: 2.8.0): Custom tooltip does not hide if clicked (or mouse pointer is moved) outside the chart canvas area
- Chart.js in flex element overflows instead of shrinking
- How to edit style of negative x grid lines on Chart.js?
- How to draw a linear regression line in Chart.js
- tc-angular-chartjs problems while assigning value to chart-data
- How do I selecting a date range (like onClick but drag/select)
- Chart.js scale x values
- How can I style scale numbers in a Polar Area chart (chart.js)
- Failed to execute 'createLinearGradient' on 'CanvasRenderingContext2D': The provided double value is non-finite
- Click event on stacked bar chart - ChartJs
- rotated partial doughnut chart with a point marker in chart.js agularJS
- Chartjs + Vue.js - Cannot read property '_meta' of undefined
- ChartJS set default options only to specific Canvas, not globally
- ChartJS tooltip scrollbar to prevent data overflow?
- Chart.js bar chart change the label position X-axis