github.com/grafana/pyroscope@v1.18.0/public/app/components/TimelineChart/util.ts (about)

     1  export type TimelineChartSelectionTimeRangeAxis = {
     2    from: number;
     3    to: number;
     4  };
     5  
     6  export type TimelineAxisSelection = {
     7    from: number;
     8    to: number;
     9  };
    10  
    11  export type TimelineVisibleData = Array<{
    12    data: number[][];
    13  }>;
    14  
    15  /**
    16   * Evaluates if a time range contains points suitable to zoom into.
    17   * The criteria is that there should be at least two points on the same data set.
    18   * If the number of xaxis pixels is smaller than number of points in a dataset,
    19   * we assume that no xaxisRange can be selected that won't contain at least two points.
    20   *
    21   * @param xaxisRange mouse drag/drop selection range along the xaxis
    22   * @param datasets a collection of datasets that are currently visible on the chart
    23   * @param xaxisPixels the width of the chart in pixels
    24   * @returns
    25   */
    26  export function rangeIsAcceptableForZoom(
    27    xaxisRange: TimelineAxisSelection,
    28    datasets: TimelineVisibleData,
    29    xaxisPixels: number
    30  ) {
    31    if (xaxisRange == null) {
    32      // Invalid range, do nothing.
    33      return false;
    34    }
    35  
    36    const { from, to } = xaxisRange;
    37  
    38    for (const dataset of datasets) {
    39      const points = dataset.data;
    40  
    41      if (points.length > xaxisPixels) {
    42        // There are more points than pixels visible,
    43        // so we can assume the range is safe.
    44        return true;
    45      }
    46  
    47      let pointCount = 0;
    48      for (const point of points) {
    49        // If at least two points are on the range, it is acceptable.
    50        if (point[0] > from && point[0] < to) {
    51          pointCount++;
    52          if (pointCount >= 2) {
    53            return true;
    54          }
    55        }
    56      }
    57    }
    58  
    59    return false;
    60  }