Skip to content

Commit

Permalink
ui-spacetimechart: improve data label
Browse files Browse the repository at this point in the history
data label would disappear if we go too close to the right or bottom border of the canvas. the label now shifts above or left of the mouse to stay visible
  • Loading branch information
anisometropie committed Feb 19, 2025
1 parent 7ba94da commit b65d392
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ui-spacetimechart/src/stories/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,15 @@ const DataLabel = ({
position,
isDiff,
marginTop = 0,
shiftTextX = 0,
shiftTextY = 0,
}: {
data: DataPoint;
position: Point;
isDiff?: boolean;
marginTop?: number;
shiftTextX?: number;
shiftTextY?: number;
}) => (
<div
style={{
Expand All @@ -75,7 +79,10 @@ const DataLabel = ({
}}
>
<Cross />
<div className="content" style={{ background: WHITE_75 }}>
<div
className="content"
style={{ background: WHITE_75, marginTop: `${shiftTextY}px`, marginLeft: `${shiftTextX}px` }}
>
{isDiff ? (
<>
<div>Time difference: {formatTimeLength(new Date(data.time))}</div>
Expand All @@ -95,7 +102,7 @@ const DataLabel = ({
* This component renders a DataLabel under the mouse, using the MouseContext from the SpaceTimeChart:
*/
export const MouseTracker = ({ reference }: { reference?: DataPoint }) => {
const { getPoint } = useContext(SpaceTimeChartContext);
const { getPoint, width, height } = useContext(SpaceTimeChartContext);
const { position, data, isHover } = useContext(MouseContext);

return isHover ? (
Expand All @@ -113,7 +120,8 @@ export const MouseTracker = ({ reference }: { reference?: DataPoint }) => {
}
position={position}
isDiff={!!reference}
marginTop={30}
shiftTextX={position.x > width - 100 ? -100 : 10}
shiftTextY={position.y > height - 50 ? -50 : 20}
/>
</>
) : null;
Expand Down

0 comments on commit b65d392

Please sign in to comment.