Skip to content

Commit

Permalink
Refactored method to find the nearest point to the pointer.
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoPazz committed Nov 15, 2024
1 parent b028d67 commit 853b2ad
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions lib/ReactViews/Custom/Chart/BottomDockChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,26 @@ class Chart extends React.Component {
get pointsNearMouse() {
if (!this.mouseCoords) return [];
return this.chartItems
.map((chartItem) => ({
chartItem,
point: findNearestPoint(
.map((chartItem, idx) => {
const point = findNearestPoint(
chartItem.points,
this.mouseCoords,
this.xScale,
7,
chartItem === this.chartItems[1],
this.props.terria
)
}))
7
);

if (idx === 1) {
this.props.terria.setSelectedStopSummaryRowIndex(
"fromChart",
point ? chartItem.points.indexOf(point) : null
);
}

return {
chartItem,
point
};
})
.filter(({ point }) => point !== undefined);
}

Expand Down Expand Up @@ -258,7 +267,7 @@ class Chart extends React.Component {
if (idx !== null && this.props.chartItems) {
const sumDistances =
this.props.terria.measurableGeom.stopGroundDistances
.slice(0, idx)
.slice(0, idx + 1)
.reverse()
.reduce((acc, distance) => acc + distance, 0);

Expand Down Expand Up @@ -617,14 +626,7 @@ function sortChartItemsByType(chartItems) {
});
}

function findNearestPoint(
points,
coords,
xScale,
maxDistancePx,
isAirChartItem = false,
terria = undefined
) {
function findNearestPoint(points, coords, xScale, maxDistancePx) {
function distance(coords, point) {
return point ? coords.x - xScale(point.x) : Infinity;
}
Expand All @@ -648,13 +650,6 @@ function findNearestPoint(
p ? Math.abs(distance(coords, p)) : Infinity
);

if (isAirChartItem) {
terria.setSelectedStopSummaryRowIndex(
"fromChart",
points.indexOf(nearestPoint)
);
}

return Math.abs(distance(coords, nearestPoint)) <= maxDistancePx
? nearestPoint
: undefined;
Expand Down

0 comments on commit 853b2ad

Please sign in to comment.