Skip to content

Commit

Permalink
fix: Use sorting order correctly even if there are multiple ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Nov 4, 2022
1 parent 7b2ef1e commit f56f925
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
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
9 changes: 9 additions & 0 deletions app/utils/sorting-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
};

0 comments on commit f56f925

Please sign in to comment.