【发布时间】:2016-08-25 17:08:34
【问题描述】:
我已经按照这个问题实现了任意一行:
Chart.js — drawing an arbitrary vertical line
我用这行代码改成虚线:
this.chart.ctx.setLineDash([3]);
但这也改变了图表上的线条,我只想改变任意的线条描边。我该如何调整?
谢谢。
【问题讨论】:
标签: javascript html5-canvas chart.js
我已经按照这个问题实现了任意一行:
Chart.js — drawing an arbitrary vertical line
我用这行代码改成虚线:
this.chart.ctx.setLineDash([3]);
但这也改变了图表上的线条,我只想改变任意的线条描边。我该如何调整?
谢谢。
【问题讨论】:
标签: javascript html5-canvas chart.js
只需将设置ctx 属性的块包装起来,然后使用以下行进行绘制以保存和恢复上下文,
this.chart.ctx.save();
...
this.chart.ctx.restore();
小提琴 - http://jsfiddle.net/ps3186ex/
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.save();
this.chart.ctx.setLineDash([3]);
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();
this.chart.ctx.restore();
// 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
});
<script src='https://rawgit.com/nnnick/Chart.js/v1.0.2/Chart.min.js'></script>
<canvas id="LineWithLine" width="600" height="400"></canvas>
【讨论】: