highcharts 如何创建多条可拖动的图表?

efzxgjgh  于 2023-04-30  发布在  Highcharts
关注(0)|答案(1)|浏览(226)

有人能让这些红线在Highcharts上可拖动吗
尝试使用plotlines时,元素未拖动

  • 移动1行时,所有行都应移动
  • 如果我选择第一条红线,那么第二条红线也应该移动相同的点。

代码审查:https://jsfiddle.net/gauravjain024/vfq5ybje/11/
enter image description here

var chart = Highcharts.chart('container', {
    chart: {
        events: {
            load() {
                var chart = this,
                    lineWidth = 5;
                console.log('chart.xAxis, ', chart.container)
                let draggablePlotLine = chart.xAxis[0].plotLinesAndBands[0].svgElem
                /* chart.renderer.rect(100, chart.plotTop, lineWidth, chart.plotHeight)
                          .attr({
                 fill: 'red'
                          })
                .add(); */
                console.log('draggablePlotLine', draggablePlotLine);
                    //console.log('chart.xAxis[0].plotLinesAndBands[0]',     chart.xAxis[0].plotLinesAndBands[0].svgElem)
                chart.container.onmousemove = function (e) {
                    console.log('onmousemove')
                    if (draggablePlotLine.drag) {
                        let normalizedEvent = chart.pointer.normalize(e),
                            extremes = {
                                left: chart.plotLeft,
                                right: chart.plotLeft + chart.plotWidth
                            };

                        // Move line
                        if (
                            e.chartX >= extremes.left &&
                            e.chartX <= extremes.right
                        ) {
                            //console.log('draggablePlotLine', e.chartX)
                            //draggablePlotLine.options.value = e.chartX;
                            draggablePlotLine.attr({
                                x: e.chartX
                            })
                        }
                    }
                }

                draggablePlotLine.element.onmousedown = function () {
                    console.log('onmousemdown')
                    draggablePlotLine.drag = true
                }

                draggablePlotLine.element.onmouseup = function () {
                    draggablePlotLine.drag = false
                }

            }
        }
    },
    xAxis: {
        plotLines: [{
            color: "#FF0000",
            width: 5,
            value: 3,
            drag: true,
            label: {
                text: "this is label"
            }
        },
        {
            color: "#FF0000",
            width: 5,
            value: 5,
            label: {
                text: "this is label"
            }
        },
        {
            color: "#FF0000",
            width: 5,
            value: 7,
            label: {
                text: "this is label"
            }
        }
        ]
    },
    series: [{
        name: 'Installation',
        data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175]
    }],
});
nszi6y05

nszi6y051#

使用自定义元素而不是创建默认的地块线将更容易和更有效,类似于以下线程:How do I create a draggable plot line in Highcharts?
下面的解决方案增加了响应性,并在拖动时连接线条。

chart: {
    events: {
      render() {
        const chart = this,
          xAxis = this.xAxis[0];

        if (!chart.draggablePlotLines) {
          chart.draggablePlotLines = [];

          linesOptions.forEach(({
            width,
            color,
            value
          }) => {
            const line = chart.renderer.rect(null, chart.plotTop, width, chart.plotHeight)
              .attr({
                fill: color
              })
              .add();

            line.element.onmousedown = function() {
              chart.draggablePlotLine = line;
            }

            line.element.onmouseup = function() {
              chart.draggablePlotLine = false;
            }

            chart.draggablePlotLines.push(line);
          });
        }

        chart.draggablePlotLines.forEach((line, index) => {
          line.attr({
            x: xAxis.toPixels(linesOptions[index].value) - linesOptions[index].width / 2
          });
        });
      },
      load() {
        const chart = this,
          xAxis = this.xAxis[0];

        chart.container.onmousemove = function(e) {
          if (chart.draggablePlotLine) {
            let normalizedEvent = chart.pointer.normalize(e),
              extremes = {
                left: chart.plotLeft,
                right: chart.plotLeft + chart.plotWidth
              };

            if (
              e.chartX >= extremes.left &&
              e.chartX <= extremes.right
            ) {
              const distance = e.chartX - chart.draggablePlotLine.attr('x');

              chart.draggablePlotLines.forEach((line, index) => {
                if (line === chart.draggablePlotLine) {
                  line.attr({
                    x: e.chartX
                  });
                  linesOptions[index].value = xAxis.toValue(e.chartX);
                } else {
                  line.attr({
                    x: line.attr('x') + distance
                  });
                  linesOptions[index].value = xAxis.toValue(line.attr('x') + distance);
                }
              });
            }
          }
        }
      }
    }
  }

现场演示:https://jsfiddle.net/BlackLabel/o4m7tfds/
**API引用:**https:// www.example.com

相关问题