简体   繁体   中英

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. I need to map these callback values to my original dataset which I have hashed my x.

Iterating over my original dataset looking for the closest x value for min and the closest value for y is not viable.

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?

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. 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() {
  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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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