【问题标题】:Highcharts Annotation SVG doesn't move with other shapesHighcharts Annotation SVG 不随其他形状移动
【发布时间】: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


    【解决方案1】:
     d: ["M", 440, 72, "L", 410, 45, 470, 45, "Z"],
    

    我不推荐这种方式来创建可拖动的自定义形状。此选项按预期创建形状,但也设置固定位置。或许可以实现移动功能,但我认为这需要对可拖动的核心代码进行大量更改。

    我可以建议这样做:

    annotations: [{
     draggable: 'x',
     shapes: [{
       points: [{
         x: 440,
         y: 72
       }, {
         x: 410,
         y: 45
       }, {
         x: 470,
         y: 45
       }, {
         x: 440,
         y: 72
       }],
       fill: 'red',
       type: 'path',
       stroke: "blue",
       strokeWidth: 3,
       markerStart: 'circle',
     }]
    }]
    

    其中 markerStart 是定义的形状。 See a Demo

    【讨论】:

    • 嗨塞巴斯蒂安,感谢您的回答。但是在尝试了我的图表之后,结果证明很难计算出正确的坐标。最后我使用类型“图像”来显示形状。该形状是一个 Font-Awesome 图标,所以在使用“图像”之前,我确实尝试添加一个带有“useHTML”的标签:true,但似乎图标在第一次重绘(false)后移动了一点,不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2015-12-17
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多