diff --git a/src/utils/bivariate/helpers/converters/axisDTOtoAxis.ts b/src/utils/bivariate/helpers/converters/axisDTOtoAxis.ts index 06bde73ad..4010b5c4e 100644 --- a/src/utils/bivariate/helpers/converters/axisDTOtoAxis.ts +++ b/src/utils/bivariate/helpers/converters/axisDTOtoAxis.ts @@ -1,4 +1,7 @@ -import { formatBivariateAxisLabel } from '~utils/bivariate/labelFormatters'; +import { + formatBivariateAxisLabel, + formatCustomBivariateAxisLabel, +} from '~utils/bivariate/labelFormatters'; import type { AxisDTO } from '~core/resources/bivariateStatisticsResource/types'; import type { Axis } from '~utils/bivariate/types/stat.types'; @@ -6,7 +9,9 @@ export function axisDTOtoAxis(dto: AxisDTO): Axis { return { ...dto, id: dto.quotient.join('|'), - label: dto.label || formatBivariateAxisLabel(dto.quotients), + label: dto.label + ? formatCustomBivariateAxisLabel(dto.label, dto.quotients) + : formatBivariateAxisLabel(dto.quotients), transformation: { ...dto.transformation!, transformation: dto.transformation?.transformation ?? 'no', diff --git a/src/utils/bivariate/labelFormatters.test.ts b/src/utils/bivariate/labelFormatters.test.ts index 567a81f09..1ee2a24ee 100644 --- a/src/utils/bivariate/labelFormatters.test.ts +++ b/src/utils/bivariate/labelFormatters.test.ts @@ -1,5 +1,9 @@ import { expect, describe, it } from 'vitest'; -import { hasUnits, formatBivariateAxisLabel } from './labelFormatters'; +import { + hasUnits, + formatBivariateAxisLabel, + formatCustomBivariateAxisLabel, +} from './labelFormatters'; import type { Axis } from '~utils/bivariate'; describe('BivariateLegend labels formatting', () => { @@ -118,4 +122,64 @@ describe('BivariateLegend labels formatting', () => { ); }); }); + + describe('formatCustomLabelForBivariateAxis', () => { + it('must return customLabel with units from quotients', () => { + const quotients: Axis['quotients'] = [ + { + name: 'total_road_length', + label: 'Total road length', + direction: [['unimportant'], ['important']], + unit: { + id: 'km', + shortName: 'km', + longName: 'kilometers', + }, + }, + { + name: 'population', + label: 'Population', + direction: [['unimportant'], ['important']], + unit: { + id: 'ppl', + shortName: 'ppl', + longName: 'people', + }, + }, + ]; + const customLabel = 'Custom man-distance label'; + expect(formatCustomBivariateAxisLabel(customLabel, quotients)).toEqual( + 'Custom man-distance label (km/ppl)', + ); + }); + + it('must return customLabel without units if numerator has no unit', () => { + const quotients: Axis['quotients'] = [ + { + name: 'total_road_length', + label: 'Total road length', + direction: [['unimportant'], ['important']], + unit: { + id: null, + shortName: null, + longName: null, + }, + }, + { + name: 'population', + label: 'Population', + direction: [['unimportant'], ['important']], + unit: { + id: 'ppl', + shortName: 'ppl', + longName: 'people', + }, + }, + ]; + const customLabel = 'Custom man-distance label'; + expect(formatCustomBivariateAxisLabel(customLabel, quotients)).toEqual( + 'Custom man-distance label', + ); + }); + }); }); diff --git a/src/utils/bivariate/labelFormatters.ts b/src/utils/bivariate/labelFormatters.ts index 072eca2fa..350f63a97 100644 --- a/src/utils/bivariate/labelFormatters.ts +++ b/src/utils/bivariate/labelFormatters.ts @@ -10,6 +10,14 @@ export const convertDirectionsArrayToLabel = (directions: string[][]) => { return `${formatSentimentDirection(from)} → ${formatSentimentDirection(to)}`; }; +export const formatCustomBivariateAxisLabel = ( + customLabel: string, + quotients: Axis['quotients'], +): string => { + const units = formatBivariateAxisUnit(quotients); + return units ? `${customLabel} (${units})` : customLabel; +}; + export const formatBivariateAxisLabel = (quotients: Axis['quotients']): string => { if (!quotients) return ''; const [numerator, denominator] = quotients;