UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

39 lines (37 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findClosestPoints = findClosestPoints; var _scaleGuards = require("../../../scaleGuards"); function findClosestPoints(flatbush, seriesData, xScale, yScale, xZoomStart, xZoomEnd, yZoomStart, yZoomEnd, svgPointX, svgPointY, maxRadius = Infinity, maxResults = 1) { const originalXScale = xScale.copy(); const originalYScale = yScale.copy(); originalXScale.range([0, 1]); originalYScale.range([0, 1]); const excludeIfOutsideDrawingArea = function excludeIfOutsideDrawingArea(index) { const x = originalXScale(seriesData[index].x); const y = originalYScale(seriesData[index].y); return x >= xZoomStart && x <= xZoomEnd && y >= yZoomStart && y <= yZoomEnd; }; // We need to convert the distance from the original range [0, 1] to the current drawing area // so the comparison is done on pixels instead of normalized values. // fx and fy are the factors to convert the distance from [0, 1] to the current drawing area. const fx = xScale.range()[1] - xScale.range()[0]; const fy = yScale.range()[1] - yScale.range()[0]; const fxSq = fx * fx; const fySq = fy * fy; function sqDistFn(dx, dy) { return fxSq * dx * dx + fySq * dy * dy; } const pointX = originalXScale(invertScale(xScale, svgPointX, dataIndex => seriesData[dataIndex]?.x)); const pointY = originalYScale(invertScale(yScale, svgPointY, dataIndex => seriesData[dataIndex]?.y)); return flatbush.neighbors(pointX, pointY, maxResults, maxRadius != null ? maxRadius * maxRadius : Infinity, excludeIfOutsideDrawingArea, sqDistFn); } function invertScale(scale, value, getDataPoint) { if ((0, _scaleGuards.isOrdinalScale)(scale)) { const dataIndex = scale.bandwidth() === 0 ? Math.floor((value - Math.min(...scale.range()) + scale.step() / 2) / scale.step()) : Math.floor((value - Math.min(...scale.range())) / scale.step()); return getDataPoint(dataIndex); } return scale.invert(value); }