有没有办法使用Chart.JS 2.0创建垂直线(事件线,相位变化)?
我在网上看到了一些例子(see this related question),但是,当使用Moment.js创建水平轴时,不可能给LineAtIndex一个moment.js日期来在该日期创建一条线 .
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();
}
}
});
这是展示问题的小提琴:https://jsfiddle.net/harblz/0am8vehg/
我相信我的问题是我没有正确理解这段代码:
var xaxis = chart.scales ['x-axis-0'];
var yaxis = chart.scales ['y-axis-0'];
如果我能够解决这个问题,我会在这里发布一个工作小提琴,以供今后用户处理同一个项目 .
谢谢你的时间阅读这个:)