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.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
 });
potato skins

Update : See https://stackoverflow.com/a/45092928/360067 for a simpler and more robust solution using the "chart annotation" plugin .

You can extend linethe type to add support for line drawing


preview

enter image description here


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();
    }
  }
});

Then

var config = {
  type: 'line',
  data: {
    labels: ...
    datasets: [
        ...
    ],
    lineAtIndex: 2
  }
};

Fiddle - http://jsfiddle.net/mn8x6fso/

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 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 — 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 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 — 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 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

Display multiple dynamic vertical lines on a chart using Chart.js

Mark Ashdown I'm trying to create multiple vertical lines on a single chart that represent treatment dates. Each row has a different value (date) and possibly a different colour (depending on the treatment type) Currently I have multiple rows, but these are ha

Display multiple dynamic vertical lines on a chart using Chart.js

Mark Ashdown I'm trying to create multiple vertical lines on a single chart that represent treatment dates. Each row has a different value (date) and possibly a different colour (depending on the treatment type) Currently I have multiple rows, but these are ha

How to place static pointDots and make vertical lines in Chart.js

Worstein Azatian js community, I'm trying to make some customizations to Chart.js, but I'm running into some basic issues... In this photo you can see what I have achieved My question is how to set fixed points in the chart? Is there any way to do what I'm try

How to place static pointDots and make vertical lines in Chart.js

Worstein Azatian js community, I'm trying to make some customizations to Chart.js, but I'm running into some basic issues... In this photo you can see what I have achieved My question is how to set fixed points in the chart? Is there any way to do what I'm try

How to place static pointDots and make vertical lines in Chart.js

Worstein Azatian js community, I'm trying to make some customizations to Chart.js, but I'm running into some basic issues... In this photo you can see what I have achieved My question is how to set fixed points in the chart? Is there any way to do what I'm try

Scatter Chart with Vertical Lines - Lollipop Chart

green I want to build a scatter plot like this : http://www.cbioportal.org/public-portal/images/previews/tp53_mutations.png where each point has a circle + vertical line. The closest thing I've found below is a 3D scatter plot library(scatterplot3d), but I onl

Add vertical lines to the background of a Quantmod chart

behemoth How can I add vertical lines to a graph in quantmod that appears in the background ? Consider the following example: library(quantmod) symbol <- "AAPL" cache <- new.env() getSymbols(symbol, env=cache) chartSeries(cache$AAPL, subset="last 3 months") pl

Bar chart with vertical lines in each bar

Markaff Is there any way to draw the following chart with Chart.js? I've looked through the docs but haven't found anything similar... the closest is probably a multi-axis bar chart Thanks for any help! Markaff In the end, I followed the guide here to choose a

How to Put Vertical Lines on a Google Chart Scatter

Jorge Valenzuela How to draw vertical lines on a scatter plot? I need to place the red line vertically: Here is my current code: //making array with data var dataArray = []; dataArray.push(["", "", { role: 'annotation' }, "", ""]); proj

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

Add two vertical lines to a bar chart

username I'm trying to add two vertical lines to a bar chart I'm making, but I'm having trouble. example dataset data<-data.frame(area=rep(c("ES","OC","VB"),each=2,times=2),num=c(0,10,23,40,25,60,80,45,10,25,10,0), bin=rep(c(85,90),times=3)) Bar chart cod

Add two vertical lines to a bar chart

username I'm trying to add two vertical lines to a bar chart I'm making, but I'm having trouble. example dataset data<-data.frame(area=rep(c("ES","OC","VB"),each=2,times=2),num=c(0,10,23,40,25,60,80,45,10,25,10,0), bin=rep(c(85,90),times=3)) Bar chart cod

Add vertical lines to the background of a Quantmod chart

behemoth How can I add vertical lines to a graph in quantmod that appears in the background ? Consider the following example: library(quantmod) symbol <- "AAPL" cache <- new.env() getSymbols(symbol, env=cache) chartSeries(cache$AAPL, subset="last 3 months") pl

Add vertical lines to the background of a Quantmod chart

behemoth How can I add vertical lines to a graph in quantmod that appears in the background ? Consider the following example: library(quantmod) symbol <- "AAPL" cache <- new.env() getSymbols(symbol, env=cache) chartSeries(cache$AAPL, subset="last 3 months") pl

Add vertical lines to the background of a Quantmod chart

behemoth How can I add vertical lines to a graph in quantmod that appears in the background ? Consider the following example: library(quantmod) symbol <- "AAPL" cache <- new.env() getSymbols(symbol, env=cache) chartSeries(cache$AAPL, subset="last 3 months") pl