Skip to content

Commit 870fdfd

Browse files
authored
Enabled multi-selection of Controlled legends (#33479)
1 parent 835c01b commit 870fdfd

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Enabled Multi Select behaviour of Controlled legends",
4+
"packageName": "@fluentui/react-charting",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charting/src/components/DeclarativeChart/DeclarativeChart.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ export const DeclarativeChart: React.FunctionComponent<DeclarativeChartProps> =
140140
return (
141141
<LineChart
142142
{...transformPlotlyJsonToScatterChartProps({ data, layout }, false, colorMap, isDarkTheme)}
143-
legendProps={legendProps}
143+
legendProps={{
144+
...legendProps,
145+
canSelectMultipleLegends: true,
146+
selectedLegends: activeLegends,
147+
}}
144148
/>
145149
);
146150
}

packages/charts/react-charting/src/components/LineChart/LineChart.base.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ export class LineChartBase extends React.Component<ILineChartProps, ILineChartSt
188188
refSelected: '',
189189
selectedLegend: props.legendProps?.selectedLegend ?? '',
190190
isCalloutVisible: false,
191-
selectedLegendPoints: [],
191+
selectedLegendPoints: this._injectIndexPropertyInLineChartData(this.props.data.lineChartData, true),
192192
selectedColorBarLegend: [],
193-
isSelectedLegend: false,
193+
isSelectedLegend: (this.props.legendProps?.selectedLegends?.length ?? 0) > 0,
194194
activePoint: '',
195195
nearestCircleToHighlight: null,
196196
activeLine: null,
@@ -379,10 +379,21 @@ export class LineChartBase extends React.Component<ILineChartProps, ILineChartSt
379379
return domainNRangeValue;
380380
};
381381

382-
private _injectIndexPropertyInLineChartData = (lineChartData?: ILineChartPoints[]): LineChartDataWithIndex[] | [] => {
382+
private _injectIndexPropertyInLineChartData = (
383+
lineChartData?: ILineChartPoints[],
384+
isFilterSelectedLegends: boolean = false,
385+
): LineChartDataWithIndex[] | [] => {
383386
const { allowMultipleShapesForPoints = false } = this.props;
384-
return lineChartData
385-
? lineChartData.map((item: ILineChartPoints, index: number) => {
387+
// Apply filter only if isPropChange is true
388+
const filteredData = isFilterSelectedLegends
389+
? lineChartData?.filter(
390+
(item: ILineChartPoints) =>
391+
this.props.legendProps?.selectedLegends?.includes(item.legend) ||
392+
this.props.legendProps?.selectedLegend === item.legend,
393+
)
394+
: lineChartData;
395+
return filteredData
396+
? filteredData.map((item: ILineChartPoints, index: number) => {
386397
let color: string;
387398
// isInverted property is applicable to v8 themes only
388399
if (typeof item.color === 'undefined') {

packages/react-examples/src/react-charting/DeclarativeChart/schema/fluent_line.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,5 @@
436436
"paper_bgcolor": "#F5F6F9"
437437
},
438438
"frames": [],
439-
"selectedLegends": ["Trace 0"]
439+
"selectedLegends": ["Trace 0", "Trace 1"]
440440
}

0 commit comments

Comments
 (0)