Skip to content

Commit

Permalink
feat: Order segments by position for ordinal dimensions in lines chart
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jan 28, 2022
1 parent a397b46 commit 794a28d
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions app/charts/line/lines-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ScaleTime,
scaleTime,
} from "d3";
import { sortBy } from "lodash";
import { ReactNode, useMemo } from "react";
import { LineFields } from "../../configurator";
import {
Expand All @@ -22,6 +23,7 @@ import { Observation } from "../../domain/data";
import { sortByIndex } from "../../lib/array";
import { estimateTextWidth } from "../../lib/estimate-text-width";
import { useTheme } from "../../themes";
import { makeOrdinalDimensionSorter } from "../../utils/sorting-values";
import { BRUSH_BOTTOM_SPACE } from "../shared/brush";
import {
getLabelWithUnit,
Expand Down Expand Up @@ -173,11 +175,20 @@ const useLinesState = ({
const yAxisLabel = getLabelWithUnit(yMeasure);

// segments
const segments = useMemo(
() =>
[...new Set(sortedData.map(getSegment))].sort((a, b) => ascending(a, b)),
[getSegment, sortedData]
);
const segments = useMemo(() => {
const segments = [...new Set(sortedData.map(getSegment))].sort((a, b) =>
ascending(a, b)
);
const dimension = dimensions.find(
(d) => d.iri === fields?.segment?.componentIri
);
if (dimension?.__typename === "OrdinalDimension") {
const sorter = makeOrdinalDimensionSorter(dimension);
return sortBy(segments, sorter);
}
return segments;
}, [dimensions, fields?.segment?.componentIri, getSegment, sortedData]);

// Map ordered segments to colors
const colors = scaleOrdinal<string, string>();
const segmentDimension = dimensions.find(
Expand Down

0 comments on commit 794a28d

Please sign in to comment.