Skip to content

Commit

Permalink
feat(plugin-chart-pivot-table): sort by metric (#1240)
Browse files Browse the repository at this point in the history
* feat(plugin-chart-pivot-table): sort by metric

* Use normalizeOrderBy func
  • Loading branch information
kgabryje authored and zhaoyongjie committed Nov 26, 2021
1 parent f36619d commit 8d54c69
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
* specific language governing permissions and limitations
* under the License.
*/
import { buildQueryContext, ensureIsArray } from '@superset-ui/core';
import { buildQueryContext, ensureIsArray, normalizeOrderBy } from '@superset-ui/core';
import { PivotTableQueryFormData } from '../types';

export default function buildQuery(formData: PivotTableQueryFormData) {
const { groupbyColumns = [], groupbyRows = [] } = formData;
const { groupbyColumns = [], groupbyRows = [], order_desc = true } = formData;
const groupbySet = new Set([
...ensureIsArray<string>(groupbyColumns),
...ensureIsArray<string>(groupbyRows),
]);
return buildQueryContext(formData, baseQueryObject => [
{
...baseQueryObject,
columns: [...groupbySet],
},
]);
return buildQueryContext(formData, baseQueryObject => {
const queryObject = normalizeOrderBy({ ...baseQueryObject, order_desc });
return [
{
...queryObject,
columns: [...groupbySet],
},
];
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ const config: ControlPanelConfig = {
},
},
],
['timeseries_limit_metric'],
[
{
name: 'order_desc',
config: {
type: 'CheckboxControl',
label: t('Sort descending'),
default: true,
description: t(
'Whether to sort descending or ascending. Takes effect only when "Sort by" is set',
),
},
},
],
],
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
JsonObject,
TimeFormatter,
NumberFormatter,
QueryFormMetric,
} from '@superset-ui/core';
import { ColorFormatters } from '@superset-ui/chart-controls';

Expand Down Expand Up @@ -66,6 +67,8 @@ interface PivotTableCustomizeProps {
metricsLayout?: MetricsLayoutEnum;
metricColorFormatters: ColorFormatters;
dateFormatters: Record<string, DateFormatter | undefined>;
timeseries_limit_metric: QueryFormMetric[] | QueryFormMetric | null;
order_desc: boolean;
}

export type PivotTableQueryFormData = QueryFormData &
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe('PivotTableChart buildQuery', () => {
metricColorFormatters: [],
dateFormatters: {},
setDataMask: () => {},
timeseries_limit_metric: 'count',
order_desc: true,
};

it('should build groupby with series in form data', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ describe('PivotTableChart transformProps', () => {
datasource: '',
conditionalFormatting: [],
dateFormat: '',
timeseries_limit_metric: 'count',
order_desc: true,
};
const chartProps = new ChartProps<QueryFormData>({
formData,
Expand Down

0 comments on commit 8d54c69

Please sign in to comment.