Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/lib/components/chartlegend/ChartLegendWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ export type ChartLegendWrapperProps = {
colorSet:
| Record<string, ChartColors | string>
| ((seriesNames: string[]) => Record<string, ChartColors | string>);
sortOrder?: 'alphabetical' | 'status' | ((a: string, b: string) => number);
};

export const ChartLegendWrapper = ({
children,
colorSet,
sortOrder = 'alphabetical',
}: ChartLegendWrapperProps) => {
const [registeredColorSets, setRegisteredColorSets] = useState<
Record<string, string[]>
Expand Down Expand Up @@ -133,8 +135,20 @@ export const ChartLegendWrapper = ({
);

const listResources = useCallback(() => {
return Object.keys(internalColorSet).sort();
}, [internalColorSet]);
const resources = Object.keys(internalColorSet);

if (sortOrder === 'alphabetical') {
return resources.sort((a, b) => a.localeCompare(b));
} else if (sortOrder === 'status') {
return ['Success', 'Warning', 'Failed'].filter((status) =>
resources.includes(status),
);
} else if (typeof sortOrder === 'function') {
return resources.sort(sortOrder);
}

return resources;
}, [internalColorSet, sortOrder]);

const chartLegendState = useMemo(
() => ({
Expand Down
19 changes: 19 additions & 0 deletions stories/BarChart/barchart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const Playground: Story = {
Success: theme.statusHealthy,
Failed: theme.statusCritical,
}}
sortOrder="status"
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand Down Expand Up @@ -123,6 +124,7 @@ export const Time7Days: Story = {
Success: theme.statusHealthy,
Failed: theme.statusCritical,
}}
sortOrder="status"
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand Down Expand Up @@ -408,6 +410,7 @@ export const Stacked: Story = {
Failed: theme.statusCritical,
Warning: theme.statusWarning,
}}
sortOrder="status"
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand Down Expand Up @@ -464,6 +467,7 @@ export const DefaultSort: Story = {
Success: theme.statusHealthy,
Failed: theme.statusCritical,
}}
sortOrder="status"
>
<Barchart
type={{ type: 'category' }}
Expand Down Expand Up @@ -542,6 +546,7 @@ export const WithCustomTooltip: Story = {
Success: theme.statusHealthy,
Failed: theme.statusCritical,
}}
sortOrder="status"
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand Down Expand Up @@ -614,6 +619,14 @@ export const StatusColors: Story = {
'Failed Requests': theme.statusCritical,
'Warning Events': theme.statusWarning,
}}
sortOrder={(a, b) => {
const statusOrder: Record<string, number> = {
'Success Rate': 0,
'Warning Events': 1,
'Failed Requests': 2,
};
return statusOrder[a] - statusOrder[b];
}}
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand Down Expand Up @@ -668,6 +681,7 @@ export const LegendShapes: Story = {
Failed: theme.statusCritical,
Warning: theme.statusWarning,
}}
sortOrder="status"
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand All @@ -685,6 +699,7 @@ export const LegendShapes: Story = {
Failed: theme.statusCritical,
Warning: theme.statusWarning,
}}
sortOrder="status"
>
<Stack direction="vertical" gap="r16">
<Barchart
Expand Down Expand Up @@ -740,6 +755,7 @@ export const BarchartsWithSingleLegend: Story = {
Failed: theme.statusCritical,
Warning: theme.statusWarning,
}}
sortOrder="status"
>
<Barchart
type={{ type: 'category' }}
Expand Down Expand Up @@ -770,6 +786,7 @@ export const ErrorState: Story = {
Success: theme.statusHealthy,
Failed: theme.statusCritical,
}}
sortOrder="status"
>
<Barchart
type={{ type: 'category' }}
Expand Down Expand Up @@ -821,6 +838,7 @@ export const StackedBarSort: Story = {
Warning: theme.statusWarning,
Failed: theme.statusCritical,
}}
sortOrder="status"
>
<Barchart
type={{ type: 'category' }}
Expand Down Expand Up @@ -896,6 +914,7 @@ export const CompleteExample: Story = {
Success: 'lineColor1',
Failed: 'lineColor2',
}}
sortOrder="status"
>
<Barchart
type={{ type: 'category' }}
Expand Down
Loading