Skip to content

Commit 67bc41d

Browse files
Anush2303Anush
andauthored
fix(react-charts): remove duplicated legends in line chart (#34991)
Co-authored-by: Anush <anushgupta@microsoft.com>
1 parent 78fdfc8 commit 67bc41d

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
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": "remove duplicated legends in line chart",
4+
"packageName": "@fluentui/react-charts",
5+
"email": "anushgupta@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charts/library/src/components/LineChart/LineChart.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,31 +341,40 @@ export const LineChart: React.FunctionComponent<LineChartProps> = React.forwardR
341341
function _createLegends(data: LineChartDataWithIndex[]): JSXElement {
342342
const { legendProps, allowMultipleShapesForPoints = false } = props;
343343
const isLegendMultiSelectEnabled = !!(legendProps && !!legendProps.canSelectMultipleLegends);
344-
const legendDataItems = data.map((point: LineChartDataWithIndex) => {
345-
const color: string = point.color!;
344+
const mapLegendToPoints: Record<string, LineChartDataWithIndex[]> = {};
345+
data.forEach((point: LineChartDataWithIndex) => {
346+
if (point.legend) {
347+
if (!mapLegendToPoints[point.legend]) {
348+
mapLegendToPoints[point.legend] = [];
349+
}
350+
mapLegendToPoints[point.legend].push(point);
351+
}
352+
});
353+
const legendDataItems: Legend[] = Object.entries(mapLegendToPoints).map(([legendTitle, points]) => {
354+
const representativePoint = points[0];
346355
// mapping data to the format Legends component needs
347356
const legend: Legend = {
348-
title: point.legend!,
349-
color,
357+
title: legendTitle,
358+
color: representativePoint.color!,
350359
action: () => {
351360
if (isLegendMultiSelectEnabled) {
352-
_handleMultipleLineLegendSelectionAction(point);
361+
points.forEach(p => _handleMultipleLineLegendSelectionAction(p));
353362
} else {
354-
_handleSingleLegendSelectionAction(point);
363+
points.forEach(p => _handleSingleLegendSelectionAction(p));
355364
}
356365
},
357366
onMouseOutAction: () => {
358367
setActiveLegend('');
359368
},
360369
hoverAction: () => {
361370
_handleChartMouseLeave();
362-
setActiveLegend(point.legend);
371+
setActiveLegend(legendTitle);
363372
},
364-
...(point.legendShape && {
365-
shape: point.legendShape,
373+
...(representativePoint.legendShape && {
374+
shape: representativePoint.legendShape,
366375
}),
367376
...(allowMultipleShapesForPoints && {
368-
shape: Points[point.index % Object.keys(pointTypes).length] as Legend['shape'],
377+
shape: Points[representativePoint.index % Object.keys(pointTypes).length] as Legend['shape'],
369378
}),
370379
};
371380
return legend;

0 commit comments

Comments
 (0)