Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 💄 Add new chart event onDataPointHover that triggers only on data point change #4008

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/web/components/chart/historical-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const HistoricalChart = memo((props: HistoricalChartProps) => {
data,
},
]}
onCrosshairMove={(params) => {
onDataPointHover={(params) => {
if (params.seriesData.size > 0) {
const [data] = [...params.seriesData.values()] as AreaData[];

Expand Down
2 changes: 1 addition & 1 deletion packages/web/components/chart/historical-volume-chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const HistoricalVolumeChart = memo(
data,
},
]}
onCrosshairMove={(params) => {
onDataPointHover={(params) => {
if (params.seriesData.size > 0) {
const [data] = [...params.seriesData.values()] as AreaData[];

Expand Down
19 changes: 18 additions & 1 deletion packages/web/components/chart/light-weight-charts/chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import React, {
memo,
PropsWithChildren,
useCallback,
useEffect,
useRef,
useState,
Expand Down Expand Up @@ -196,6 +197,7 @@ interface ChartProps<T extends TimeChartOptions, K extends Time> {
params: ChartControllerParams<TimeChartOptions, K>
) => ChartController<TimeChartOptions, K>;
onCrosshairMove?: (params: MouseEventParams<K>) => void;
onDataPointHover?: (params: MouseEventParams<K>) => void;
}

export const Chart = memo(
Expand All @@ -207,10 +209,25 @@ export const Chart = memo(
children,
series,
onCrosshairMove,
onDataPointHover,
Controller,
} = props;
const [container, setContainer] = useState<HTMLDivElement | null>(null);
const chart = useRef<ChartController<TimeChartOptions, K>>();
const lastHoverDataPointLogicalIndex =
useRef<MouseEventParams<K>["logical"]>(undefined);

const onCrosshairMoveInternal = useCallback(
(params: MouseEventParams<K>) => {
onCrosshairMove?.(params);

if (lastHoverDataPointLogicalIndex.current !== params.logical) {
onDataPointHover?.(params);
}
lastHoverDataPointLogicalIndex.current = params.logical;
},
[onCrosshairMove, onDataPointHover]
);

useSyncExternalStore(
resizeSubscribe,
Expand All @@ -228,7 +245,7 @@ export const Chart = memo(
},
series,
container,
onCrosshairMove,
onCrosshairMove: onCrosshairMoveInternal,
});
}

Expand Down
Loading