Skip to content

Commit 96d3ca4

Browse files
authored
feat: add tooltip throttle option (#52)
1 parent 7d1f711 commit 96d3ca4

File tree

5 files changed

+9
-4
lines changed

5 files changed

+9
-4
lines changed

src/components/ChartInner/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const ChartInner = (props: ChartInnerProps) => {
6262
unpinTooltip,
6363
xAxis,
6464
yAxis,
65+
tooltipThrottle: tooltip.throttle,
6566
});
6667
const clickHandler = data.chart?.events?.click;
6768
const pointerMoveHandler = data.chart?.events?.pointermove;

src/components/ChartInner/useChartInnerHandlers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {getClosestPoints} from '../../utils/chart/get-closest-data';
1212

1313
import type {useChartInnerState} from './useChartInnerState';
1414

15-
const THROTTLE_DELAY = 50;
16-
1715
type ChartInnerState = ReturnType<typeof useChartInnerState>;
1816

1917
type Props = {
@@ -29,6 +27,7 @@ type Props = {
2927
unpinTooltip: ChartInnerState['unpinTooltip'];
3028
xAxis: PreparedAxis;
3129
yAxis: PreparedAxis[];
30+
tooltipThrottle: number;
3231
};
3332

3433
export function useChartInnerHandlers(props: Props) {
@@ -45,6 +44,7 @@ export function useChartInnerHandlers(props: Props) {
4544
unpinTooltip,
4645
xAxis,
4746
yAxis,
47+
tooltipThrottle,
4848
} = props;
4949

5050
const isOutsideBounds = React.useCallback(
@@ -96,7 +96,7 @@ export function useChartInnerHandlers(props: Props) {
9696

9797
const throttledHandleMouseMove = IS_TOUCH_ENABLED
9898
? undefined
99-
: throttle(handleMouseMove, THROTTLE_DELAY);
99+
: throttle(handleMouseMove, tooltipThrottle);
100100

101101
const handleMouseLeave: React.MouseEventHandler<SVGSVGElement> = (event) => {
102102
if (tooltipPinned) {
@@ -115,7 +115,7 @@ export function useChartInnerHandlers(props: Props) {
115115
};
116116

117117
const throttledHandleTouchMove = IS_TOUCH_ENABLED
118-
? throttle(handleTouchMove, THROTTLE_DELAY)
118+
? throttle(handleTouchMove, tooltipThrottle)
119119
: undefined;
120120

121121
const handleChartClick = (event: React.MouseEvent<SVGSVGElement>) => {

src/hooks/useChartOptions/tooltip.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export const getPreparedTooltip = (args: {tooltip: ChartData['tooltip']}): Prepa
1010
return {
1111
...tooltip,
1212
enabled: get(tooltip, 'enabled', true),
13+
throttle: tooltip?.throttle ?? 0,
1314
};
1415
};

src/hooks/useChartOptions/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export type PreparedTitle = ChartData['title'] & {
6565

6666
export type PreparedTooltip = ChartData['tooltip'] & {
6767
enabled: boolean;
68+
throttle: number;
6869
};
6970

7071
export type ChartOptions = {

src/types/chart/tooltip.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ export interface ChartTooltip<T = MeaningfulAny> {
9999
enabled?: boolean;
100100
modifierKey?: 'altKey' | 'metaKey';
101101
};
102+
/** Show tooltip at most once per every ```throttle``` milliseconds */
103+
throttle?: number;
102104
}

0 commit comments

Comments
 (0)