Skip to content

Commit 249310f

Browse files
authored
Line Chart: expose events handling (#42168)
* detectbounds + events handling * changelog * add story for events
1 parent 50a95d0 commit 249310f

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Expose event handling for line chart

projects/js-packages/charts/src/components/line-chart/line-chart.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ const LineChart: FC< LineChartProps > = ( {
104104
smoothing = true,
105105
renderTooltip = renderDefaultTooltip,
106106
options = {},
107+
onPointerDown = undefined,
108+
onPointerUp = undefined,
109+
onPointerMove = undefined,
110+
onPointerOut = undefined,
107111
} ) => {
108112
const providerTheme = useChartTheme();
109113
const chartId = useId(); // Ensure unique ids for gradient fill.
@@ -177,6 +181,11 @@ const LineChart: FC< LineChartProps > = ( {
177181
// xScale and yScale could be set in Axis as well, but they are `scale` props there.
178182
xScale={ { type: 'time', ...options?.xScale } }
179183
yScale={ { type: 'linear', nice: true, zero: false, ...options?.yScale } }
184+
onPointerDown={ onPointerDown }
185+
onPointerUp={ onPointerUp }
186+
onPointerMove={ onPointerMove }
187+
onPointerOut={ onPointerOut }
188+
pointerEventsDataKey="nearest"
180189
>
181190
<AnimatedGrid columns={ false } numTicks={ 4 } />
182191
<AnimatedAxis
@@ -219,6 +228,7 @@ const LineChart: FC< LineChartProps > = ( {
219228

220229
{ withTooltips && (
221230
<Tooltip
231+
detectBounds
222232
snapTooltipToDatumX
223233
snapTooltipToDatumY
224234
showSeriesGlyphs

projects/js-packages/charts/src/components/line-chart/stories/index.stories.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,10 @@ CustomTooltips.args = {
219219
);
220220
},
221221
};
222+
223+
export const WithPointerEvents: StoryObj< typeof LineChart > = Template.bind( {} );
224+
WithPointerEvents.args = {
225+
...Default.args,
226+
// eslint-disable-next-line no-alert
227+
onPointerDown: ( { datum } ) => alert( 'Pointer down:' + JSON.stringify( datum ) ),
228+
};

projects/js-packages/charts/src/types.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Orientation } from '@visx/axis';
22
import { ScaleType } from '@visx/scale';
3-
import { LineStyles } from '@visx/xychart';
4-
import type { CSSProperties } from 'react';
3+
import { EventHandlerParams, LineStyles } from '@visx/xychart';
4+
import type { CSSProperties, PointerEvent } from 'react';
55

66
type ValueOf< T > = T[ keyof T ];
77

@@ -125,6 +125,22 @@ export type BaseChartProps< T = DataPoint | DataPointDate > = {
125125
bottom?: number;
126126
left?: number;
127127
};
128+
/**
129+
* Callback function for pointer down event
130+
*/
131+
onPointerDown?: ( event: EventHandlerParams< object > ) => void;
132+
/**
133+
* Callback function for pointer down event
134+
*/
135+
onPointerUp?: ( event: EventHandlerParams< object > ) => void;
136+
/**
137+
* Callback function for pointer down event
138+
*/
139+
onPointerMove?: ( event: EventHandlerParams< object > ) => void;
140+
/**
141+
* Callback function for pointer up event
142+
*/
143+
onPointerOut?: ( event: PointerEvent< Element > ) => void;
128144
/**
129145
* Whether to show tooltips on hover. False by default.
130146
*/

0 commit comments

Comments
 (0)