使用Chart.js添加水平十字光标

使用Chart.js添加水平十字光标,chart.js,Chart.js,我正在使用Chart.js,一直在寻找一种在我的图表中添加水平注释/十字线的方法 How to create a horizontal crosshair that changes its position based on the cursor movement using Chart.js? JSFiddle for my application :- https://jsfiddle.net/codeandcloud/vz4qhqpw/ I have seen that in canv

我正在使用Chart.js,一直在寻找一种在我的图表中添加水平注释/十字线的方法

How to create a horizontal crosshair that changes its position based on the cursor movement using Chart.js?

JSFiddle for my application :- https://jsfiddle.net/codeandcloud/vz4qhqpw/

I have seen that in canvas.js this Functionality works but i want to use chart.js only ( Canvas Js Jsfiddle https://jsfiddle.net/uwfom5Le/ )`enter code here`
试一试

const选项={
悬停:{
交集:错
},
onHover:function(this:Chart,event:MouseEvent,activeElements:Array):any{
if(activeElements.length){
const activePoint=activeElements[0]如有;
const ctx=this.ctx;
如果(!ctx){
返回;
}
常量x=活动点。\u view.x;
常量y=活动点。\u view.y;
const leftX=this.chartraea.left;
const topY=this.chartraea.top;
const RightX=this.chartraea.right;
const bottomY=this.chartArea.bottom;
ctx.beginPath();
ctx.moveTo(x,topY);
ctx.lineTo(x,底部);
ctx.moveTo(leftX,y);
ctx.lineTo(右X,y);
ctx.lineWidth=1;
ctx.strokeStyle=“#C2C7CC”;
ctx.stroke();
ctx.closePath();
}
},
//...
}
试试看

const选项={
悬停:{
交集:错
},
onHover:function(this:Chart,event:MouseEvent,activeElements:Array):any{
if(activeElements.length){
const activePoint=activeElements[0]如有;
const ctx=this.ctx;
如果(!ctx){
返回;
}
常量x=活动点。\u view.x;
常量y=活动点。\u view.y;
const leftX=this.chartraea.left;
const topY=this.chartraea.top;
const RightX=this.chartraea.right;
const bottomY=this.chartArea.bottom;
ctx.beginPath();
ctx.moveTo(x,topY);
ctx.lineTo(x,底部);
ctx.moveTo(leftX,y);
ctx.lineTo(右X,y);
ctx.lineWidth=1;
ctx.strokeStyle=“#C2C7CC”;
ctx.stroke();
ctx.closePath();
}
},
//...
}
  const options = {
    hover: {
      intersect: false
    },
    onHover: function(this: Chart, event: MouseEvent, activeElements: Array<object>): any {
      if (activeElements.length) {
        const activePoint = activeElements[0] as any;
        const ctx = this.ctx;
        if (!ctx) {
          return;
        }
        const x = activePoint._view.x;
        const y = activePoint._view.y;
        const leftX = this.chartArea.left;
        const topY = this.chartArea.top;
        const RightX = this.chartArea.right;
        const bottomY = this.chartArea.bottom;
        ctx.beginPath();
        ctx.moveTo(x, topY);
        ctx.lineTo(x, bottomY);
        ctx.moveTo(leftX, y);
        ctx.lineTo(RightX, y);
        ctx.lineWidth = 1;
        ctx.strokeStyle = "#C2C7CC";
        ctx.stroke();
        ctx.closePath();
      }
    },
   //...
}