在 Highcharts 散点图中按比例缩放 xy

Zoom xy proportionally in Highcharts scatter plot

有散点图:

https://jsfiddle.net/hdq4rc35/

我希望能够仅按比例缩放 xy,因此 y 刻度之间的距离在任何时候都与 x 刻度相同,从而产生方形缩放字段而不是一个自由矩形。

我尝试了 chart.setExtremes() 但它并没有影响缩放功能。

Highcarts 默认不支持该功能,但您可以通过包装 Pointer 原型的 drag 方法来添加它,例如:

(function(H) {
  H.wrap(H.Pointer.prototype, 'drag', function(proceed, e) {
    var chartX = e.chartX,
      chartY = e.chartY,
      mouseDownX = this.mouseDownX,
      mouseDownY = this.mouseDownY,
      rectX = Math.abs(chartX - mouseDownX),
      rectY = Math.abs(chartY - mouseDownY);

    if (rectX > rectY) {
      e.chartY = chartY + (
        mouseDownY > chartY ?
        -(rectX - rectY) :
        (rectX - rectY)
      );
    } else {
      e.chartX = chartX + (
        mouseDownX > chartX ?
        -(rectY - rectX) :
        (rectY - rectX)
      );
    }

    proceed.apply(this, Array.prototype.slice.call(arguments, 1));
  });
}(Highcharts));

现场演示: https://jsfiddle.net/BlackLabel/aubkLt5o/

API参考:https://www.highcharts.com/docs/extending-highcharts/extending-highcharts