Skip to content

Commit

Permalink
Merge pull request #821 from visualize-admin/fix/order-position-ident…
Browse files Browse the repository at this point in the history
…ifier
  • Loading branch information
ptbrowne authored Nov 4, 2022
2 parents 7a8ec64 + f56f925 commit 094f5f0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 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
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"
);
};

1 comment on commit 094f5f0

@vercel
Copy link

@vercel vercel bot commented on 094f5f0 Nov 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app

Please sign in to comment.