我找到了一种方法,特别是使用axis.p2c函数。这是一个示例函数,它使用此逻辑绘制指向当前时间的插入符号:
window.TimePointer = function(plot, currentTime) {
// Get the currente axes
var axes = plot.getAxes();
var xaxis = axes.xaxis;
var yaxis = axes.yaxis;
var element = plot.getPlaceholder();
// Find the chart icon element
var pointer = element.find('.time-pointer');
// Check that the intended point is inside the visible area
if((currentTime < xaxis.max) && (currentTime > xaxis.min)) {
if(pointer.length == 0) {
// If it doesn't exist already, create it
element.append('<i class="time-pointer icon-caret-down"></i>');
pointer = element.find('.time-pointer');
}
var plotOffset = plot.offset();
var offset = {
top: plotOffset.top - pointer.height(),
left: xaxis.p2c(currentTime) + plotOffset.left - (pointer.outerWidth()/2)
};
pointer.offset(offset);
} else {
// If the currently selected area doesn't contain the point, erase the pointer
pointer.remove();
}
}
关键线在偏移量计算中
xaxis.p2c(time) + plotOffset.left - (pointer.outerwidth()/2)
基于此,我还可以按照相同的逻辑在任何给定时间点向我的图表添加基于时间的注释。请注意,最好将此函数绑定到图表的 drawOverlay 阶段,以便充分利用 resize 事件等功能。