Skip to content

Commit 41ed5f9

Browse files
committed
Toggle exact notation in Live vis tooltip with wheel button
1 parent 849beff commit 41ed5f9

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

packages/lib/src/vis/line/LineVis.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function LineVis(props: Props) {
124124

125125
<TooltipMesh
126126
guides="vertical"
127-
renderTooltip={(x) => {
127+
renderTooltip={(x, _, exact) => {
128128
const xi = abscissaToIndex(x);
129129
const abscissa = abscissas[xi];
130130

@@ -137,7 +137,7 @@ function LineVis(props: Props) {
137137

138138
return (
139139
<>
140-
{`${abscissaLabel ?? 'x'} = ${formatTooltipVal(abscissa)}`}
140+
{`${abscissaLabel ?? 'x'} = ${exact ? abscissa : formatTooltipVal(abscissa)}`}
141141

142142
<div className={styles.tooltipValue}>
143143
{auxiliaries.length > 0 && (
@@ -148,8 +148,9 @@ function LineVis(props: Props) {
148148
/>
149149
)}
150150
<span>
151-
<strong>{formatTooltipVal(value)}</strong>
152-
{error !== undefined && ` ±${formatTooltipErr(error)}`}
151+
<strong>{exact ? value : formatTooltipVal(value)}</strong>
152+
{error !== undefined &&
153+
` ±${exact ? error : formatTooltipErr(error)}`}
153154
{dtype && <em>{` (${dtype})`}</em>}
154155
</span>
155156
</div>
@@ -162,8 +163,9 @@ function LineVis(props: Props) {
162163
style={{ color: auxColors[index % auxColors.length] }}
163164
/>
164165
{label ? `${label} = ` : ''}
165-
{formatTooltipVal(array.get(xi))}
166-
{errors && ` ±${formatTooltipErr(errors.get(xi))}`}
166+
{exact ? array.get(xi) : formatTooltipVal(array.get(xi))}
167+
{errors &&
168+
` ±${exact ? errors.get(xi) : formatTooltipErr(errors.get(xi))}`}
167169
</div>
168170
))}
169171
</>

packages/lib/src/vis/shared/TooltipMesh.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useToggle } from '@react-hookz/web';
12
import { type ThreeEvent } from '@react-three/fiber';
23
import { useTooltip } from '@visx/tooltip';
34
import { type ReactElement, useCallback } from 'react';
@@ -10,7 +11,11 @@ import VisMesh from './VisMesh';
1011
interface Props {
1112
size?: Size;
1213
guides?: 'horizontal' | 'vertical' | 'both';
13-
renderTooltip: (x: number, y: number) => ReactElement | undefined;
14+
renderTooltip: (
15+
x: number,
16+
y: number,
17+
exact: boolean,
18+
) => ReactElement | undefined;
1419
}
1520

1621
function TooltipMesh(props: Props) {
@@ -27,6 +32,8 @@ function TooltipMesh(props: Props) {
2732
hideTooltip,
2833
} = useTooltip<Coords>();
2934

35+
const [isExact, toggleExact] = useToggle();
36+
3037
// Show and/or update tooltip when pointer moves except when dragging
3138
const onPointerMove = useCallback(
3239
(evt: ThreeEvent<PointerEvent>) => {
@@ -57,7 +64,16 @@ function TooltipMesh(props: Props) {
5764
}, [hideTooltip, tooltipOpen]);
5865

5966
// Hide tooltip when user starts panning
60-
const onPointerDown = useCallback(() => hideTooltip(), [hideTooltip]);
67+
const onPointerDown = useCallback(
68+
(evt: ThreeEvent<PointerEvent>) => {
69+
if (evt.button === 1) {
70+
toggleExact();
71+
} else {
72+
hideTooltip();
73+
}
74+
},
75+
[hideTooltip, toggleExact],
76+
);
6177

6278
// Show tooltip after dragging, if pointer is released inside the vis viewport
6379
const onPointerUp = useCallback(
@@ -71,7 +87,7 @@ function TooltipMesh(props: Props) {
7187
[height, onPointerMove, width],
7288
);
7389

74-
const content = tooltipData && renderTooltip(...tooltipData);
90+
const content = tooltipData && renderTooltip(...tooltipData, isExact);
7591

7692
return (
7793
<>

0 commit comments

Comments
 (0)