#7880·recharts

Theme: legible labels drawn on top of filled shapes (Treemap, inside LabelList)

Author: PavelVanecekCreated Sep 29, 2026Updated Sep 29, 2026

Problem

This covers labels drawn on top of a filled shape. It shows up in two ways: Treemap labels in the typography color, and LabelList labels in the series' own color.

Treemap labels in the typography color

Treemap draws its tile labels (and the type="nest" indicator arrow) directly on top of tiles colored from the theme palette. Labels always use the typography color, and no single text color is legible on every palette entry.

After #7879 (Treemap tiles colored by branch), the dark theme draws #f4f4f5 text on mid-light palette tiles, which gives only about 1.4–3.3:1 contrast. WCAG 1.4.3 asks for 4.5:1 for normal-size text. The light theme is better but still not at 4.5:1 everywhere.

Contrast of opaque palette tiles against possible label colors:

Theme Label color Worst tile Contrast
dark typography #f4f4f5 #ffc470 1.43
dark background #18181b #ac71c0 4.96
light typography #18181b #723984 2.22
light background #fff #00928e 3.82

The nest indicator arrow has no fill at all, so it renders in the browser default black, which is invisible on dark tiles.

Labels in the series' own color

A second, worse variant of this affects every LabelList. When no explicit fill is given, the label takes the fill of its data entry (src/component/LabelList.tsx):

typescript
fill={restProps.fill ?? entry.fill}

Bar, Funnel and RadialBar pass entry.fill to their labels, and the default Pie label spreads the sector (including its fill) into the label props. With a theme that fill is the palette color.

  • Outside positions work well and are intentional: Pie's default outside labels, position="top" on a Bar, Scatter and Line labels. The label color identifies the series, and it has at least 3:1 contrast against the background thanks to the palette.
  • Positions overlaid on the shape break down completely: inside, center, insideTop, insideLeft and so on on Bar and RadialBar, Funnel labels (inside by default), and Pie labels with an inside position. The text is painted in the same color as the shape under it, which is 1:1 contrast, so the label is invisible. The source already has a comment acknowledging this.

So the fix has to depend on the position. Keep the series color when the label sits on the background. When the label is overlaid on its own shape, use a color chosen against the shape fill (any of the alternatives below).

Open points for this variant:

  • A Label / LabelList needs to know whether its position is overlaid on the shape. The mapping from position to inside or outside differs between cartesian (LabelPosition) and polar shapes, and insideStart / insideEnd on RadialBar are inside. It could be a small lookup next to the existing position logic.
  • An explicit fill on LabelList / Label must keep winning.
  • Custom label content receives fill in its props. Changing it for inside positions changes what custom content sees, which could break someone. It may need to be theme-only, or opt-in in 3.x.
  • Unthemed charts: the legacy default colors have the same 1:1 problem inside shapes. Fixing it there too is a visible change, so it might be limited to themed charts first.

Other components

Beyond these, the same problem will come up for any component that paints text inside a shape: Treemap, Sankey node labels, and custom content that reads fill.

Alternatives

1. Pick the most legible of a few candidate colors, per tile

For each label, compute the WCAG contrast ratio between the tile fill and each candidate, and use the candidate with the highest ratio. The natural candidates already exist in the theme: the typography color and chart.backgroundColor (one light and one dark color). A prototype of this was in an earlier revision of #7879.

  • Works with any palette, including colors from data.
  • Best-of results with the current palettes: dark theme at least 4.96:1 on every entry. The light theme misses 4.5:1 on three entries: blue 4.38, sky 4.45, green 4.32.
  • Needs a small color parser (hex and rgb()). Named colors, CSS variables, hsl(), gradients and url(#...) can't be evaluated without the DOM, so those fall back to the typography color.
  • Depends on chart.backgroundColor being an opaque color. If it becomes transparent (see #7881), a second candidate has to come from somewhere else.
  • Labels on the same chart switch between dark and light text. That's common (Plotly, Observable), but some people find it busy.

2. Configurable candidate list in the theme

Same as (1), but with an explicit theme key, for example treemap.labelColors: ['#000', '#fff'], or a more general insideLabelColors.

  • Pure black and white together guarantee at least 4.58:1 against any opaque color, which would close the light theme gap.
  • This is new API. It would probably have to be general, not Treemap-specific, because the same need exists in other components.

3. Tune the palettes so that one label color works everywhere

For example, lighten the dark palette so that #18181b text always passes, and darken the light palette so that white text always passes.

  • No runtime computation, and one label color per theme.
  • The palettes are shared by every chart type and are tuned for color vision deficiency separation. Changing them affects everything and is a much bigger design task.
  • Colors defined in data would still be unhandled.

4. Translucent (tinted) tiles with a solid outline

Paint tiles at low fillOpacity over the background and outline them with the full palette color. Text in the typography color then has high contrast everywhere.

  • Needs no color math.
  • Looks very different ("modern dashboard" style, lower visual weight).
  • Treemap draws parents under children, so translucent fills stack and nested tiles come out darker. That needs extra handling, such as not filling parents that have children.
  • Parent labels would show through child tiles.

5. Text halo

Draw the label with a stroke in the opposite color and paint-order: stroke.

  • Legible on nearly anything, including gradients and images.
  • Visually heavy at small font sizes, and still needs a choice of halo color.

6. Do nothing in the library, and document content / typography

Users can already render custom content.

  • The out-of-the-box themed Treemap stays inaccessible, which is the thing we want to fix.

Recommendation

Start with (1), because it needs no new API, and make sure it is implemented once and shared (for example src/theme/pickLegibleColor.ts). Treemap, and LabelList / Label in overlaid positions, should all use it. Outside labels keep the series color. Decide on (2) or (3) separately if the remaining light theme gap needs closing. The arrow should use the same color as the label.

Acceptance

  • test/theme/themeAccessibility.spec.ts checks tile label contrast for every palette entry in both built-in themes.
  • The nest arrow is visible on every tile.
  • Themed Bar, RadialBar, Funnel and Pie labels in inside positions are legible on their own shape. Outside labels keep the series color.
  • Unthemed Treemaps are unchanged.