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: Sorting issues #1055

Merged
merged 5 commits into from
Jun 6, 2023
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
7 changes: 5 additions & 2 deletions app/charts/line/lines-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -221,7 +224,7 @@ const useLinesState = (
return orderBy(
uniqueSegments,
sorters,
sorting?.sortingOrder === "desc" ? "desc" : "asc"
getSortingOrders(sorters, fields.segment?.sorting)
);
}, [
segmentDimension,
Expand Down
69 changes: 65 additions & 4 deletions app/utils/sorting-values.spec.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand Down
15 changes: 6 additions & 9 deletions app/utils/sorting-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const makeDimensionValueSorters = (
dimensionFilter: undefined,
}
): ((label: string) => string | undefined | number)[] => {
if (dimension?.__typename === "NumericalMeasure") {
return [(d) => +d];
}

const {
sorting,
sumsBySegment,
Expand Down Expand Up @@ -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);
Expand Down