Chart.js / HTML Canvas - 移动线

Chart.js / HTML Canvas - move line

我需要使用 Chart.js 在 HTML canvas 上移动垂直线。

我正在使用这个:

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/ 线正在移动。

所以我需要的可能是这两个示例的组合。

感谢您的回复。

我用插件解决了

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