Javascript 使任意线虚线

Javascript 使任意线虚线,javascript,html5-canvas,chart.js,Javascript,Html5 Canvas,Chart.js,我按照这个问题得出了一个任意的结论: 我使用这行代码更改为虚线: this.chart.ctx.setLineDash([3]); 但是这也改变了图表上的线条,我只想改变任意的线条。我怎样才能适应这个 谢谢。只需将设置ctx属性的块包装起来,并用以下行绘制以保存和恢复上下文 this.chart.ctx.save(); ... this.chart.ctx.restore(); 小提琴- var数据={ 标签:[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“

我按照这个问题得出了一个任意的结论:

我使用这行代码更改为虚线:

this.chart.ctx.setLineDash([3]);
但是这也改变了图表上的线条,我只想改变任意的线条。我怎样才能适应这个


谢谢。

只需将设置
ctx
属性的块包装起来,并用以下行绘制以保存和恢复上下文

this.chart.ctx.save();
...
this.chart.ctx.restore();

小提琴-

var数据={
标签:[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”],
数据集:[{
数据:[12,3,2,1,8,8,2,2,3,5,7,1]
}]
};
var ctx=document.getElementById(“LineWithLine”).getContext(“2d”);
Chart.types.Line.extend({
名称:“LineWithLine”,
绘图:函数(){
Chart.types.Line.prototype.draw.apply(这是参数);
var point=this.dataset[0]。points[this.options.lineAtIndex]
var scale=此.scale
//划线
this.chart.ctx.save();
这个.chart.ctx.setLineDash([3]);
this.chart.ctx.beginPath();
这个.chart.ctx.moveTo(点.x,标度.startPoint+24);
this.chart.ctx.strokeStyle='#ff0000';
this.chart.ctx.lineTo(point.x,scale.endPoint);
this.chart.ctx.stroke();
this.chart.ctx.restore();
//今天写
this.chart.ctx.textAlign='center';
this.chart.ctx.fillText(“今天”,point.x,scale.startPoint+12);
}
});
新图表(ctx).LineWithLine(数据{
datasetFill:false,
线性指数:2
});

好答案,但请使用,以便答案的代码显示在答案中,并避免链接损坏。