From 77af2a891ef26ec2836654ea2ac3850c3689bc50 Mon Sep 17 00:00:00 2001 From: Anush Gupta <74965306+Anush2303@users.noreply.github.com> Date: Tue, 24 Dec 2024 10:15:11 +0000 Subject: [PATCH] Edit line chart based on legend selection --- .../components/LineChart/LineChart.base.tsx | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/charts/react-charting/src/components/LineChart/LineChart.base.tsx b/packages/charts/react-charting/src/components/LineChart/LineChart.base.tsx index c0dcab6b52dc41..59b61bbe4cff6f 100644 --- a/packages/charts/react-charting/src/components/LineChart/LineChart.base.tsx +++ b/packages/charts/react-charting/src/components/LineChart/LineChart.base.tsx @@ -2,7 +2,7 @@ import * as React from 'react'; import { Axis as D3Axis } from 'd3-axis'; import { select as d3Select, pointer } from 'd3-selection'; import { bisector } from 'd3-array'; -import { ILegend, Legends } from '../Legends/index'; +import { ILegend, ILegendsProps, Legends } from '../Legends/index'; import { line as d3Line, curveLinear as d3curveLinear } from 'd3-shape'; import { classNamesFunction, @@ -188,6 +188,29 @@ export class LineChartBase extends React.Component; + public static getDerivedStateFromProps( + nextProps: Readonly, + prevState: Readonly, + ): Partial | null { + if (nextProps.legendProps?.selectedLegends?.length! > 0) { + const { selectedLegends = [] } = (nextProps.legendProps! as ILegendsProps) || {}; + return { + ...prevState, + selectedLegendPoints: new LineChartBase(nextProps)._injectIndexPropertyInLineChartData( + nextProps.data.lineChartData, + Array.isArray(selectedLegends) && selectedLegends?.some(legend => legend.trim().length > 0), + ), + }; + } else if (nextProps.legendProps?.selectedLegend !== undefined) { + const { selectedLegend = undefined } = (nextProps.legendProps! as ILegendsProps) || {}; + return { + ...prevState, + selectedLegend, + }; + } + return prevState; + } + constructor(props: ILineChartProps) { super(props);