score:34
UPDATE: See https://stackoverflow.com/a/45092928/360067 for a simpler and more robust solution using the Chart Annotations plugin.
You can extend the line
type to add support for drawing a line
Preview
Script
var originalLineDraw = Chart.controllers.line.prototype.draw;
Chart.helpers.extend(Chart.controllers.line.prototype, {
draw: function() {
originalLineDraw.apply(this, arguments);
var chart = this.chart;
var ctx = chart.chart.ctx;
var index = chart.config.data.lineAtIndex;
if (index) {
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];
ctx.save();
ctx.beginPath();
ctx.moveTo(xaxis.getPixelForValue(undefined, index), yaxis.top);
ctx.strokeStyle = '#ff0000';
ctx.lineTo(xaxis.getPixelForValue(undefined, index), yaxis.bottom);
ctx.stroke();
ctx.restore();
}
}
});
and then
var config = {
type: 'line',
data: {
labels: ...
datasets: [
...
],
lineAtIndex: 2
}
};
Fiddle - http://jsfiddle.net/mn8x6fso/
score:1
For the ones looking for horizontal lines, here is what i got so far:
ctx.save();
ctx.beginPath();
ctx.moveTo(xaxis.left, limits[i].value);
ctx.strokeStyle = limits[i].color;
ctx.lineTo(xaxis.right, limits[i].value);
ctx.stroke();
ctx.restore();
score:18
For v2.0, the best way is to use the Chart Annotations plugin (https://github.com/chartjs/chartjs-plugin-annotation)
Fiddle - https://codepen.io/anon/pen/ZywgKr
Script
var ctx = document.getElementById("canvas").getContext("2d");
new Chart(ctx, {
type: "line",
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]
}
]
},
options: {
annotation: {
annotations: [
{
type: "line",
mode: "vertical",
scaleID: "x-axis-0",
value: "MAR",
borderColor: "red",
label: {
content: "TODAY",
enabled: true,
position: "top"
}
}
]
}
}
}
);
Cross posted from https://github.com/chartjs/Chart.js/issues/4495#issuecomment-315238365
Source: stackoverflow.com
Related Query
- Multiple dynamic vertical lines on a chart with Chart.js
- Draw vertical and horizonal lines on the radar chart (like x-y axes)
- Bar chart with vertical lines in each bar
- Charts.js : How to remove the Vertical Black lines in Bar Chart
- Remove the vertical line in the chart js line chart
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Moving vertical line when hovering over the chart using chart.js
- How can I make two of my lines in Chart JS thicker
- Chart.js 2.0 - vertical lines
- Vertical stacked bar chart with chart.js
- Chart JS Fill Between two lines
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Draw a horizontal and vertical line on mouse hover in chart js
- How to display data labels outside in pie chart with lines in ionic
- ChartJS: Draw vertical line at data point on chart on mouseover
- ChartJS Radar Chart radar lines color?
- Chart js vertical line z-index
- Find intersection between the chart lines in chartjs
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Angular-chart / line chart with multiple horizontal lines (margins)
- ChartJS / Chartjs-plugin-annotation How to draw multiple vertical lines using an array?
- ChartJS v2 - Keep tooltip open when user click on a point of a multiple lines chart
- How to Remove axis Lines from chart in chart js
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- change space between horizontal (grid) lines for line chart
- Vertical Bar Chart not displaying Correctly
- display vertical axis label in line chart using chart.js
- How can I keep the vertical lines under the horizontal ruler line is chartjs?
- How to set vertical lines for new day on x-axis in ChartJS v3.x
More Query from same tag
- Chart.js - barchart - plugin labels
- Chart.js - each dataset value = different axis
- How to start Y Axis Line at 0 from right for negative values in chart js 2?
- How to show text as a series on a chart in Javascript?
- Chart.js: A value is empty, but the line must not be broken
- How to draw 2d line time plot with chart.js
- chartjs undefined length when using 2 datasets
- create different labels for different data chart js
- Uncaught TypeError: [Array].filter is not a function
- Meteor, ChartsJS and MongoDB
- ChartJS Zoom Granularity
- Can't make chart js responsive
- TypeError: Cannot read property 'currentStyle' of null in chart.js
- Looping through afterDraw in ChartJS
- Unable to display HH:mm:ss data in Chart.js
- Using Datalabels and ChartJS with Chartjs-node-canvas
- chart.js chart failing to render
- How do I correctly include Moment.js and Chart.js with RequireJS when I want to create charts with time scales?
- Chartjs break line for axes tick labels text
- Copy div content to dynamically create a modal window:possible?
- ChartJS 3.x How to place tick label inside graph?
- Adjust appearance of tooltip in lien chart rendered by Chart.js
- Chart.js Box Annotations Fill Entire Chart
- Chart JS tooltip appears differently when set from script instead of html
- Chart.js data background color is overwriting point background color
- Custom label from using separate array in Chart JS
- ChartJS: Can interior of polar area chart be hollow?
- Style individual points from one dataset in Chart.js
- How to remove transparency from images downloaded in Chart.js
- How to set minimal value for bar chart in Chart.js? (ver. 2.0.2)