Skip to content

Commit

Permalink
feat: Order segments by position for ordinal dimensions in area chart
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jan 28, 2022
1 parent 794a28d commit d00ccd0
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/charts/area/areas-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
stackOrderReverse,
sum,
} from "d3";
import { sortBy } from "lodash";
import { ReactNode, useMemo } from "react";
import { AreaFields } from "../../configurator";
import {
Expand All @@ -29,6 +30,7 @@ import { Observation } from "../../domain/data";
import { sortByIndex } from "../../lib/array";
import { estimateTextWidth } from "../../lib/estimate-text-width";
import { useLocale } from "../../locales/use-locale";
import { makeOrdinalDimensionSorter } from "../../utils/sorting-values";
import { BRUSH_BOTTOM_SPACE } from "../shared/brush";
import {
getLabelWithUnit,
Expand Down Expand Up @@ -207,10 +209,27 @@ const useAreasState = ({
)
.map((d) => d[0]);

const getSegmentsOrderedByPosition = () => {
const segments = Array.from(
new Set(sortedData.map((d) => getSegment(d)))
);
const sorter = dimension ? makeOrdinalDimensionSorter(dimension) : null;
return sorter ? sortBy(segments, sorter) : segments;
};

const dimension = dimensions.find(
(dim) => dim.iri === fields.segment?.componentIri
);
if (dimension?.__typename === "OrdinalDimension") {
return getSegmentsOrderedByPosition();
}

return segmentSortingType === "byDimensionLabel"
? getSegmentsOrderedByName()
: getSegmentsOrderedByTotalValue();
}, [
dimensions,
fields.segment?.componentIri,
getSegment,
getY,
locale,
Expand Down

0 comments on commit d00ccd0

Please sign in to comment.