Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Order for sorting is position / identifier #821

Merged
merged 2 commits into from
Nov 4, 2022
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
8 changes: 5 additions & 3 deletions app/charts/area/areas-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ import {
import { getPalette } from "@/palettes";
import { sortByIndex } from "@/utils/array";
import { estimateTextWidth } from "@/utils/estimate-text-width";
import { makeDimensionValueSorters } from "@/utils/sorting-values";
import {
getSortingOrders,
makeDimensionValueSorters,
} from "@/utils/sorting-values";

export interface AreasState extends CommonChartState {
chartType: "area";
Expand Down Expand Up @@ -228,13 +231,12 @@ const useAreasState = (
return orderBy(
uniqueSegments,
sorters,
segmentSortingOrder === "desc" ? "desc" : "asc"
getSortingOrders(sorters, segmentSorting)
);
}, [
plottableSortedData,
dimensions,
segmentSorting,
segmentSortingOrder,
getY,
getSegment,
fields.segment?.componentIri,
Expand Down
11 changes: 5 additions & 6 deletions app/charts/column/columns-grouped-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ import { isTemporalDimension, Observation } from "@/domain/data";
import { useFormatNumber, formatNumberWithUnit } from "@/formatters";
import { getPalette } from "@/palettes";
import { sortByIndex } from "@/utils/array";
import { makeDimensionValueSorters } from "@/utils/sorting-values";
import {
getSortingOrders,
makeDimensionValueSorters,
} from "@/utils/sorting-values";

export interface GroupedColumnsState extends CommonChartState {
chartType: "column";
Expand Down Expand Up @@ -197,11 +200,7 @@ const useGroupedColumnsState = (
sumsBySegment,
});

return orderBy(
uniqueSegments,
sorters,
sorting?.sortingOrder === "desc" ? "desc" : "asc"
);
return orderBy(uniqueSegments, sorters, getSortingOrders(sorters, sorting));
}, [
plottableSortedData,
dimensions,
Expand Down
12 changes: 6 additions & 6 deletions app/charts/column/columns-stacked-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ import { isTemporalDimension, Observation } from "@/domain/data";
import { formatNumberWithUnit, useFormatNumber } from "@/formatters";
import { getPalette } from "@/palettes";
import { sortByIndex } from "@/utils/array";
import { makeDimensionValueSorters } from "@/utils/sorting-values";
import {
getSortingOrders,
makeDimensionValueSorters,
} from "@/utils/sorting-values";

export interface StackedColumnsState extends CommonChartState {
chartType: "column";
Expand Down Expand Up @@ -201,11 +204,8 @@ const useColumnsStackedState = (
sorting,
sumsBySegment,
});
return orderBy(
uniqueSegments,
sorters,
sorting?.sortingOrder === "desc" ? "desc" : "asc"
);

return orderBy(uniqueSegments, sorters, getSortingOrders(sorters, sorting));
}, [
plottableSortedData,
dimensions,
Expand Down
11 changes: 7 additions & 4 deletions app/charts/pie/pie-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import { PieFields } from "@/configurator";
import { DimensionValue, Observation } from "@/domain/data";
import { formatNumberWithUnit, useFormatNumber } from "@/formatters";
import { getPalette } from "@/palettes";
import { makeDimensionValueSorters } from "@/utils/sorting-values";

import {
getSortingOrders,
makeDimensionValueSorters,
} from "@/utils/sorting-values";
export interface PieState extends CommonChartState {
chartType: "pie";
allData: Observation[];
Expand Down Expand Up @@ -114,15 +116,16 @@ const usePieState = (
.filter((x) => typeof x[1] === "number")
.map((x) => x[0]);

const sorting = fields.segment.sorting;
const sorters = makeDimensionValueSorters(segmentDimension, {
sorting: fields.segment.sorting,
sorting: sorting,
measureBySegment,
});

const segments = orderBy(
uniqueSegments,
sorters,
sortingOrder === "desc" ? "desc" : "asc"
getSortingOrders(sorters, sorting)
);

if (fields.segment && segmentDimension && fields.segment.colorMapping) {
Expand Down
13 changes: 11 additions & 2 deletions app/utils/sorting-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const makeDimensionValueSorters = (
dv: DimensionValue["label"]
) => string | undefined | number)[] = [];

const defaultSorters = [getIdentifier, getPosition, getLabel];
const defaultSorters = [getPosition, getIdentifier, getLabel];

switch (sortingType) {
case "byDimensionLabel":
Expand All @@ -69,7 +69,7 @@ export const makeDimensionValueSorters = (
sorters = [getMeasure];
break;
case "byAuto":
sorters = [getIdentifier, getPosition];
sorters = [getPosition, getIdentifier];
break;
default:
sorters = defaultSorters;
Expand All @@ -90,3 +90,12 @@ export const valueComparator = (locale: string) => (a: Value, b: Value) => {
return a.label.localeCompare(b.label, locale);
}
};

export const getSortingOrders = (
sorters: ((...args: any[]) => any)[],
sorting: SortingField["sorting"]
) => {
return Array(sorters.length).fill(
sorting?.sortingOrder === "desc" ? "desc" : "asc"
);
};