Skip to content

Commit

Permalink
internal(xychart/findNearestGroupDatum): improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
williaster committed Dec 7, 2020
1 parent d5cb1cf commit 953718a
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions packages/visx-xychart/src/utils/findNearestGroupDatum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@ export default function findNearestGroupDatum<
if (!datum || !point) return null;

const barGroupOffset = groupScale(dataKey);
const barWidth = groupScale.step(); // @TODO this doesn't currently account for initial paddingOuter
const barWidth = groupScale.step();

if (horizontal) {
const groupPosition = yScale(yAccessor(datum.datum));
const barStart = (groupPosition ?? Infinity) + (barGroupOffset ?? Infinity);
const barEnd = barStart + barWidth;
const barMiddle = (barStart + barEnd) / 2;
const cursorIsOnBar = point.y >= barStart && point.y <= barEnd;
return {
...datum,
distanceX: 0,
distanceY: cursorIsOnBar ? 0 : datum.distanceY,
distanceX: 0, // we want all group bars to have same X distance so only Y distance matters
distanceY: cursorIsOnBar ? 0 : Math.abs(point.y - barMiddle),
};
}
const groupPosition = xScale(xAccessor(datum.datum));
const barStart = (groupPosition ?? Infinity) + (barGroupOffset ?? Infinity);
const barEnd = barStart + barWidth;
const barMiddle = (barStart + barEnd) / 2;
const cursorIsOnBar = point.x >= barStart && point.x <= barEnd;
return {
...datum,
distanceY: 0,
distanceX: cursorIsOnBar ? 0 : datum.distanceX,
distanceY: 0, // we want all group bars to have same Y distance so only X distance matters
distanceX: cursorIsOnBar ? 0 : Math.abs(point.x - barMiddle),
};
}

0 comments on commit 953718a

Please sign in to comment.