Chart.js — draw arbitrary vertical lines


Frederick:

How to draw a vertical line at a specific point on the x-axis using Chart.js?

In particular, I want to draw a line on the LineChart to indicate the current day. Here is a mockup of the graph : http://i.stack.imgur.com/VQDWR.png

enter image description here

Potato Skins:

UPDATE - This answer is for Chart.js 1.x, if you are looking for a 2.x answer please check the comments and other answers.

You can extend the line chart and include the logic for drawing the line in the plot function.


preview

enter image description here


HTML

<div>
    <canvas id="LineWithLine" width="600" height="400"></canvas>
</div>

script

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",
    draw: function () {
        Chart.types.Line.prototype.draw.apply(this, arguments);

        var point = this.datasets[0].points[this.options.lineAtIndex]
        var scale = this.scale

        // draw line
        this.chart.ctx.beginPath();
        this.chart.ctx.moveTo(point.x, scale.startPoint + 24);
        this.chart.ctx.strokeStyle = '#ff0000';
        this.chart.ctx.lineTo(point.x, scale.endPoint);
        this.chart.ctx.stroke();

        // write TODAY
        this.chart.ctx.textAlign = 'center';
        this.chart.ctx.fillText("TODAY", point.x, scale.startPoint + 12);
    }
});

new Chart(ctx).LineWithLine(data, {
    datasetFill : false,
    lineAtIndex: 2
});

The option property lineAtIndex controls at which point the line is drawn.

Fiddle - http://jsfiddle.net/dbyze2ga/14/

Related


Chart.js — draw arbitrary vertical lines

Frederick: How to draw a vertical line at a specific point on the x-axis using Chart.js? In particular, I want to draw a line on the LineChart to indicate the current day. Here is a mockup of the graph : http://i.stack.imgur.com/VQDWR.png Potato Skins: UPDATE

Chart.js — draw arbitrary vertical lines

Frederick: How to draw a vertical line at a specific point on the x-axis using Chart.js? In particular, I want to draw a line on the LineChart to indicate the current day. Here is a mockup of the graph : http://i.stack.imgur.com/VQDWR.png Potato Skins: UPDATE

How to draw vertical lines in bar chart?

user I want to draw a vertical line at a specific position on the x-axis, but the x-position of the lines is not correct. How can I fix this? x <- c(0,0,0,4,5,6) barplot(x, names.arg=1:length(x)) abline(v=1:length(x), col="red") abline(v=c(5.5), col="blue") d

How to draw vertical lines in bar chart?

user I want to draw a vertical line at a specific position on the x-axis, but the x-position of the lines is not correct. How can I fix this? x <- c(0,0,0,4,5,6) barplot(x, names.arg=1:length(x)) abline(v=1:length(x), col="red") abline(v=c(5.5), col="blue") d

How to draw vertical lines in bar chart?

user I want to draw a vertical line at a specific position on the x-axis, but the x-position of the lines is not correct. How can I fix this? x <- c(0,0,0,4,5,6) barplot(x, names.arg=1:length(x)) abline(v=1:length(x), col="red") abline(v=c(5.5), col="blue") d

How to draw vertical lines in bar chart?

user I want to draw a vertical line at a specific position on the x-axis, but the x-position of the lines is not correct. How can I fix this? x <- c(0,0,0,4,5,6) barplot(x, names.arg=1:length(x)) abline(v=1:length(x), col="red") abline(v=c(5.5), col="blue") d

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Chart.js 2.0 - Vertical Lines

Wannen Can anyone tell me how to extend Chart.js v2.0. I need vertical lines in a line chart and want to achieve something similar to http://jsfiddle.net/dbyze2ga/ . Chart.types.Line.extend({ name: "LineWithLine", draw: function () { Chart.types.Line.proto

Google Charts: How to draw vertical lines for a bar chart

Lewis I'm trying to add a vertical line so that I can have a visual distinction when an element goes out of value. thanks <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("visualization

Chart.js - draw horizontal lines

Christine: I want to draw a horizontal line in a chart using Chart.js . But I can't. I've read this question - Chart.js - Drawing Arbitrary Vertical Lines - but I can't convert the code for drawing horizontal lines that are not vertical . Hope you can help me

Chart.js - draw horizontal lines

Christine: I want to draw a horizontal line in a chart using Chart.js . But I can't. I've read this question - Chart.js - Drawing Arbitrary Vertical Lines - but I can't convert the code for drawing horizontal lines that are not vertical . Hope you can help me

Chart.js - draw horizontal lines

Christine: I want to draw a horizontal line in a chart using Chart.js . But I can't. I've read this question - Chart.js - Drawing Arbitrary Vertical Lines - but I can't convert the code for drawing horizontal lines that are not vertical . Hope you can help me

Chart.js - draw horizontal lines

Christine: I want to draw a horizontal line in a chart using Chart.js . But I can't. I've read this question - Chart.js - Drawing Arbitrary Vertical Lines - but I can't convert the code for drawing horizontal lines that are not vertical . Hope you can help me

Draw vertical lines with matplotlib

Philip Jewell I'm trying to draw a vertical line with Matpotlib, I'm using axvlineit, but it doesn't work. import sys import matplotlib matplotlib.use('Qt4Agg') from ui_courbe import * from PyQt4 import QtGui from matplotlib import pyplot as plt class Wind

Draw vertical lines with matplotlib

Philip Jewell I'm trying to draw a vertical line with Matpotlib, I'm using axvlineit, but it doesn't work. import sys import matplotlib matplotlib.use('Qt4Agg') from ui_courbe import * from PyQt4 import QtGui from matplotlib import pyplot as plt class Wind

Draw vertical lines with matplotlib

Philip Jewell I'm trying to draw a vertical line with Matpotlib, I'm using axvlineit, but it doesn't work. import sys import matplotlib matplotlib.use('Qt4Agg') from ui_courbe import * from PyQt4 import QtGui from matplotlib import pyplot as plt class Wind

Python - draw vertical lines

John Newsom I have some data curves plotted using matplotlib. The fractional value x range of the data consists entirely of NaN values, so my curve starts abruptly from some value of x >> 0 (which is not necessarily the same value for the different datasets I