@@ -47,11 +47,12 @@ import {
4747 ChartTooltipContent ,
4848 ChartLegend ,
4949 ChartLegendContent ,
50+ type LegendSwatch ,
5051 ChartContainerConfig
5152} from './ChartContainerImpl' ;
5253import { mapScatterClick , mapTreemapClick , mapSankeyClick } from './chartDrillEvents' ;
5354import { formatterFor , domainFor , ticksFor , plotsWholeNumbers , RENDERABLE , SINGLE_VALUE_CHART_TYPES , TABULAR_CHART_TYPES , effectiveChartFamily , comboBaseFamily , placeYAxes , type DeclaredChartFamily , type NormalizedAxis , type NormalizedSeries , type ValueAxisSlot , type YAxisPlacement } from './normalizeChartSchema' ;
54- import { buildCategoryRank , chartRowBucketId , isRealCalendarDate , toDateInputValue , toDisplayDate , type ChartSegmentClickEvent } from '@object-ui/core' ;
55+ import { buildCategoryRank , chartRowBucketId , formatMonthBucketLabel , isRealCalendarDate , toDateInputValue , toDisplayDate , type ChartSegmentClickEvent } from '@object-ui/core' ;
5556import { createSafeTranslation , useDisplayLocale , useSafeTranslate } from '@object-ui/i18n' ;
5657
5758// Default color fallback for chart series
@@ -479,6 +480,87 @@ function estimateLabelWidthPx(label: string): number {
479480 return ems * AXIS_TICK_FONT_PX ;
480481}
481482
483+ /**
484+ * The most lines a horizontal-bar category label wraps to before it is cut.
485+ * Two keep a bar's label beside its own bar at the row heights a dashboard
486+ * tile draws.
487+ */
488+ const CATEGORY_LABEL_MAX_LINES = 2 ;
489+
490+ /**
491+ * A horizontal-bar category label broken into lines that fit `maxWidthPx`,
492+ * by the same upper estimate the x axis sizes its bands from
493+ * ({@link estimateLabelWidthPx}). Words wrap whole; a word wider than a line
494+ * is cut by glyph. Past {@link CATEGORY_LABEL_MAX_LINES} the last line ends in
495+ * an ellipsis and `truncated` is set, so the tick carries the full label as
496+ * its tooltip.
497+ */
498+ function wrapCategoryLabel ( label : string , maxWidthPx : number ) : { lines : string [ ] ; truncated : boolean } {
499+ const fits = ( text : string ) => estimateLabelWidthPx ( text ) <= maxWidthPx ;
500+ if ( label === '' || fits ( label ) ) return { lines : [ label ] , truncated : false } ;
501+ const lines : string [ ] = [ ] ;
502+ let current = '' ;
503+ const pushGlyphs = ( word : string ) => {
504+ for ( const glyph of word ) {
505+ if ( current !== '' && ! fits ( current + glyph ) ) {
506+ lines . push ( current ) ;
507+ current = '' ;
508+ }
509+ current += glyph ;
510+ }
511+ } ;
512+ for ( const word of label . split ( / \s + / u) . filter ( Boolean ) ) {
513+ const candidate = current === '' ? word : `${ current } ${ word } ` ;
514+ if ( fits ( candidate ) ) {
515+ current = candidate ;
516+ } else {
517+ if ( current !== '' ) lines . push ( current ) ;
518+ current = '' ;
519+ if ( fits ( word ) ) current = word ;
520+ else pushGlyphs ( word ) ;
521+ }
522+ }
523+ if ( current !== '' ) lines . push ( current ) ;
524+ if ( lines . length <= CATEGORY_LABEL_MAX_LINES ) return { lines, truncated : false } ;
525+ const kept = lines . slice ( 0 , CATEGORY_LABEL_MAX_LINES ) ;
526+ let last = kept [ kept . length - 1 ] ;
527+ while ( last . length > 0 && ! fits ( `${ last } …` ) ) last = Array . from ( last ) . slice ( 0 , - 1 ) . join ( '' ) ;
528+ kept [ kept . length - 1 ] = `${ last . trimEnd ( ) } …` ;
529+ return { lines : kept , truncated : true } ;
530+ }
531+
532+ /**
533+ * What a left category axis keeps between its edge and the label's end:
534+ * recharts' `tickSize` plus its default `tickMargin` of 2.
535+ */
536+ const CATEGORY_TICK_INSET_PX = RECHARTS_TICK_SIZE + 2 ;
537+
538+ /** The line advance of a wrapped category label, in em. */
539+ const CATEGORY_LABEL_LINE_EM = 1.1 ;
540+
541+ /**
542+ * The horizontal-bar CATEGORY axis tick: the full label, wrapped into the
543+ * axis width ({@link wrapCategoryLabel}) and centred on its bar, with the full
544+ * label as a tooltip when it had to be cut. It replaces the phone cut to eight
545+ * characters, which left a bar named only by its first letters ("In Progr…").
546+ */
547+ function WrappedCategoryTick ( props : any ) {
548+ const { x, y, payload, textAnchor, maxWidthPx, formatLabel } = props ;
549+ const full = String ( formatLabel ( payload ?. value ) ?? '' ) ;
550+ const { lines, truncated } = wrapCategoryLabel ( full , maxWidthPx ) ;
551+ // Centre the block on the tick: the first line rises half the extra lines,
552+ // and 0.355em puts a line's middle (not its baseline) on `y`.
553+ const firstDy = `${ 0.355 - ( ( lines . length - 1 ) * CATEGORY_LABEL_LINE_EM ) / 2 } em` ;
554+ return (
555+ < text x = { x } y = { y } textAnchor = { textAnchor ?? 'end' } fill = { AXIS_TICK_STYLE . fill } className = "recharts-text recharts-cartesian-axis-tick-value" >
556+ { truncated ? < title > { full } </ title > : null }
557+ { lines . map ( ( line , i ) => (
558+ < tspan key = { i } x = { x } dy = { i === 0 ? firstDy : `${ CATEGORY_LABEL_LINE_EM } em` } > { line } </ tspan >
559+ ) ) }
560+ </ text >
561+ ) ;
562+ }
563+
482564/** recharts' default chart margin, left plus right (5px each side). */
483565const CHART_MARGIN_X_PX = 10 ;
484566
@@ -2281,8 +2363,10 @@ function AdvancedChartImplInner({
22812363 } , [ ] ) ;
22822364
22832365 // Format ISO date strings into compact "MMM D" / "MMM YYYY" labels for X-axis ticks.
2284- // Falls back to the raw value when not parseable as a date.
2285- const formatTick = React . useCallback ( ( value : any ) : string => {
2366+ // Falls back to the raw value when not parseable as a date. The full label:
2367+ // `formatTick` below adds the phone cut, which the horizontal-bar category
2368+ // axis does not take (it wraps instead, see `WrappedCategoryTick`).
2369+ const formatCategoryLabel = React . useCallback ( ( value : any ) : string => {
22862370 if ( value == null || value === '' ) return '' ;
22872371 // A resolved display label for this exact category value — the same
22882372 // `config[key]?.label` convention already used for series names (below)
@@ -2292,6 +2376,10 @@ function AdvancedChartImplInner({
22922376 const resolved = config ?. [ String ( value ) ] ?. label ;
22932377 if ( resolved != null ) return String ( resolved ) ;
22942378 const str = typeof value === 'string' ? value : String ( value ) ;
2379+ // A month bucket key (`2025-10`, a `month`-granularity date dimension) reads
2380+ // as `Oct 2025`; the key itself stays the bucket's identity in the row.
2381+ const month = formatMonthBucketLabel ( str , displayLocale ) ;
2382+ if ( month !== undefined ) return month ;
22952383 // Detect ISO 8601 date / datetime strings (YYYY-MM-DD or with time component)
22962384 const isoLike = / ^ \d { 4 } - \d { 2 } - \d { 2 } / . test ( str ) ;
22972385 if ( isoLike ) {
@@ -2327,9 +2415,13 @@ function AdvancedChartImplInner({
23272415 }
23282416 }
23292417 }
2418+ return str ;
2419+ } , [ data , xAxisKey , config , displayLocale ] ) ;
2420+ const formatTick = React . useCallback ( ( value : any ) : string => {
2421+ const str = formatCategoryLabel ( value ) ;
23302422 if ( isMobile && str . length > 8 ) return str . slice ( 0 , 8 ) + '…' ;
23312423 return str ;
2332- } , [ data , xAxisKey , isMobile , config , displayLocale ] ) ;
2424+ } , [ formatCategoryLabel , isMobile ] ) ;
23332425
23342426 // Memoize whether any X-axis label is long enough to warrant angle rotation
23352427 const hasLongLabels = React . useMemo (
@@ -3477,6 +3569,12 @@ function AdvancedChartImplInner({
34773569
34783570 // Horizontal bar — swap X/Y axis types and orientation.
34793571 const isHorizontal = chartType === 'horizontal-bar' ;
3572+ // Its category axis draws each label in full (no phone cut), wrapped into
3573+ // this width by `WrappedCategoryTick`.
3574+ const categoryTickFormatter = formatterFor ( xAxisSpec ?. format , displayLocale ) ?? formatCategoryLabel ;
3575+ const categoryAxisWidth = isHorizontal
3576+ ? Math . min ( 140 , Math . max ( 60 , Math . ceil ( Math . max ( 0 , ...data . map ( ( d ) => estimateLabelWidthPx ( String ( categoryTickFormatter ( d ?. [ xAxisKey ] ) ?? '' ) ) ) ) ) + CATEGORY_TICK_INSET_PX ) )
3577+ : 0 ;
34803578
34813579 // Build vertical fill gradients (bar + area) for every colour in play, so
34823580 // fills read as a polished ramp instead of flat blocks (大屏 look). Inline
@@ -3492,6 +3590,30 @@ function AdvancedChartImplInner({
34923590 ] ) ) ;
34933591 const gslug = ( c : string ) => 'g' + c . replace ( / [ ^ a - z A - Z 0 - 9 ] / g, '' ) ;
34943592
3593+ // A series' colour. A comparison series mirrors the colour of the primary
3594+ // series it overlays rather than taking a fresh palette slot: the first
3595+ // non-comparison series above it.
3596+ const seriesColorAt = ( sIdx : number ) : string => {
3597+ const s = series [ sIdx ] ;
3598+ const isComparison = s ?. variant === 'comparison' ;
3599+ const baseSeries = isComparison
3600+ ? ( series . slice ( 0 , sIdx ) . find ( ( p : any ) => p . variant !== 'comparison' ) || series [ 0 ] )
3601+ : s ;
3602+ const baseIdx = isComparison ? series . indexOf ( baseSeries ) : sIdx ;
3603+ return resolveColor ( config [ baseSeries . dataKey ] ?. color || _gpal [ baseIdx % _gpal . length ] || DEFAULT_CHART_COLOR ) ;
3604+ } ;
3605+ // The legend's swatch per series. A bar or area mark fills from a gradient,
3606+ // so the colour recharts hands the legend is a paint-server reference no
3607+ // swatch can draw; the swatch takes the series colour instead. A comparison
3608+ // swatch is drawn hollow, the way its mark is muted, so it reads apart from
3609+ // the primary series it shares a colour with.
3610+ const legendSwatches : Record < string , LegendSwatch > = Object . fromEntries (
3611+ series . map ( ( s : any , sIdx : number ) => [
3612+ String ( s . dataKey ) ,
3613+ { color : seriesColorAt ( sIdx ) , comparison : s . variant === 'comparison' } ,
3614+ ] ) ,
3615+ ) ;
3616+
34953617 return (
34963618 < ChartFrame title = { title } subtitle = { subtitle } >
34973619 < ChartFootnote note = { joinNotes ( xPositionNote , yPositionNote ) } >
@@ -3543,14 +3665,18 @@ function AdvancedChartImplInner({
35433665 `position` included: `left` / `right` are its sides here
35443666 (objectui#10587, see `placeXAxis`). Its `title` follows them too,
35453667 laid out for an axis running down the plot (objectui#10654). */ }
3668+ { /* The category labels wrap inside the axis width and carry the
3669+ full label as a tooltip when cut (`WrappedCategoryTick`); the
3670+ width is sized from the widest label, between 60 and 140 px. */ }
35463671 < YAxis
35473672 type = "category"
35483673 dataKey = { xAxisKey }
3549- tick = { AXIS_TICK_STYLE }
3674+ tick = { ( tickProps : any ) => (
3675+ < WrappedCategoryTick { ...tickProps } maxWidthPx = { categoryAxisWidth - CATEGORY_TICK_INSET_PX } formatLabel = { categoryTickFormatter } />
3676+ ) }
35503677 tickLine = { false }
35513678 axisLine = { false }
3552- width = { Math . min ( 140 , Math . max ( 60 , Math . max ( ...data . map ( d => String ( d [ xAxisKey ] ?? '' ) . length ) ) * 7 ) ) }
3553- tickFormatter = { xTickFormatter }
3679+ width = { categoryAxisWidth }
35543680 { ...( xAxisDown ? { orientation : xAxisDown } : { } ) }
35553681 { ...( xAxisSpec ?. title ? { label : { value : xAxisSpec . title , ...yAxisTitleLayoutFor ( xAxisDown ) } } : { } ) }
35563682 />
@@ -3608,23 +3734,15 @@ function AdvancedChartImplInner({
36083734 { tooltipsEnabled ? < ChartTooltip content = { < ChartTooltipContent /> } /> : null }
36093735 { legendVisible ? (
36103736 < ChartLegend
3611- content = { < ChartLegendContent /> }
3737+ content = { < ChartLegendContent swatches = { legendSwatches } /> }
36123738 { ...( isMobile && { verticalAlign : "bottom" , wrapperStyle : { fontSize : '11px' , paddingTop : '8px' } } ) }
36133739 />
36143740 ) : null }
36153741 { annotationEls }
36163742 { brushEl }
36173743 { series . map ( ( s : any , sIdx : number ) => {
3618- const palette = getPalette ( ) ;
3619- // Comparison series should mirror the color of the primary series
3620- // they overlay, not be assigned a fresh palette color. Find the
3621- // first non-comparison series above this one and reuse its color.
36223744 const isComparison = s . variant === 'comparison' ;
3623- const baseSeries = isComparison
3624- ? ( series . slice ( 0 , sIdx ) . find ( ( p : any ) => p . variant !== 'comparison' ) || series [ 0 ] )
3625- : s ;
3626- const baseIdx = isComparison ? series . indexOf ( baseSeries ) : sIdx ;
3627- const seriesColor = resolveColor ( config [ baseSeries . dataKey ] ?. color || palette [ baseIdx % palette . length ] || DEFAULT_CHART_COLOR ) ;
3745+ const seriesColor = seriesColorAt ( sIdx ) ;
36283746
36293747 // Spec `series[].yAxis` binds this series to a value-axis slot. Only
36303748 // meaningful once a second axis is declared, and bound to the id the
0 commit comments