高图在可拖动折线图上重新绘制数据并对其进行排序

时间:2018-11-28 11:42:50

标签: javascript highcharts

我在Highcharts中有一个折线图,想要将点拖到另一点时重绘或折线。

我想在拖动时重绘图表并对其进行排序,以使线条更新并且不会彼此交叉。我们可以使用chart.series [0] .setData()或chart.series [0] .update();更新图表。但是在更新图表时,我不断收到排序错误或可拖动点为空(dragPoint或hoverPoint)的错误

这里是一个示例,其中我通过更改可拖动模块中的getNewPos(e)函数来检测何时将一个点拖动到另一个点上 http://jsfiddle.net/u4xpmf2j/1/

 */
function getNewPos(e) {
  toClose = false;
  var originalEvent = e.originalEvent || e,
    pageX = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageX : e.pageX,
    pageY = originalEvent.changedTouches ? originalEvent.changedTouches[0].pageY : e.pageY,
    series = dragPoint.series,
    draggableX = series.options.draggableX && dragPoint.draggableX !== false,
    draggableY = series.options.draggableY && dragPoint.draggableY !== false,
    dragSensitivity = pick(series.options.dragSensitiviy, 1),
    dragMaxToPoint = pick(series.options.dragMaxToPoint, 0.3),
    deltaX = draggableX ? dragX - pageX : 0,
    deltaY = draggableY ? dragY - pageY : 0,
    newPlotX = dragPlotX - deltaX,
    newPlotY = dragPlotY - deltaY,
    newX = dragX === undefined ? dragPoint.x : series.xAxis.toValue(newPlotX, true),
    newY = dragY === undefined ? dragPoint.y : series.yAxis.toValue(newPlotY, true),
    ret;
  newX = filterRange(newX, series, 'X');
  newY = filterRange(newY, series, 'Y');
  if (dragPoint.index > 0 && (newX - dragPoint.series.data[dragPoint.index - 1].x) < dragMaxToPoint || dragPoint.index < dragPoint.series.data.length - 1 && (newX - dragPoint.series.data[dragPoint.index + 1].x) > -dragMaxToPoint) {
    toClose = true; // DETECT POINT BEING DRAGGED PAST ANOTHER
  }
  if (Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)) > dragSensitivity) {
    return {
      x: draggableX ? newX : dragPoint.x,
      y: draggableY ? newY : dragPoint.y
    };
  } else {
    return null;
  }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如何在放置事件之后对数据进行排序,然后使用series.setData更新图表:

  plotOptions: {
    series: {
      point: {
        events: {
          drop: function() {
            var series = this.series,
                xData = series.xData.slice(),
                yData = series.yData.slice(),
                newData = [];

            xData.forEach(function(data, i) {
                newData.push([
                data,
                yData[i]
              ]);
            });

            newData.sort(function(a, b) {
                return a[0] - b[0];
            });

            series.setData(newData, false, false);     
            series.chart.redraw();
          }
        }
      },
      stickyTracking: false
    },
  }

演示: http://jsfiddle.net/BlackLabel/6kyt3roL/