From f53f86f7965b9f4e98c40047ec6d64016d5adc53 Mon Sep 17 00:00:00 2001 From: Geido <60598000+geido@users.noreply.github.com> Date: Wed, 9 Mar 2022 20:12:45 +0200 Subject: [PATCH] Control sortby based on series limit (#18950) --- .../src/shared-controls/index.tsx | 10 +++++ .../src/Bar/controlPanel.ts | 38 ++++++++++++++++++- .../src/NVD3Controls.tsx | 14 +------ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/index.tsx b/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/index.tsx index 90c0e88f9..105f6f154 100644 --- a/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/index.tsx +++ b/superset-frontend/packages/superset-ui-chart-controls/src/shared-controls/index.tsx @@ -336,6 +336,15 @@ const row_limit: SharedControlConfig<'SelectControl'> = { description: t('Limits the number of rows that get displayed.'), }; +const order_desc: SharedControlConfig<'CheckboxControl'> = { + type: 'CheckboxControl', + label: t('Sort Descending'), + default: true, + description: t('Whether to sort descending or ascending'), + visibility: ({ controls }) => + Boolean(controls?.timeseries_limit_metric.value), +}; + const limit: SharedControlConfig<'SelectControl'> = { type: 'SelectControl', freeForm: true, @@ -507,6 +516,7 @@ const sharedControls = { limit, timeseries_limit_metric: enableExploreDnd ? dnd_sort_by : sort_by, orderby: enableExploreDnd ? dnd_sort_by : sort_by, + order_desc, series: enableExploreDnd ? dndSeries : series, entity: enableExploreDnd ? dndEntity : entity, x: enableExploreDnd ? dnd_x : x, diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/Bar/controlPanel.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/Bar/controlPanel.ts index d53ad9fcf..61d3f14ad 100644 --- a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/Bar/controlPanel.ts +++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/Bar/controlPanel.ts @@ -17,7 +17,11 @@ * under the License. */ import { t } from '@superset-ui/core'; -import { ControlPanelConfig, sections } from '@superset-ui/chart-controls'; +import { + ControlPanelConfig, + sections, + sharedControls, +} from '@superset-ui/chart-controls'; import { lineInterpolation, showBrush, @@ -41,6 +45,38 @@ import { } from '../NVD3Controls'; const config: ControlPanelConfig = { + controlOverrides: { + limit: { + rerender: ['timeseries_limit_metric', 'order_desc'], + }, + timeseries_limit_metric: { + label: t('Series Limit Sort By'), + description: t( + 'Metric used to order the limit if a series limit is present. ' + + 'If undefined reverts to the first metric (where appropriate).', + ), + visibility: ({ controls }) => Boolean(controls?.limit.value), + mapStateToProps: (state, controlState) => { + const timeserieslimitProps = + sharedControls.timeseries_limit_metric.mapStateToProps?.( + state, + controlState, + ) || {}; + timeserieslimitProps.value = state.controls?.limit?.value + ? controlState.value + : []; + return timeserieslimitProps; + }, + }, + order_desc: { + label: t('Series Limit Sort Descending'), + default: false, + description: t( + 'Whether to sort descending or ascending if a series limit is present', + ), + visibility: ({ controls }) => Boolean(controls?.limit.value), + }, + }, controlPanelSections: [ sections.legacyTimeseriesTime, timeSeriesSection[0], diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Controls.tsx b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Controls.tsx index 4f942b4b7..3b0bb92ac 100644 --- a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Controls.tsx +++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/NVD3Controls.tsx @@ -346,19 +346,7 @@ export const timeSeriesSection: ControlPanelSectionConfig[] = [ ['groupby'], ['limit'], ['timeseries_limit_metric'], - [ - { - name: 'order_desc', - config: { - type: 'CheckboxControl', - label: t('Sort Descending'), - default: true, - description: t('Whether to sort descending or ascending'), - visibility: ({ controls }) => - Boolean(controls?.timeseries_limit_metric.value), - }, - }, - ], + ['order_desc'], [ { name: 'contribution',