Skip to content

Commit 8e908cb

Browse files
committed
refactor(engine): extract formatLabelValue shared helper for bar/column/dot
Consolidates a byte-identical 4-line value formatter that was duplicated across bar, column, and dot label modules into packages/engine/src/charts/_shared/format-label-value.ts. Font sizes (11 in bar/dot, 10 in column), LABEL_FONT_WEIGHT, the bar-only parseDisplayNumber / SUFFIX_MULTIPLIERS, and density filter logic are intentionally left local because they diverge or will be consolidated separately in a later step. Pure dedup: no behavior change, all 1792 tests pass, all 8 visual baselines match with zero pixel drift.
1 parent d738cc7 commit 8e908cb

4 files changed

Lines changed: 21 additions & 35 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Shared numeric value formatter for data labels.
3+
*
4+
* Used by bar, column, and dot label computation to display a value:
5+
* abbreviated (K/M/B/T) for magnitudes >= 1000, otherwise the default
6+
* numeric format.
7+
*/
8+
9+
import { abbreviateNumber, formatNumber } from '@opendata-ai/openchart-core';
10+
11+
/** Format a label value for display (abbreviate large numbers). */
12+
export function formatLabelValue(value: number): string {
13+
if (Math.abs(value) >= 1000) return abbreviateNumber(value);
14+
return formatNumber(value);
15+
}

packages/engine/src/charts/bar/labels.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,17 @@ import type {
1818
ResolvedLabel,
1919
} from '@opendata-ai/openchart-core';
2020
import {
21-
abbreviateNumber,
2221
buildD3Formatter,
2322
estimateTextWidth,
24-
formatNumber,
2523
getRepresentativeColor,
2624
resolveCollisions,
2725
} from '@opendata-ai/openchart-core';
26+
import { formatLabelValue } from '../_shared/format-label-value';
2827

2928
// ---------------------------------------------------------------------------
3029
// Helpers
3130
// ---------------------------------------------------------------------------
3231

33-
/** Format a bar value for display (abbreviate large numbers). */
34-
function formatBarValue(value: number): string {
35-
if (Math.abs(value) >= 1000) return abbreviateNumber(value);
36-
return formatNumber(value);
37-
}
38-
3932
/** Suffix multipliers mirroring core's abbreviateNumber output (K/M/B/T). */
4033
const SUFFIX_MULTIPLIERS: Record<string, number> = {
4134
K: 1_000,
@@ -124,7 +117,7 @@ export function computeBarLabels(
124117
if (formatter && Number.isFinite(rawNum)) {
125118
valuePart = formatter(rawNum);
126119
} else if (Number.isFinite(rawNum)) {
127-
valuePart = formatBarValue(rawNum);
120+
valuePart = formatLabelValue(rawNum);
128121
} else {
129122
// Fallback: extract from aria label
130123
const ariaLabel = mark.aria.label;

packages/engine/src/charts/column/labels.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,12 @@ import type {
1818
ResolvedLabel,
1919
} from '@opendata-ai/openchart-core';
2020
import {
21-
abbreviateNumber,
2221
buildD3Formatter,
2322
estimateTextWidth,
24-
formatNumber,
2523
getRepresentativeColor,
2624
resolveCollisions,
2725
} from '@opendata-ai/openchart-core';
28-
29-
// ---------------------------------------------------------------------------
30-
// Helpers
31-
// ---------------------------------------------------------------------------
32-
33-
/** Format a column value for display (abbreviate large numbers). */
34-
function formatColumnValue(value: number): string {
35-
if (Math.abs(value) >= 1000) return abbreviateNumber(value);
36-
return formatNumber(value);
37-
}
26+
import { formatLabelValue } from '../_shared/format-label-value';
3827

3928
// ---------------------------------------------------------------------------
4029
// Constants
@@ -82,7 +71,7 @@ export function computeColumnLabels(
8271
if (formatter && Number.isFinite(rawNum)) {
8372
valuePart = formatter(rawNum);
8473
} else if (Number.isFinite(rawNum)) {
85-
valuePart = formatColumnValue(rawNum);
74+
valuePart = formatLabelValue(rawNum);
8675
} else {
8776
// Fallback: extract from aria label
8877
const ariaLabel = mark.aria.label;

packages/engine/src/charts/dot/labels.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,12 @@ import type {
1818
ResolvedLabel,
1919
} from '@opendata-ai/openchart-core';
2020
import {
21-
abbreviateNumber,
2221
buildD3Formatter,
2322
estimateTextWidth,
24-
formatNumber,
2523
getRepresentativeColor,
2624
resolveCollisions,
2725
} from '@opendata-ai/openchart-core';
28-
29-
// ---------------------------------------------------------------------------
30-
// Helpers
31-
// ---------------------------------------------------------------------------
32-
33-
/** Format a dot value for display (abbreviate large numbers). */
34-
function formatDotValue(value: number): string {
35-
if (Math.abs(value) >= 1000) return abbreviateNumber(value);
36-
return formatNumber(value);
37-
}
26+
import { formatLabelValue } from '../_shared/format-label-value';
3827

3928
// ---------------------------------------------------------------------------
4029
// Constants
@@ -81,7 +70,7 @@ export function computeDotLabels(
8170
if (formatter && Number.isFinite(rawNum)) {
8271
valuePart = formatter(rawNum);
8372
} else if (Number.isFinite(rawNum)) {
84-
valuePart = formatDotValue(rawNum);
73+
valuePart = formatLabelValue(rawNum);
8574
} else {
8675
// Fallback: extract from aria label
8776
const ariaLabel = mark.aria.label;

0 commit comments

Comments
 (0)