diff --git a/app/charts/line/lines-state.tsx b/app/charts/line/lines-state.tsx index 2d8b2bf93..dfc1b0d03 100644 --- a/app/charts/line/lines-state.tsx +++ b/app/charts/line/lines-state.tsx @@ -44,7 +44,10 @@ import { DimensionMetadataFragment } from "@/graphql/query-hooks"; 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 LinesState extends CommonChartState { chartType: "line"; @@ -221,7 +224,7 @@ const useLinesState = ( return orderBy( uniqueSegments, sorters, - sorting?.sortingOrder === "desc" ? "desc" : "asc" + getSortingOrders(sorters, fields.segment?.sorting) ); }, [ segmentDimension, diff --git a/app/utils/sorting-values.spec.ts b/app/utils/sorting-values.spec.ts index 46b2877c0..af6c86dd8 100644 --- a/app/utils/sorting-values.spec.ts +++ b/app/utils/sorting-values.spec.ts @@ -1,10 +1,49 @@ import orderBy from "lodash/orderBy"; import { SortingField } from "@/configurator"; -import { DimensionMetadataWithHierarchiesFragment } from "@/graphql/query-hooks"; -import { makeDimensionValueSorters } from "@/utils/sorting-values"; +import { + DimensionMetadataFragment, + DimensionMetadataWithHierarchiesFragment, +} from "@/graphql/query-hooks"; +import { + getSortingOrders, + makeDimensionValueSorters, +} from "@/utils/sorting-values"; const dimension = { + values: [ + { + value: "A", + label: "A", + position: 5, + identifier: "A", + }, + { + value: "B", + label: "B", + position: 5, + identifier: "B", + }, + { + value: "C", + label: "C", + position: 1, + identifier: "C", + }, + { + value: "D", + label: "D", + position: 1, + identifier: "C", + }, + ], +} as unknown as DimensionMetadataFragment; + +const measure = { + __typename: "NumericalMeasure", +} as unknown as DimensionMetadataFragment; + +const hierarchicalDimension = { hierarchy: [ { label: "Switzerland", @@ -71,10 +110,32 @@ describe("makeDimensionValueSorters", () => { sortingOrder: "asc", }; - it("should correctly sort hierarchical dimensions byAuto", () => { + it("should correctly sort dimensions byAuto", () => { const values = dimension.values.map((d) => d.value); const sorters = makeDimensionValueSorters(dimension, { sorting }); - expect(orderBy(values, sorters, ["asc"])).toEqual([ + const sortingOrders = getSortingOrders(sorters, sorting); + expect(orderBy(values, sorters, sortingOrders)).toEqual([ + "C", + "D", + "A", + "B", + ]); + }); + + it("should correctly sort numerical measures byAuto", () => { + const values = [1, 10, 5, 100, 2]; + const sorters = makeDimensionValueSorters(measure, { sorting }); + const sortingOrders = getSortingOrders(sorters, sorting); + expect(orderBy(values, sorters, sortingOrders)).toEqual([1, 2, 5, 10, 100]); + }); + + it("should correctly sort hierarchical dimensions byAuto", () => { + const values = hierarchicalDimension.values.map((d) => d.value); + const sorters = makeDimensionValueSorters(hierarchicalDimension, { + sorting, + }); + const sortingOrders = getSortingOrders(sorters, sorting); + expect(orderBy(values, sorters, sortingOrders)).toEqual([ "CH", "CH-PROD-EAST", "CH-PROD-WEST", diff --git a/app/utils/sorting-values.ts b/app/utils/sorting-values.ts index acaf30b00..fc8e13207 100644 --- a/app/utils/sorting-values.ts +++ b/app/utils/sorting-values.ts @@ -41,6 +41,10 @@ export const makeDimensionValueSorters = ( dimensionFilter: undefined, } ): ((label: string) => string | undefined | number)[] => { + if (dimension?.__typename === "NumericalMeasure") { + return [(d) => +d]; + } + const { sorting, sumsBySegment, @@ -75,17 +79,10 @@ export const makeDimensionValueSorters = ( allHierarchyValues, (dv) => dv.value ); - const valuesByValue = uniqueMapBy( - values.filter((x) => x.identifier || x.position), - (dv) => dv.value - ); - // Index values that have an identifier or a position + const valuesByValue = uniqueMapBy(values, (dv) => dv.value); // Warning: if two values have the same label and have an identifier / position // there could be problems as we could select the "wrong" value for the order - const valuesByLabel = uniqueMapBy( - values.filter((x) => x.identifier || x.position), - (dv) => dv.label - ); + const valuesByLabel = uniqueMapBy(values, (dv) => dv.label); const getByValueOrLabel = (valueOrLabel: string) => { return valuesByValue.get(valueOrLabel) ?? valuesByLabel.get(valueOrLabel);