【发布时间】:2020-02-16 02:38:09
【问题描述】:
在我的 Highcharts 项目中,我成功地使单个注释中的所有形状都可拖动,但是当我添加 SVG(此形状的类型为“路径”)时,SVG 不会与其他形状一起移动注释。
我需要在注释中有一些自定义的形状。谁能指出这个 SVG 的问题是什么?是否可以将 SVG 放在注释中并且仍然可以拖动?还是我犯的一些错误导致了这个问题?
我的例子在这里。 https://jsfiddle.net/roy_jaide/754xuhtk/ 可以看到,label、circle、line 都是可以拖动的,但是 SVG 的形状根本不动。
感谢您阅读我的问题,如果提供任何解决方案,我们将不胜感激。
Highcharts.Annotation.prototype.onDrag = function (e) {
if (this.chart.isInsidePlot(e.chartX - this.chart.plotLeft, e.chartY - this.chart.plotTop)) {
var translation = this.mouseMoveToTranslation(e),
xAxis = this.chart.xAxis[0],
yAxis = this.chart.yAxis[0],
xStep = this.options.stepX,
yStep = this.options.stepY,
pxStep = xAxis.toPixels(xStep) - xAxis.toPixels(0),
pyStep = yAxis.toPixels(yStep) - yAxis.toPixels(0);
if (this.options.draggable === 'x') { //for now, it's exclusive for age handle
this.potentialTranslationX += translation.x;
if (Math.abs(this.potentialTranslationX) >= Math.abs(pxStep)) {
translation.x = (this.potentialTranslationX > 0) ? pxStep : -pxStep;
translation.y = 0;
this.currentXValue += (this.potentialTranslationX > 0) ? xStep : -xStep;
this.potentialTranslationX -= (this.potentialTranslationX > 0) ? pxStep : -pxStep; //minus the step and continue to cumulate
//this.potentialTranslation = 0; //not correct, the mouse will go faster than the handle
if (this.points.length) {
this.translate(translation.x, 0);
} else {
this.shapes.forEach(function (shape) {
shape.translate(translation.x, 0);
});
this.labels.forEach(function (label) {
label.translate(translation.x, 0);
label.text = label.annotation.options.preText + label.annotation.currentXValue;
});
}
}
}
this.redraw(false);
}
}
更新 1:在我的图表上尝试了 Sebastian 的答案后,结果证明很难计算出正确的坐标。最后我使用类型“图像”来显示形状。该形状是一个 Font-Awesome 图标,所以在使用“图像”之前,我确实尝试添加一个带有“useHTML”的标签:true,但似乎图标在第一次重绘(false)后移动了一点,不知道为什么。
The image of the shape. I achieved this by adding "image" shape.
【问题讨论】:
标签: javascript svg highcharts annotations