Skip to content

Commit d40ec3d

Browse files
Anush2303Anush
andauthored
fix(react-charts): update segment color in donut chart (microsoft#35122)
Co-authored-by: Anush <[email protected]>
1 parent 3aaa8a9 commit d40ec3d

File tree

3 files changed

+82
-47
lines changed

3 files changed

+82
-47
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": "update segment colors of donut chart",
4+
"packageName": "@fluentui/react-charts",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

packages/charts/react-charts/library/src/components/DeclarativeChart/PlotlySchemaAdapter.ts

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -497,26 +497,54 @@ export const transformPlotlyJsonToDonutProps = (
497497
isDarkTheme,
498498
);
499499
const mapLegendToDataPoint: Record<string, ChartDataPoint> = {};
500-
firstData.labels?.forEach((label, index: number) => {
501-
const value = getNumberAtIndexOrDefault(firstData.values, index);
502-
if (isInvalidValue(value) || (value as number) < 0) {
503-
return;
504-
}
500+
if (colors) {
501+
firstData.labels?.forEach((label, index: number) => {
502+
const value = getNumberAtIndexOrDefault(firstData.values, index);
503+
if (isInvalidValue(value) || (value as number) < 0) {
504+
return;
505+
}
505506

506-
const legend = `${label}`;
507-
// resolve color for each legend from the extracted colors
508-
const color: string = resolveColor(colors, index, legend, colorMap, isDarkTheme);
507+
const legend = `${label}`;
508+
// resolve color for each legend from the extracted colors
509+
const color: string = resolveColor(colors, index, legend, colorMap, isDarkTheme);
509510

510-
if (!mapLegendToDataPoint[legend]) {
511-
mapLegendToDataPoint[legend] = {
512-
legend,
513-
data: value,
514-
color,
515-
};
516-
} else {
517-
mapLegendToDataPoint[legend].data! += value as number;
511+
if (!mapLegendToDataPoint[legend]) {
512+
mapLegendToDataPoint[legend] = {
513+
legend,
514+
data: value,
515+
color,
516+
};
517+
} else {
518+
mapLegendToDataPoint[legend].data! += value as number;
519+
}
520+
});
521+
} else {
522+
// Sort labels by value descending before mapping
523+
if (firstData.labels && firstData.values) {
524+
const labelValuePairs = firstData.labels.map((label, index) => ({
525+
label,
526+
value: getNumberAtIndexOrDefault(firstData.values, index),
527+
index,
528+
}));
529+
// Filter out invalid values
530+
const validPairs = labelValuePairs.filter(pair => !isInvalidValue(pair.value));
531+
// Sort descending by value
532+
validPairs.sort((a, b) => (b.value as number) - (a.value as number));
533+
validPairs.forEach((pair, sortedIdx) => {
534+
const legend = `${pair.label}`;
535+
const color: string = resolveColor(colors, sortedIdx, legend, colorMap, isDarkTheme);
536+
if (!mapLegendToDataPoint[legend]) {
537+
mapLegendToDataPoint[legend] = {
538+
legend,
539+
data: pair.value,
540+
color,
541+
};
542+
} else {
543+
mapLegendToDataPoint[legend].data! += pair.value as number;
544+
}
545+
});
518546
}
519-
});
547+
}
520548

521549
const width: number = input.layout?.width ?? 440;
522550
const height: number = input.layout?.height ?? 220;

packages/charts/react-charts/library/src/components/DeclarativeChart/__snapshots__/PlotlySchemaAdapterUT.test.tsx.snap

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,103 +87,103 @@ Object {
8787
"chartData": Array [
8888
Object {
8989
"color": "#57811b",
90-
"data": 1,
91-
"legend": "AMC",
90+
"data": 7,
91+
"legend": "Merc",
9292
},
9393
Object {
9494
"color": "#9373c0",
95-
"data": 1,
96-
"legend": "Cadillac",
95+
"data": 2,
96+
"legend": "Fiat",
9797
},
9898
Object {
9999
"color": "#ca5010",
100-
"data": 1,
101-
"legend": "Camaro",
100+
"data": 2,
101+
"legend": "Hornet",
102102
},
103103
Object {
104104
"color": "#3a96dd",
105-
"data": 1,
106-
"legend": "Chrysler",
105+
"data": 2,
106+
"legend": "Mazda",
107107
},
108108
Object {
109109
"color": "#e3008c",
110-
"data": 1,
111-
"legend": "Datsun",
110+
"data": 2,
111+
"legend": "Toyota",
112112
},
113113
Object {
114114
"color": "#13a10e",
115115
"data": 1,
116-
"legend": "Dodge",
116+
"legend": "AMC",
117117
},
118118
Object {
119119
"color": "#b146c2",
120120
"data": 1,
121-
"legend": "Duster",
121+
"legend": "Cadillac",
122122
},
123123
Object {
124124
"color": "#ae8c00",
125125
"data": 1,
126-
"legend": "Ferrari",
126+
"legend": "Camaro",
127127
},
128128
Object {
129129
"color": "#93a4f4",
130-
"data": 2,
131-
"legend": "Fiat",
130+
"data": 1,
131+
"legend": "Chrysler",
132132
},
133133
Object {
134134
"color": "#ee5fb7",
135135
"data": 1,
136-
"legend": "Ford",
136+
"legend": "Datsun",
137137
},
138138
Object {
139139
"color": "#4cb4b7",
140140
"data": 1,
141-
"legend": "Honda",
141+
"legend": "Dodge",
142142
},
143143
Object {
144144
"color": "#a083c9",
145-
"data": 2,
146-
"legend": "Hornet",
145+
"data": 1,
146+
"legend": "Duster",
147147
},
148148
Object {
149149
"color": "#27ac22",
150150
"data": 1,
151-
"legend": "Lincoln",
151+
"legend": "Ferrari",
152152
},
153153
Object {
154154
"color": "#4fa1e1",
155155
"data": 1,
156-
"legend": "Lotus",
156+
"legend": "Ford",
157157
},
158158
Object {
159159
"color": "#d77440",
160160
"data": 1,
161-
"legend": "Maserati",
161+
"legend": "Honda",
162162
},
163163
Object {
164164
"color": "#73aa24",
165-
"data": 2,
166-
"legend": "Mazda",
165+
"data": 1,
166+
"legend": "Lincoln",
167167
},
168168
Object {
169169
"color": "#c36bd1",
170-
"data": 7,
171-
"legend": "Merc",
170+
"data": 1,
171+
"legend": "Lotus",
172172
},
173173
Object {
174174
"color": "#d0b232",
175175
"data": 1,
176-
"legend": "Pontiac",
176+
"legend": "Maserati",
177177
},
178178
Object {
179179
"color": "#4f6bed",
180180
"data": 1,
181-
"legend": "Porsche",
181+
"legend": "Pontiac",
182182
},
183183
Object {
184184
"color": "#ea38a6",
185-
"data": 2,
186-
"legend": "Toyota",
185+
"data": 1,
186+
"legend": "Porsche",
187187
},
188188
Object {
189189
"color": "#038387",

0 commit comments

Comments
 (0)