我正在使用此插件绘制一条垂直线,用户触摸/悬停在图表上:
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
if (chart.tooltip._active && chart.tooltip._active.length) {
var activePoint = chart.tooltip._active[0],
ctx = chart.ctx,
y_axis = chart.scales['y-axis-0'],
x = activePoint.tooltipPosition().x,
topY = y_axis.top,
bottomY = y_axis.bottom;
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 1;
ctx.strokeStyle = '#26D4A5';
ctx.stroke();
ctx.restore();
}
}
});
在悬停结束后线被移除,但是如果发生touchend / touchcancel,则该线仍在那里。
我尝试将afterEvent添加到插件中,如下所示:
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
if (chart.tooltip._active && chart.tooltip._active.length) {
var activePoint = chart.tooltip._active[0],
ctx = chart.ctx,
y_axis = chart.scales['y-axis-0'],
x = activePoint.tooltipPosition().x,
topY = y_axis.top,
bottomY = y_axis.bottom;
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 1;
ctx.strokeStyle = '#26D4A5';
ctx.stroke();
ctx.restore();
}
},
afterEvent: function(chart, e) {
if(e.type=="mouseout"){
alert("mouseout");
}
if(e.type=="touchend"){
alert("touchend");
}
if(e.type=="mousemove"){
alert("mouse move");
}
}
});
但是唯一可以触摸的事件是mousemove,touchend和mouseout在移动/触摸上不起作用。有没有解决方法?
答案 0 :(得分:2)
似乎问题是" touchend"事件已从默认配置中删除。
重新添加活动" touchend"对于这样的选项,在touchend之后删除工具提示。
events: ["mousemove", "mouseout", "click", "touchstart", "touchmove", "touchend"]