0

I need to move vertical line on HTML canvas using Chart.js.

I am using this:

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

http://jsfiddle.net/dbyze2ga/377/ but what I need is to move the line to another index when button on the web page is clicked. I read about html canvas and found that it is "impossible" to remove line but then I found that: https://jsfiddle.net/ombaww9t/2/ and the line is moving.

So what I need is probably combination of those two examples.

Thanks for response.

Nivelety
  • 73
  • 1
  • 1
  • 8

1 Answers1

0

I solve it with plugin.

var verticalLinePlugin = {
afterDraw: function(chartInstance) 
{       
    var index = weatherMainChart.config.options.verticalLine[0].x;

    if (index) 
    {
            if (chartInstance.options.verticalLine) 
            {
                var canvas = chartInstance.chart;
                var ctx = canvas.ctx;
                var xaxis = chartInstance.scales['x-axis-0'];
                var yaxis = chartInstance.scales['A'];

                ctx.save();
                ctx.beginPath();
                ctx.moveTo(xaxis.getPixelForValue(undefined, index), yaxis.top + 32);
                ctx.strokeStyle = '#005580';
                ctx.lineTo(xaxis.getPixelForValue(undefined, index), yaxis.bottom);
                ctx.stroke();
                ctx.restore();
            };
    }
}
};

Chart.pluginService.register(verticalLinePlugin);
Nivelety
  • 73
  • 1
  • 1
  • 8