File tree Expand file tree Collapse file tree
packages/engine/src/charts Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -18,24 +18,17 @@ import type {
1818 ResolvedLabel ,
1919} from '@opendata-ai/openchart-core' ;
2020import {
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). */
4033const 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 ;
Original file line number Diff line number Diff line change @@ -18,23 +18,12 @@ import type {
1818 ResolvedLabel ,
1919} from '@opendata-ai/openchart-core' ;
2020import {
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 ;
Original file line number Diff line number Diff line change @@ -18,23 +18,12 @@ import type {
1818 ResolvedLabel ,
1919} from '@opendata-ai/openchart-core' ;
2020import {
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 ;
You can’t perform that action at this time.
0 commit comments