Skip to content

Commit 14b429e

Browse files
committed
Merge branch 'bugfix/fix-the-legend-order-issue' into q/1.0
2 parents aeadb0b + 28eb42c commit 14b429e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/lib/components/chartlegend/ChartLegendWrapper.tsx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ export type ChartLegendWrapperProps = {
4141
colorSet:
4242
| Record<string, ChartColors | string>
4343
| ((seriesNames: string[]) => Record<string, ChartColors | string>);
44+
sortOrder?: 'alphabetical' | 'status' | ((a: string, b: string) => number);
4445
};
4546

4647
export const ChartLegendWrapper = ({
4748
children,
4849
colorSet,
50+
sortOrder = 'alphabetical',
4951
}: ChartLegendWrapperProps) => {
5052
const [registeredColorSets, setRegisteredColorSets] = useState<
5153
Record<string, string[]>
@@ -133,8 +135,20 @@ export const ChartLegendWrapper = ({
133135
);
134136

135137
const listResources = useCallback(() => {
136-
return Object.keys(internalColorSet).sort();
137-
}, [internalColorSet]);
138+
const resources = Object.keys(internalColorSet);
139+
140+
if (sortOrder === 'alphabetical') {
141+
return resources.sort((a, b) => a.localeCompare(b));
142+
} else if (sortOrder === 'status') {
143+
return ['Success', 'Warning', 'Failed'].filter((status) =>
144+
resources.includes(status),
145+
);
146+
} else if (typeof sortOrder === 'function') {
147+
return resources.sort(sortOrder);
148+
}
149+
150+
return resources;
151+
}, [internalColorSet, sortOrder]);
138152

139153
const chartLegendState = useMemo(
140154
() => ({

stories/BarChart/barchart.stories.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const Playground: Story = {
5959
Success: theme.statusHealthy,
6060
Failed: theme.statusCritical,
6161
}}
62+
sortOrder="status"
6263
>
6364
<Stack direction="vertical" gap="r16">
6465
<Barchart
@@ -123,6 +124,7 @@ export const Time7Days: Story = {
123124
Success: theme.statusHealthy,
124125
Failed: theme.statusCritical,
125126
}}
127+
sortOrder="status"
126128
>
127129
<Stack direction="vertical" gap="r16">
128130
<Barchart
@@ -408,6 +410,7 @@ export const Stacked: Story = {
408410
Failed: theme.statusCritical,
409411
Warning: theme.statusWarning,
410412
}}
413+
sortOrder="status"
411414
>
412415
<Stack direction="vertical" gap="r16">
413416
<Barchart
@@ -464,6 +467,7 @@ export const DefaultSort: Story = {
464467
Success: theme.statusHealthy,
465468
Failed: theme.statusCritical,
466469
}}
470+
sortOrder="status"
467471
>
468472
<Barchart
469473
type={{ type: 'category' }}
@@ -542,6 +546,7 @@ export const WithCustomTooltip: Story = {
542546
Success: theme.statusHealthy,
543547
Failed: theme.statusCritical,
544548
}}
549+
sortOrder="status"
545550
>
546551
<Stack direction="vertical" gap="r16">
547552
<Barchart
@@ -614,6 +619,14 @@ export const StatusColors: Story = {
614619
'Failed Requests': theme.statusCritical,
615620
'Warning Events': theme.statusWarning,
616621
}}
622+
sortOrder={(a, b) => {
623+
const statusOrder: Record<string, number> = {
624+
'Success Rate': 0,
625+
'Warning Events': 1,
626+
'Failed Requests': 2,
627+
};
628+
return statusOrder[a] - statusOrder[b];
629+
}}
617630
>
618631
<Stack direction="vertical" gap="r16">
619632
<Barchart
@@ -668,6 +681,7 @@ export const LegendShapes: Story = {
668681
Failed: theme.statusCritical,
669682
Warning: theme.statusWarning,
670683
}}
684+
sortOrder="status"
671685
>
672686
<Stack direction="vertical" gap="r16">
673687
<Barchart
@@ -685,6 +699,7 @@ export const LegendShapes: Story = {
685699
Failed: theme.statusCritical,
686700
Warning: theme.statusWarning,
687701
}}
702+
sortOrder="status"
688703
>
689704
<Stack direction="vertical" gap="r16">
690705
<Barchart
@@ -740,6 +755,7 @@ export const BarchartsWithSingleLegend: Story = {
740755
Failed: theme.statusCritical,
741756
Warning: theme.statusWarning,
742757
}}
758+
sortOrder="status"
743759
>
744760
<Barchart
745761
type={{ type: 'category' }}
@@ -770,6 +786,7 @@ export const ErrorState: Story = {
770786
Success: theme.statusHealthy,
771787
Failed: theme.statusCritical,
772788
}}
789+
sortOrder="status"
773790
>
774791
<Barchart
775792
type={{ type: 'category' }}
@@ -821,6 +838,7 @@ export const StackedBarSort: Story = {
821838
Warning: theme.statusWarning,
822839
Failed: theme.statusCritical,
823840
}}
841+
sortOrder="status"
824842
>
825843
<Barchart
826844
type={{ type: 'category' }}
@@ -896,6 +914,7 @@ export const CompleteExample: Story = {
896914
Success: 'lineColor1',
897915
Failed: 'lineColor2',
898916
}}
917+
sortOrder="status"
899918
>
900919
<Barchart
901920
type={{ type: 'category' }}

0 commit comments

Comments
 (0)