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 canvas.js this Functionality works but i want to use chart.js only ( Canvas Js Jsfiddle https://jsfiddle.net/uwfom5Le/ )`enter code here`
解决方案
尝试
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();
}
},
//...
}
推荐阅读
- php - 阿帕奇。如何将 wp-config.php 和 wp-content 从 wordpress 核心移出?
- javascript - 如何将资产中的图像附加到rails 5中的红宝石对象
- javascript - How to filter a Google Visualization Chart by row headers (categories) instead of first column
- amazon-web-services - 是否可以通过刀引导程序选择性地同步食谱?
- windows - CMake 找不到 ifort 编译器
- javascript - 以不同方式为每个 Materialize.css 集合操作着色
- javascript - Joi schema.validate 自定义类型返回未定义的值
- python - 枕头不会导入
- templates - Beego 模板 - 使用带有静态页面的持久模板
- html - 如何调整图片大小