Skip to content

Commit

Permalink
fix(D3 plugin): fix the detection of the closest point for line chart (
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzmadom authored Aug 26, 2024
1 parent ce57909 commit b11fa31
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/plugins/d3/__stories__/line/Split.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ const ChartStory = () => {
},
},
},
chart: {
events: {
click: action('chart.events.click'),
},
},
};

if (loading) {
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/d3/renderer/utils/get-closest-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ function getClosestPointsByXValue(x: number, y: number, points: ShapePoint[]) {
closestYIndex = closestPoints.length - 1;
} else {
closestYIndex = closestPoints.findIndex((p) => y > p.y0 && y < p.y1);
if (closestYIndex === -1) {
const sortedY = sort(
closestPoints.map((p, index) => ({index, y: p.y1 + (p.y0 - p.y1) / 2})),
(p) => p.y,
);
const sortedYIndex = bisector<{y: number}, number>((p) => p.y).center(sortedY, y);
closestYIndex = sortedY[sortedYIndex]?.index ?? -1;
}
}

return closestPoints.map((p, i) => ({
Expand Down

0 comments on commit b11fa31

Please sign in to comment.