Skip to content

Commit eee145c

Browse files
committed
LineChart: fix click event
1 parent c24095f commit eee145c

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

packages/charts/src/components/LineChart/LineChart.tsx

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import { useLabelFormatter } from '../../hooks/useLabelFormatter.js';
1919
import { useLegendItemClick } from '../../hooks/useLegendItemClick.js';
2020
import { useLongestYAxisLabel } from '../../hooks/useLongestYAxisLabel.js';
2121
import { useObserveXAxisHeights } from '../../hooks/useObserveXAxisHeights.js';
22+
import { useOnClickInternal } from '../../hooks/useOnClickInternal.js';
2223
import { usePrepareDimensionsAndMeasures } from '../../hooks/usePrepareDimensionsAndMeasures.js';
2324
import { useTooltipFormatter } from '../../hooks/useTooltipFormatter.js';
24-
import type { IChartBaseProps } from '../../interfaces/IChartBaseProps.js';
25+
import type { ActivePayload, IChartBaseProps } from '../../interfaces/IChartBaseProps.js';
2526
import type { IChartDimension } from '../../interfaces/IChartDimension.js';
2627
import type { IChartMeasure } from '../../interfaces/IChartMeasure.js';
2728
import { ChartContainer } from '../../internal/ChartContainer.js';
@@ -160,7 +161,7 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
160161
dimensionDefaults,
161162
measureDefaults,
162163
);
163-
164+
const activePayloadsRef = useRef<ActivePayload[]>(measures);
164165
const tooltipValueFormatter = useTooltipFormatter(measures);
165166

166167
const primaryDimension = dimensions[0];
@@ -180,8 +181,8 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
180181

181182
const onItemLegendClick = useLegendItemClick(onLegendClick);
182183
const preventOnClickCall = useRef(0);
184+
const handleClick = useOnClickInternal(onClick, dataset, activePayloadsRef);
183185

184-
//todo: check this
185186
const onDataPointClickInternal = useCallback(
186187
(payload, eventOrIndex) => {
187188
if (eventOrIndex.dataKey && typeof onDataPointClick === 'function') {
@@ -195,18 +196,13 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
195196
}),
196197
);
197198
} else if (typeof onClick === 'function' && preventOnClickCall.current === 0) {
198-
onClick(
199-
enrichEventWithDetails(eventOrIndex, {
200-
payload: payload?.activePayload?.[0]?.payload,
201-
activePayloads: payload?.activePayload,
202-
}),
203-
);
199+
handleClick(payload, eventOrIndex);
204200
}
205201
if (preventOnClickCall.current > 0) {
206202
preventOnClickCall.current -= 1;
207203
}
208204
},
209-
[onDataPointClick, preventOnClickCall.current],
205+
[handleClick, onClick, onDataPointClick],
210206
);
211207

212208
const isBigDataSet = dataset?.length > 30;
@@ -299,17 +295,28 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
299295
/>
300296
)}
301297
{measures.map((element, index) => {
298+
const color = element.color ?? `var(--sapChart_OrderedColor_${(index % 12) + 1})`;
299+
const dataKey = element.accessor;
300+
const name = element.label ?? element.accessor;
301+
const opacity = element.opacity ?? 1;
302+
activePayloadsRef.current[index].color = color;
303+
activePayloadsRef.current[index].stroke = color;
304+
activePayloadsRef.current[index].dataKey = dataKey;
305+
activePayloadsRef.current[index].hide = element.hide;
306+
activePayloadsRef.current[index].name = name;
307+
activePayloadsRef.current[index].fillOpacity = opacity;
308+
activePayloadsRef.current[index].strokeOpacity = opacity;
302309
return (
303310
<Line
304311
dot={element.showDot ?? !isBigDataSet}
305312
yAxisId={chartConfig.secondYAxis?.dataKey === element.accessor ? 'right' : 'left'}
306313
key={element.reactKey}
307-
name={element.label ?? element.accessor}
308-
strokeOpacity={element.opacity}
314+
name={name}
315+
strokeOpacity={opacity}
309316
label={isBigDataSet ? false : <ChartDataLabel config={element} chartType="line" position="top" />}
310317
type="monotone"
311-
dataKey={element.accessor}
312-
stroke={element.color ?? `var(--sapChart_OrderedColor_${(index % 12) + 1})`}
318+
dataKey={dataKey}
319+
stroke={color}
313320
strokeWidth={element.width}
314321
activeDot={{ onClick: onDataPointClickInternal }}
315322
isAnimationActive={!noAnimation}
@@ -335,7 +342,6 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
335342
label={referenceLine?.label}
336343
/>
337344
)}
338-
{/*ToDo: remove conditional rendering once `active` is working again (https://github.com/recharts/recharts/issues/2703)*/}
339345
{tooltipConfig?.active !== false && (
340346
<Tooltip
341347
cursor={tooltipFillOpacity}

packages/charts/src/interfaces/IChartBaseProps.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import type { ICartesianChartConfig } from './ICartesianChartConfig.js';
1010
// }
1111

1212
export interface ActivePayload {
13-
color: string;
14-
stroke: string;
13+
color: string | undefined;
14+
stroke: string | undefined;
1515
dataKey: string;
16-
hide?: boolean;
16+
hide?: boolean | undefined;
1717
name: string;
1818
fillOpacity?: string | number;
1919
strokeOpacity?: string | number;

0 commit comments

Comments
 (0)