Skip to content

Commit

Permalink
feat(plugin-chart-echarts): add series sorting (#23392)
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro committed Mar 16, 2023
1 parent da3791a commit 0c454c6
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
onlyTotalControl,
showValueControl,
richTooltipSection,
seriesOrderSection,
} from '../../controls';
import { AreaChartExtraControlsOptions } from '../../constants';

Expand Down Expand Up @@ -62,6 +63,7 @@ const config: ControlPanelConfig = {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
...seriesOrderSection,
['color_scheme'],
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
import {
legendSection,
richTooltipSection,
seriesOrderSection,
showValueSection,
} from '../../../controls';

Expand Down Expand Up @@ -301,6 +302,7 @@ const config: ControlPanelConfig = {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
...seriesOrderSection,
['color_scheme'],
...showValueSection,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
import {
legendSection,
richTooltipSection,
seriesOrderSection,
showValueSection,
} from '../../../controls';

Expand Down Expand Up @@ -64,6 +65,7 @@ const config: ControlPanelConfig = {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
...seriesOrderSection,
['color_scheme'],
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import {
legendSection,
richTooltipSection,
seriesOrderSection,
showValueSection,
} from '../../../controls';

Expand All @@ -60,6 +61,7 @@ const config: ControlPanelConfig = {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
...seriesOrderSection,
['color_scheme'],
...showValueSection,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import {
legendSection,
richTooltipSection,
seriesOrderSection,
showValueSectionWithoutStack,
} from '../../../controls';

Expand All @@ -60,6 +61,7 @@ const config: ControlPanelConfig = {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
...seriesOrderSection,
['color_scheme'],
...showValueSectionWithoutStack,
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { DEFAULT_FORM_DATA, TIME_SERIES_DESCRIPTION_TEXT } from '../constants';
import {
legendSection,
richTooltipSection,
seriesOrderSection,
showValueSection,
} from '../../controls';

Expand Down Expand Up @@ -60,6 +61,7 @@ const config: ControlPanelConfig = {
label: t('Chart Options'),
expanded: true,
controlSetRows: [
...seriesOrderSection,
['color_scheme'],
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import {
} from './types';
import {
DEFAULT_LEGEND_FORM_DATA,
DEFAULT_SORT_SERIES_DATA,
DEFAULT_TITLE_FORM_DATA,
} from '../constants';

// @ts-ignore
export const DEFAULT_FORM_DATA: EchartsTimeseriesFormData = {
...DEFAULT_LEGEND_FORM_DATA,
...DEFAULT_TITLE_FORM_DATA,
...DEFAULT_SORT_SERIES_DATA,
annotationLayers: sections.annotationLayers,
area: false,
forecastEnabled: sections.FORECAST_DEFAULT_DATA.forecastEnabled,
Expand Down Expand Up @@ -63,6 +65,8 @@ export const DEFAULT_FORM_DATA: EchartsTimeseriesFormData = {
onlyTotal: false,
percentageThreshold: 0,
orientation: OrientationType.vertical,
sort_series_type: 'sum',
sort_series_ascending: false,
};

export const TIME_SERIES_DESCRIPTION_TEXT: string = t(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export default function transformProps(
showLegend,
showValue,
sliceId,
sortSeriesType,
sortSeriesAscending,
timeGrainSqla,
timeCompare,
stack,
Expand Down Expand Up @@ -197,6 +199,8 @@ export default function transformProps(
stack,
totalStackedValues,
isHorizontal,
sortSeriesType,
sortSeriesAscending,
});
const showValueIndexes = extractShowValueIndexes(rawSeries, {
stack,
Expand Down Expand Up @@ -418,7 +422,7 @@ export default function transformProps(
forecastValue.sort((a, b) => b.data[yIndex] - a.data[yIndex]);
}

const rows: Array<string> = [`${tooltipFormatter(xValue)}`];
const rows: string[] = [];
const forecastValues: Record<string, ForecastValue> =
extractForecastValuesFromTooltipParams(forecastValue, isHorizontal);

Expand All @@ -435,6 +439,10 @@ export default function transformProps(
rows.push(`<span style="opacity: 0.7">${content}</span>`);
}
});
if (stack) {
rows.reverse();
}
rows.unshift(`${tooltipFormatter(xValue)}`);
return rows.join('<br />');
},
},
Expand Down
11 changes: 9 additions & 2 deletions superset-frontend/plugins/plugin-chart-echarts/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
import { JsonValue, t, TimeGranularity } from '@superset-ui/core';
import { ReactNode } from 'react';
import {
LegendFormData,
TitleFormData,
LabelPositionEnum,
LegendFormData,
LegendOrientation,
LegendType,
SortSeriesData,
SortSeriesType,
TitleFormData,
} from './types';

// eslint-disable-next-line import/prefer-default-export
Expand Down Expand Up @@ -114,3 +116,8 @@ export const TOOLTIP_POINTER_MARGIN = 10;
// If no satisfactory position can be found, how far away
// from the edge of the window should the tooltip be kept
export const TOOLTIP_OVERFLOW_MARGIN = 5;

export const DEFAULT_SORT_SERIES_DATA: SortSeriesData = {
sort_series_type: SortSeriesType.Sum,
sort_series_ascending: false,
};
44 changes: 43 additions & 1 deletion superset-frontend/plugins/plugin-chart-echarts/src/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ import {
ControlSetRow,
sharedControls,
} from '@superset-ui/chart-controls';
import { DEFAULT_LEGEND_FORM_DATA } from './constants';
import {
DEFAULT_LEGEND_FORM_DATA,
DEFAULT_SORT_SERIES_DATA,
} from './constants';
import { DEFAULT_FORM_DATA } from './Timeseries/constants';
import { SortSeriesType } from './types';

const { legendMargin, legendOrientation, legendType, showLegend } =
DEFAULT_LEGEND_FORM_DATA;
Expand Down Expand Up @@ -212,3 +216,41 @@ export const richTooltipSection: ControlSetRow[] = [
[tooltipSortByMetricControl],
[tooltipTimeFormatControl],
];

const sortSeriesType: ControlSetItem = {
name: 'sort_series_type',
config: {
type: 'SelectControl',
freeForm: false,
label: t('Sort Series By'),
choices: [
[SortSeriesType.Name, t('Category name')],
[SortSeriesType.Sum, t('Total value')],
[SortSeriesType.Min, t('Minimum value')],
[SortSeriesType.Max, t('Maximum value')],
[SortSeriesType.Avg, t('Average value')],
],
default: DEFAULT_SORT_SERIES_DATA.sort_series_type,
renderTrigger: true,
description: t(
'Based on what should series be ordered on the chart and legend',
),
},
};

const sortSeriesAscending: ControlSetItem = {
name: 'sort_series_ascending',
config: {
type: 'CheckboxControl',
label: t('Sort Series Ascending'),
default: DEFAULT_SORT_SERIES_DATA.sort_series_ascending,
renderTrigger: true,
description: t('Sort series in ascending order'),
},
};

export const seriesOrderSection: ControlSetRow[] = [
[<div className="section-header">{t('Series Order')}</div>],
[sortSeriesType],
[sortSeriesAscending],
];
13 changes: 13 additions & 0 deletions superset-frontend/plugins/plugin-chart-echarts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,17 @@ export interface TreePathInfo {
value: number | number[];
}

export enum SortSeriesType {
Name = 'name',
Max = 'max',
Min = 'min',
Sum = 'sum',
Avg = 'avg',
}

export type SortSeriesData = {
sort_series_type: SortSeriesType;
sort_series_ascending: boolean;
};

export * from './Timeseries/types';
Loading

0 comments on commit 0c454c6

Please sign in to comment.