Skip to content

Commit 1fc1fd8

Browse files
committed
feat: add legendSort in legendSection
1 parent 798b7a6 commit 1fc1fd8

20 files changed

Lines changed: 732 additions & 237 deletions

File tree

‎superset-frontend/plugins/plugin-chart-echarts/src/Bubble/transformProps.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export default function transformProps(chartProps: EchartsBubbleChartProps) {
128128
legendOrientation,
129129
legendMargin,
130130
legendType,
131+
legendSort,
131132
sliceId,
132133
}: EchartsBubbleFormData = { ...DEFAULT_FORM_DATA, ...formData };
133134
const colorFn = CategoricalColorNamespace.getScale(colorScheme as string);
@@ -230,7 +231,10 @@ export default function transformProps(chartProps: EchartsBubbleChartProps) {
230231
},
231232
legend: {
232233
...getLegendProps(legendType, legendOrientation, showLegend, theme),
233-
data: Array.from(legends),
234+
data: Array.from(legends).sort((a: string, b: string) => {
235+
if (!legendSort) return 0;
236+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
237+
}),
234238
},
235239
tooltip: {
236240
show: !inContextMenu,

‎superset-frontend/plugins/plugin-chart-echarts/src/Funnel/transformProps.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export default function transformProps(
112112
legendMargin,
113113
legendOrientation,
114114
legendType,
115+
legendSort,
115116
metric = '',
116117
numberFormat,
117118
currencyFormat,
@@ -290,7 +291,10 @@ export default function transformProps(
290291
},
291292
legend: {
292293
...getLegendProps(legendType, legendOrientation, showLegend, theme),
293-
data: keys,
294+
data: keys.sort((a: string, b: string) => {
295+
if (!legendSort) return 0;
296+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
297+
}),
294298
},
295299
series,
296300
};

‎superset-frontend/plugins/plugin-chart-echarts/src/Gantt/transformProps.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ export default function transformProps(chartProps: EchartsGanttChartProps) {
131131
legendMargin,
132132
legendOrientation,
133133
legendType,
134+
legendSort,
134135
showLegend,
135136
yAxisTitle,
136137
yAxisTitleMargin,
@@ -330,6 +331,18 @@ export default function transformProps(chartProps: EchartsGanttChartProps) {
330331
},
331332
);
332333

334+
const legendData = series
335+
.map(entry => {
336+
const { name } = entry;
337+
if (name === null || name === undefined) return '';
338+
return String(name);
339+
})
340+
.filter(name => name !== '')
341+
.sort((a, b) => {
342+
if (!legendSort) return 0;
343+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
344+
});
345+
333346
const tooltipFormatterMap = {
334347
[GenericDataType.Numeric]: tooltipValuesFormatter,
335348
[GenericDataType.String]: undefined,
@@ -365,6 +378,7 @@ export default function transformProps(chartProps: EchartsGanttChartProps) {
365378
zoomable,
366379
legendState,
367380
),
381+
data: legendData,
368382
},
369383
grid: {
370384
...defaultGrid,

‎superset-frontend/plugins/plugin-chart-echarts/src/Graph/transformProps.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export default function transformProps(
188188
legendMargin,
189189
legendOrientation,
190190
legendType,
191+
legendSort,
191192
showLegend,
192193
baseEdgeWidth,
193194
baseNodeSize,
@@ -353,7 +354,10 @@ export default function transformProps(
353354
},
354355
legend: {
355356
...getLegendProps(legendType, legendOrientation, showLegend, theme),
356-
data: categoryList,
357+
data: categoryList.sort((a: string, b: string) => {
358+
if (!legendSort) return 0;
359+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
360+
}),
357361
},
358362
series,
359363
};

‎superset-frontend/plugins/plugin-chart-echarts/src/MixedTimeseries/transformProps.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export default function transformProps(
158158
contributionMode,
159159
legendOrientation,
160160
legendType,
161+
legendSort,
161162
logAxis,
162163
logAxisSecondary,
163164
markerEnabled,
@@ -720,7 +721,11 @@ export default function transformProps(
720721
ForecastSeriesEnum.Observation,
721722
)
722723
.map(entry => entry.id || entry.name || '')
723-
.concat(extractAnnotationLabels(annotationLayers)),
724+
.concat(extractAnnotationLabels(annotationLayers))
725+
.sort((a: string, b: string) => {
726+
if (!legendSort) return 0;
727+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
728+
}),
724729
},
725730
series: dedupSeries(reorderForecastSeries(series) as SeriesOption[]),
726731
toolbox: {

‎superset-frontend/plugins/plugin-chart-echarts/src/Pie/controlPanel.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
sharedControls,
3030
} from '@superset-ui/chart-controls';
3131
import { DEFAULT_FORM_DATA } from './types';
32-
import { legendSection, legendSortControl } from '../controls';
32+
import { legendSection } from '../controls';
3333

3434
const {
3535
donut,
@@ -119,7 +119,6 @@ const config: ControlPanelConfig = {
119119
},
120120
],
121121
...legendSection,
122-
[legendSortControl],
123122
// eslint-disable-next-line react/jsx-key
124123
[<ControlSubSectionHeader>{t('Labels')}</ControlSubSectionHeader>],
125124
[

‎superset-frontend/plugins/plugin-chart-echarts/src/Pie/types.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export type EchartsPieFormData = QueryFormData &
4040
labelType: EchartsPieLabelType;
4141
labelTemplate: string | null;
4242
labelsOutside: boolean;
43-
legendSort: 'asc' | 'desc' | null;
4443
metric?: string;
4544
outerRadius: number;
4645
showLabels: boolean;

‎superset-frontend/plugins/plugin-chart-echarts/src/Radar/transformProps.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ export default function transformProps(
116116
dateFormat,
117117
showLabels,
118118
showLegend,
119+
legendSort,
119120
isCircle,
120121
columnConfig,
121122
sliceId,
@@ -354,7 +355,10 @@ export default function transformProps(
354355
},
355356
legend: {
356357
...getLegendProps(legendType, legendOrientation, showLegend, theme),
357-
data: Array.from(columnsLabelMap.keys()),
358+
data: Array.from(columnsLabelMap.keys()).sort((a: string, b: string) => {
359+
if (!legendSort) return 0;
360+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
361+
}),
358362
},
359363
series,
360364
radar: {

‎superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/transformProps.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export default function transformProps(
149149
legendOrientation,
150150
legendType,
151151
legendMargin,
152+
legendSort,
152153
logAxis,
153154
markerEnabled,
154155
markerSize,
@@ -646,7 +647,10 @@ export default function transformProps(
646647
padding,
647648
),
648649
scrollDataIndex: legendIndex || 0,
649-
data: legendData as string[],
650+
data: legendData.sort((a: string, b: string) => {
651+
if (!legendSort) return 0;
652+
return legendSort === 'asc' ? a.localeCompare(b) : b.localeCompare(a);
653+
}) as string[],
650654
},
651655
series: dedupSeries(reorderForecastSeries(series) as SeriesOption[]),
652656
toolbox: {

‎superset-frontend/plugins/plugin-chart-echarts/src/constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export const DEFAULT_LEGEND_FORM_DATA: LegendFormData = {
103103
legendOrientation: LegendOrientation.Top,
104104
legendType: LegendType.Scroll,
105105
showLegend: true,
106+
legendSort: null,
106107
};
107108

108109
export const DEFAULT_TITLE_FORM_DATA: TitleFormData = {

0 commit comments

Comments
 (0)