@@ -37,6 +37,7 @@ import {
3737 ReferenceLine ,
3838 ReferenceArea ,
3939 Brush ,
40+ Text ,
4041 type SankeyLinkProps ,
4142 type SankeyNode ,
4243 type SankeyNodeProps ,
@@ -535,6 +536,22 @@ function wrapCategoryLabel(label: string, maxWidthPx: number): { lines: string[]
535536 */
536537const CATEGORY_TICK_INSET_PX = RECHARTS_TICK_SIZE + 2 ;
537538
539+ /** An unrotated x-axis label on a phone is cut past this many characters. */
540+ const PHONE_X_LABEL_MAX_CHARS = 8 ;
541+
542+ /**
543+ * The x axis's category tick: recharts' own tick text, drawn from the axis's
544+ * `tickFormatter`, plus the full label (`fullLabel`) as a tooltip whenever the
545+ * formatter had to cut it, so a phone's "In Progr…" still names its bucket.
546+ */
547+ function TitledCategoryTick ( props : any ) {
548+ const { fullLabel, tickFormatter, payload, index, ...rest } = props ;
549+ const shown = String ( tickFormatter ? tickFormatter ( payload ?. value , index ) : ( payload ?. value ?? '' ) ) ;
550+ const full = String ( fullLabel ( payload ?. value ) ?? '' ) ;
551+ const text = < Text { ...rest } fill = { AXIS_TICK_STYLE . fill } > { shown } </ Text > ;
552+ return shown === full ? text : < g > < title > { full } </ title > { text } </ g > ;
553+ }
554+
538555/** The line advance of a wrapped category label, in em. */
539556const CATEGORY_LABEL_LINE_EM = 1.1 ;
540557
@@ -2365,8 +2382,8 @@ function AdvancedChartImplInner({
23652382
23662383 // Format ISO date strings into compact "MMM D" / "MMM YYYY" labels for X-axis ticks.
23672384 // Falls back to the raw value when not parseable as a date. The full label:
2368- // `formatTick` below adds the phone cut, which the horizontal-bar category
2369- // axis does not take (it wraps instead, see `WrappedCategoryTick`).
2385+ // the x axis cuts it only where its band needs that (`xAxisTickFormatter`),
2386+ // and the horizontal-bar category axis wraps it ( `WrappedCategoryTick`).
23702387 const formatCategoryLabel = React . useCallback ( ( value : any ) : string => {
23712388 if ( value == null || value === '' ) return '' ;
23722389 // A resolved display label for this exact category value — the same
@@ -2418,11 +2435,6 @@ function AdvancedChartImplInner({
24182435 }
24192436 return str ;
24202437 } , [ data , xAxisKey , config , displayLocale ] ) ;
2421- const formatTick = React . useCallback ( ( value : any ) : string => {
2422- const str = formatCategoryLabel ( value ) ;
2423- if ( isMobile && str . length > 8 ) return str . slice ( 0 , 8 ) + '…' ;
2424- return str ;
2425- } , [ formatCategoryLabel , isMobile ] ) ;
24262438
24272439 // Memoize whether any X-axis label is long enough to warrant angle rotation
24282440 const hasLongLabels = React . useMemo (
@@ -2495,8 +2507,8 @@ function AdvancedChartImplInner({
24952507 yAxisUndrawnNotes ( yPlacement , yAxes , categoriesRunDown ) ,
24962508 ) ;
24972509 const xTickFormatter = React . useMemo (
2498- ( ) => formatterFor ( xAxisSpec ?. format , displayLocale ) ?? formatTick ,
2499- [ xAxisSpec ?. format , displayLocale , formatTick ] ,
2510+ ( ) => formatterFor ( xAxisSpec ?. format , displayLocale ) ?? formatCategoryLabel ,
2511+ [ xAxisSpec ?. format , displayLocale , formatCategoryLabel ] ,
25002512 ) ;
25012513
25022514 // objectui#13241 — whether the drawn labels fit their bands unrotated, read
@@ -2525,19 +2537,33 @@ function AdvancedChartImplInner({
25252537 * is sized from the longest label drawn, so the cap is what keeps that band
25262538 * bounded. See `ROTATED_X_LABEL_MAX_CHARS`.
25272539 *
2528- * Kept separate from `xTickFormatter` on purpose — that one also formats the
2529- * horizontal-bar family's CATEGORY axis, which is the y axis, sizes its own
2530- * width from the longest label, and already draws all of them.
2540+ * An unrotated label on a phone keeps the eight-character cut, unless the
2541+ * axis declares a `format`: there the rotated band is what holds a long
2542+ * label, so a phone rotates its few labels and draws "In Progress" whole
2543+ * where the cut drew "In Progr…" (objectstack#27520). Either cut leaves the
2544+ * full label in the tick's tooltip (`TitledCategoryTick`).
2545+ *
2546+ * Kept separate from `xTickFormatter` on purpose — that one is the full
2547+ * label, which the fit and rotation decisions above measure, and which the
2548+ * tooltip of a cut label carries.
25312549 */
25322550 const xAxisTickFormatter = React . useMemo ( ( ) => {
2533- if ( ! rotateXLabels ) return xTickFormatter ;
2534- return ( value : any ) : string => {
2535- const label = String ( xTickFormatter ( value ) ?? '' ) ;
2536- return label . length > ROTATED_X_LABEL_MAX_CHARS
2537- ? `${ label . slice ( 0 , ROTATED_X_LABEL_MAX_CHARS - 1 ) } …`
2538- : label ;
2539- } ;
2540- } , [ rotateXLabels , xTickFormatter ] ) ;
2551+ if ( rotateXLabels ) {
2552+ return ( value : any ) : string => {
2553+ const label = String ( xTickFormatter ( value ) ?? '' ) ;
2554+ return label . length > ROTATED_X_LABEL_MAX_CHARS
2555+ ? `${ label . slice ( 0 , ROTATED_X_LABEL_MAX_CHARS - 1 ) } …`
2556+ : label ;
2557+ } ;
2558+ }
2559+ if ( isMobile && ! xAxisSpec ?. format ) {
2560+ return ( value : any ) : string => {
2561+ const label = String ( xTickFormatter ( value ) ?? '' ) ;
2562+ return label . length > PHONE_X_LABEL_MAX_CHARS ? `${ label . slice ( 0 , PHONE_X_LABEL_MAX_CHARS ) } …` : label ;
2563+ } ;
2564+ }
2565+ return xTickFormatter ;
2566+ } , [ rotateXLabels , isMobile , xAxisSpec ?. format , xTickFormatter ] ) ;
25412567
25422568 // objectui#11796 — the band a rotated x axis reserves, from the longest
25432569 // label it draws (see `rotatedXAxisHeight`). A number, so the axis props
@@ -2737,7 +2763,7 @@ function AdvancedChartImplInner({
27372763 // 180-point daily series at 800px still thinned to 11 ticks with zero
27382764 // overlap, unchanged in kind from before this change.
27392765 const xAxisCommonProps = React . useMemo ( ( ) => ( {
2740- tick : AXIS_TICK_STYLE ,
2766+ tick : ( tickProps : any ) => < TitledCategoryTick { ... tickProps } fullLabel = { xTickFormatter } /> ,
27412767 tickLine : false as const ,
27422768 tickMargin : X_AXIS_TICK_MARGIN ,
27432769 axisLine : false as const ,
@@ -2761,7 +2787,7 @@ function AdvancedChartImplInner({
27612787 textAnchor : 'end' as const ,
27622788 height : rotatedXAxisBand ,
27632789 } ) ,
2764- } ) , [ labelEveryBucket , rotateXLabels , xAxisTickFormatter , xAxisSpec ?. title , xAxisAcross , rotatedXAxisBand ] ) ;
2790+ } ) , [ labelEveryBucket , rotateXLabels , xAxisTickFormatter , xTickFormatter , xAxisSpec ?. title , xAxisAcross , rotatedXAxisBand ] ) ;
27652791
27662792 // #2942 — the non-series spec families used to fall through the component
27672793 // map's `|| BarChart` into a bar shell whose series marks all returned
@@ -3608,10 +3634,16 @@ function AdvancedChartImplInner({
36083634 // swatch can draw; the swatch takes the series colour instead. A comparison
36093635 // swatch is drawn hollow, the way its mark is muted, so it reads apart from
36103636 // the primary series it shares a colour with.
3637+ // A lone bar series painted per category draws no swatch: no one colour
3638+ // names it, and the bars it would sit beside are each another one.
3639+ const perCategoryBars = ( chartType === 'bar' || chartType === 'horizontal-bar' )
3640+ && ! ! categoryColors && series . length === 1 && series [ 0 ] ?. variant !== 'comparison' && data . length > 1 ;
36113641 const legendSwatches : Record < string , LegendSwatch > = Object . fromEntries (
36123642 series . map ( ( s : any , sIdx : number ) => [
36133643 String ( s . dataKey ) ,
3614- { color : seriesColorAt ( sIdx ) , comparison : s . variant === 'comparison' } ,
3644+ perCategoryBars
3645+ ? { color : seriesColorAt ( sIdx ) , none : true }
3646+ : { color : seriesColorAt ( sIdx ) , comparison : s . variant === 'comparison' } ,
36153647 ] ) ,
36163648 ) ;
36173649
@@ -3769,8 +3801,7 @@ function AdvancedChartImplInner({
37693801 // that has one keeps it, every other bar takes the series colour.
37703802 // With a comparison overlay the chart has two series, so each
37713803 // keeps its own series colour.
3772- const primaryCount = series . filter ( ( p : any ) => p . variant !== 'comparison' ) . length ;
3773- const semanticPerCategory = ! ! categoryColors && primaryCount === 1 && ! isComparison && series . length === 1 && data . length > 1 ;
3804+ const semanticPerCategory = perCategoryBars ;
37743805 const pres = seriesStyle ( s , 'bar' ) ;
37753806 return (
37763807 < Bar key = { s . dataKey } dataKey = { s . dataKey } fill = { `url(#bg-${ gslug ( seriesColor ) } )` } radius = { 4 } fillOpacity = { pres . fillOpacity } strokeOpacity = { pres . strokeOpacity } strokeDasharray = { pres . strokeDasharray } { ...stackProps } { ...axisProps } { ...animProps } { ...markClickProps ( s ) } >
0 commit comments