@@ -16,9 +16,8 @@ import {
1616 sum ,
1717} from "d3" ;
1818import get from "lodash/get" ;
19- import keyBy from "lodash/keyBy" ;
2019import orderBy from "lodash/orderBy" ;
21- import React , { ReactNode , useCallback , useMemo } from "react" ;
20+ import React , { ReactNode , useMemo } from "react" ;
2221
2322import {
2423 BOTTOM_MARGIN_OFFSET ,
@@ -32,13 +31,12 @@ import {
3231 useDataAfterInteractiveFilters ,
3332 useOptionalNumericVariable ,
3433 usePlottableData ,
35- useSegment ,
36- useStringVariable ,
3734 useTemporalVariable ,
3835} from "@/charts/shared/chart-helpers" ;
3936import { CommonChartState } from "@/charts/shared/chart-state" ;
4037import { TooltipInfo } from "@/charts/shared/interaction/tooltip" ;
4138import { useChartPadding } from "@/charts/shared/padding" ;
39+ import { useMaybeAbbreviations } from "@/charts/shared/use-abbreviations" ;
4240import useChartFormatters from "@/charts/shared/use-chart-formatters" ;
4341import { ChartContext , ChartProps } from "@/charts/shared/use-chart-state" ;
4442import { InteractionProvider } from "@/charts/shared/use-interaction" ;
@@ -49,7 +47,11 @@ import {
4947 useErrorMeasure ,
5048 useErrorRange ,
5149} from "@/configurator/components/ui-helpers" ;
52- import { isTemporalDimension , Observation } from "@/domain/data" ;
50+ import {
51+ DimensionValue ,
52+ isTemporalDimension ,
53+ Observation ,
54+ } from "@/domain/data" ;
5355import { useFormatNumber , formatNumberWithUnit } from "@/formatters" ;
5456import { DimensionMetadataFragment } from "@/graphql/query-hooks" ;
5557import { getPalette } from "@/palettes" ;
@@ -112,33 +114,37 @@ const useGroupedColumnsState = (
112114
113115 const xIsTime = isTemporalDimension ( xDimension ) ;
114116
115- const getX = useStringVariable ( fields . x . componentIri ) ;
117+ const { getAbbreviationOrLabelByValue : getX } = useMaybeAbbreviations ( {
118+ useAbbreviations : fields . x . useAbbreviations ?? false ,
119+ dimension : xDimension ,
120+ } ) ;
121+
116122 const getXAsDate = useTemporalVariable ( fields . x . componentIri ) ;
117123 const getY = useOptionalNumericVariable ( fields . y . componentIri ) ;
118124 const errorMeasure = useErrorMeasure ( chartProps , fields . y . componentIri ) ;
119125 const getYErrorRange = useErrorRange ( errorMeasure , getY ) ;
120- const getSegment = useSegment ( fields . segment ?. componentIri ) ;
121-
122- const showStandardError = get ( fields , [ "y" , "showStandardError" ] , true ) ;
123126
124- const { segmentValuesByValue } = useMemo ( ( ) => {
125- const segmentDimension = dimensions . find (
126- ( d ) => d . iri === fields . segment ?. componentIri
127- ) || { values : [ ] } ;
128- return {
129- segmentValuesByValue : keyBy ( segmentDimension . values , ( x ) => x . value ) ,
130- segmentValuesByLabel : keyBy ( segmentDimension . values , ( x ) => x . label ) ,
131- } ;
132- } , [ dimensions , fields . segment ?. componentIri ] ) ;
133-
134- /** When segment values are IRIs, we need to find show the label */
135- const getSegmentLabel = useCallback (
136- ( segment : string ) : string => {
137- return segmentValuesByValue [ segment ] ?. label || segment ;
138- } ,
139- [ segmentValuesByValue ]
127+ const segmentDimension = dimensions . find (
128+ ( d ) => d . iri === fields . segment ?. componentIri
140129 ) ;
141130
131+ const {
132+ getAbbreviationOrLabelByValue : getSegment ,
133+ getLabelByAbbreviation : getSegmentLabel ,
134+ abbreviationOrLabelLookup : segmentsByAbbreviationOrLabel ,
135+ } = useMaybeAbbreviations ( {
136+ useAbbreviations : fields . segment ?. useAbbreviations ?? false ,
137+ dimension : segmentDimension ,
138+ } ) ;
139+
140+ const segmentsByValue = useMemo ( ( ) => {
141+ const values = ( segmentDimension ?. values || [ ] ) as DimensionValue [ ] ;
142+
143+ return new Map ( values . map ( ( d ) => [ d . value , d ] ) ) ;
144+ } , [ segmentDimension ?. values ] ) ;
145+
146+ const showStandardError = get ( fields , [ "y" , "showStandardError" ] , true ) ;
147+
142148 // Sort
143149 const xSorting = fields . x . sorting ;
144150
@@ -191,22 +197,20 @@ const useGroupedColumnsState = (
191197 const uniqueSegments = Array . from (
192198 new Set ( plottableSortedData . map ( ( d ) => getSegment ( d ) ) )
193199 ) ;
194- const dimension = dimensions . find (
195- ( d ) => d . iri === fields . segment ?. componentIri
196- ) ;
197200
198201 const sorting = fields ?. segment ?. sorting ;
199- const sorters = makeDimensionValueSorters ( dimension , {
202+ const sorters = makeDimensionValueSorters ( segmentDimension , {
200203 sorting,
201204 sumsBySegment,
205+ useAbbreviations : fields . segment ?. useAbbreviations ,
202206 } ) ;
203207
204208 return orderBy ( uniqueSegments , sorters , getSortingOrders ( sorters , sorting ) ) ;
205209 } , [
206210 plottableSortedData ,
207- dimensions ,
211+ segmentDimension ,
208212 fields . segment ?. sorting ,
209- fields . segment ?. componentIri ,
213+ fields . segment ?. useAbbreviations ,
210214 sumsBySegment ,
211215 getSegment ,
212216 ] ) ;
@@ -228,10 +232,10 @@ const useGroupedColumnsState = (
228232
229233 if ( fields . segment && segmentDimension && fields . segment . colorMapping ) {
230234 const orderedSegmentLabelsAndColors = segments . map ( ( segment ) => {
231- const dvIri = segmentDimension . values . find (
232- ( s : { label : string ; value : string } ) =>
233- s . label === segment || s . value === segment
234- ) . value ;
235+ const dvIri =
236+ segmentsByAbbreviationOrLabel . get ( segment ) ?. value ||
237+ segmentsByValue . get ( segment ) ? .value ||
238+ "" ;
235239
236240 return {
237241 label : segment ,
@@ -299,6 +303,8 @@ const useGroupedColumnsState = (
299303 fields . segment ,
300304 preparedData ,
301305 segments ,
306+ segmentsByAbbreviationOrLabel ,
307+ segmentsByValue ,
302308 plottableSortedData ,
303309 getX ,
304310 getXAsDate ,
0 commit comments