简体   繁体   English

在zoomcallback上获取最近的min x和max x数据点?

[英]Getting nearest min x and max x data points on zoomcallback?

I am using the zoomcallback on dygraphs, but I have an issue in that the min and max values pased back in the callback are effectively interpolated values. 我在dygraphs上使用zoomcallback,但我有一个问题,即在回调中回调的最小值和最大值是有效插值。 I need to map these callback values to my original dataset which I have hashed my x. 我需要将这些回调值映射到我已经散列x的原始数据集。

Iterating over my original dataset looking for the closest x value for min and the closest value for y is not viable. 迭代我的原始数据集,寻找min的最接近的x值,y的最接近的值是不可行的。

Anyone know a way of getting to the actual data values that are at the min x and max x viewable portion of the graph? 任何人都知道一种获取图表的最小x和最大x可视部分的实际数据值的方法吗?

The getRowForX method in dygraphs 2.0 does something similar to what you want, but it will only return an index if you match an x-value exactly. dygraphs 2.0中的getRowForX方法与您想要的类似,但只有在完全匹配x值时才会返回索引。 It's implemented using binary search. 它是使用二进制搜索实现的。 To get the closest row number for an inexact match only requires a small tweak: 要获得最接近的行号以进行不精确的匹配,只需要进行一些小调整:

function getCloseRowForX(g, xVal) {
  var low = 0,
      high = g.numRows() - 1;

  while (low <= high) {
    var idx = (high + low) >> 1;
    var x = g.getValue(idx, 0);
    if (x < xVal) {
      low = idx + 1;
    } else if (x > xVal) {
      high = idx - 1;
    } else {
      return idx;
    }
  }

  return idx;
};

In your zoomCallback you can do something like this: 在你的zoomCallback你可以这样做:

zoomCallback() {
  const [leftX, rightX] = this.xAxisRange();
  const leftIndex = getCloseRowForX(this, leftX);
  const rightIndex = getCloseRowForX(this, rightX);
  const leftValue = this.getValue(leftIndex, seriesNum);
  const rightValue = this.getValue(rightIndex, seriesNum);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM