Skip to content

Commit 3797bfb

Browse files
committed
refactor(polar): reuse numberProperty and align radialLabels docs
numericField duplicated the existing numberProperty helper that radialArc uses for its datum defaults. Document the mark with the option table used by the other polar marks, and shorten the friction entry to the format of recent entries.
1 parent 6ac6a14 commit 3797bfb

4 files changed

Lines changed: 85 additions & 131 deletions

File tree

‎API-FRICTION.md‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9324,19 +9324,11 @@ Each entry records:
93249324
- Status: resolved
93259325
- Severity: medium
93269326
- Owner: API
9327-
- Observed in: a production pie with many small adjacent slices. The
9328-
`radialRule` plus `radialText` composition placed every label on its slice
9329-
angle, so neighboring labels overlapped. The application needed its own
9330-
pixel layout, connector paths, and hiding rules outside the polar mark.
9331-
- Decision: add `radialLabels`, which stacks labels on each side of the
9332-
center, moves overlapping labels apart as little as possible, and hides the
9333-
lowest-priority labels when they do not fit. `maxShift` optionally hides
9334-
labels that would move too far from their slice.
9335-
- Verification: unit tests cover ideal placement, minimal spreading, capacity
9336-
hiding by priority, equal-priority ties, `maxShift`, vertical and horizontal
9337-
bounds, long labels, connector geometry, and connectors on the inner side
9338-
of each text column. Rendered chart tests cover connectors clear of other
9339-
labels' text boxes, non-overlapping labels per side, short, narrow, and resized charts, stable
9340-
keys, and SVG, Canvas, and React Native output. A motion test covers moving
9341-
kept labels and connectors and fading removed labels. Charts that do not
9342-
import `radialLabels` keep the same polar bundle size within 0.01 kB gzip.
9327+
- Observed in: issue #227, `radialRule` plus `radialText` labels of small
9328+
adjacent slices overlapped, so the application owned its own label layout
9329+
- Decision: add `radialLabels`, which stacks labels in one column per side,
9330+
moves overlapping labels apart, and hides lower-priority labels that do not
9331+
fit the chart height, width, or optional `maxShift`.
9332+
- Verification: layout and rendered tests cover spacing, bounds, priority
9333+
ties, connectors clear of other labels, stable keys, motion, and SVG,
9334+
Canvas, and React Native output.

‎docs/reference/marks/polar.md‎

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -379,59 +379,43 @@ function radialLabels<TDatum>(
379379
): PolarMark<TDatum>
380380
```
381381

382-
`radialLabels` draws outside labels with connectors for pie and donut slices,
383-
and keeps crowded labels readable. `angle` defaults to the numeric `angle`
384-
field of each row, so `pie()` output needs no angle channel. `radius` defaults
385-
to `1`, the outer edge of a `[0, 1]` radius scale. Connectors start at the
386-
largest mapped radius, bend at an elbow 16 pixels outside it on the slice
387-
angle, and run horizontally to a text column 12 pixels beyond the outermost
388-
visible elbow on that side. The text starts 4 pixels after the column. Every
389-
connector stays on the inner side of the column, so no connector crosses
390-
another label's text.
391-
392-
Labels form one vertical stack on each side of the center, in angular order.
393-
Each label gets `fontSize * 1.4` pixels of height (`fontSize` defaults to
394-
12). When labels overlap, the mark moves them apart as little as
395-
possible, and keeps every label inside the polar container's chart bounds.
396-
When a side has more labels than fit, the labels with the lowest `priority`
397-
are hidden first. `priority` defaults to the numeric `value` field, so the
398-
smallest `pie()` slices are hidden first. On equal priority, the later row is
399-
hidden first. Set `maxShift` to also hide labels that would move more than
400-
that many pixels from their slice; the mark hides the lowest-priority label
401-
in the crowded group and lays out again.
402-
403-
The mark measures each label with the chart's text measurer and keeps every
404-
label inside the left and right chart edges. A label that does not fit even
405-
beside its own elbow is hidden first. While the widest remaining label still
406-
does not fit beside the column, the mark hides the lower-priority one of that
407-
label and the label with the outermost elbow; on equal priority it hides the
408-
outermost-elbow label, because that label sets the column. Long labels and
409-
narrow charts therefore lose labels instead of overflowing; widen the chart,
410-
lower `radiusRatio`, or shorten `text` to keep more labels. Layout runs on every
411-
render, so a resized chart can show labels that a smaller size hid.
412-
413-
The mark also accepts `angleScale`, `radiusScale`, `text`, `key`, `z`,
414-
`color`, fill, font size and weight, opacity, and connector `stroke`,
415-
`strokeOpacity`, and `strokeWidth`. `stroke` defaults to the label fill. Each
416-
visible label emits one interaction point at its final text position. Hidden
417-
labels emit no nodes and no points, so pair the mark with a tooltip or legend
418-
when every slice name must stay available.
419-
420-
The mark emits only label and polyline scene nodes, so SVG, Canvas, and React
421-
Native paint it the same way. Label keys come from `key`; each connector uses
422-
the label key with a `:connector` suffix. With the motion renderer, kept labels
423-
and connectors move to their new layout, and hidden or removed labels fade
424-
out.
425-
426-
```tsx
427-
radialLabels(slices, {
428-
text: (row) => `${row.name} ${row.share}%`,
429-
key: 'name',
430-
color: 'name',
431-
fontSize: 12,
432-
maxShift: 24,
433-
})
434-
```
382+
`radialLabels` renders outside slice labels with elbow connectors and keeps
383+
crowded labels readable.
384+
385+
| Option | Meaning |
386+
| ------------------------------ | -------------------------------------------------------------- |
387+
| `id`, `className` | Stable layer ID and optional class |
388+
| `angle` | Angle channel; defaults to datum `angle` |
389+
| `radius` | Connector start radius; defaults to `1` |
390+
| `text` | Label text |
391+
| `priority` | Lower priority hides first; defaults to datum `value` |
392+
| `maxShift` | Hides a label that would move more pixels from its slice |
393+
| `key` | Stable label identity; defaults to top/nested `id`, then index |
394+
| `z` | Interaction group |
395+
| `color` | Color-scale value; defaults to `z` |
396+
| `fill` | Final constant or datum-derived text paint |
397+
| `fontSize`, `fontWeight` | Text size, default 12, and weight |
398+
| `opacity` | Label and connector opacity |
399+
| `stroke` | Connector paint; defaults to the label fill |
400+
| `strokeOpacity`, `strokeWidth` | Connector opacity and width |
401+
402+
Labels stack on each side of the center in angular order, `fontSize * 1.4`
403+
pixels apart. Overlapping labels move apart as little as possible and stay
404+
inside the chart bounds. Each side places its text in one column outside its
405+
outermost visible connector elbow, so no connector crosses another label.
406+
407+
A label is hidden when its side runs out of height, when its text would cross
408+
the left or right chart edge, or when it would move more than `maxShift`
409+
pixels. Lower-priority labels hide first; on equal priority, the later row
410+
hides first, except that the width check hides the outermost-elbow label
411+
first because it sets the column. Hidden labels emit no nodes or interaction
412+
points, so pair the mark with a tooltip or legend when every slice name must
413+
stay available.
414+
415+
The mark emits label and polyline scene nodes, so SVG, Canvas, and React
416+
Native paint it the same way. Connector keys add `:connector` to the label
417+
key. With the motion renderer, kept labels and connectors move to their new
418+
layout, and hidden labels fade out.
435419

436420
## `radialRule`
437421

‎packages/charts-core/docs/reference/marks/polar.md‎

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -379,59 +379,43 @@ function radialLabels<TDatum>(
379379
): PolarMark<TDatum>
380380
```
381381

382-
`radialLabels` draws outside labels with connectors for pie and donut slices,
383-
and keeps crowded labels readable. `angle` defaults to the numeric `angle`
384-
field of each row, so `pie()` output needs no angle channel. `radius` defaults
385-
to `1`, the outer edge of a `[0, 1]` radius scale. Connectors start at the
386-
largest mapped radius, bend at an elbow 16 pixels outside it on the slice
387-
angle, and run horizontally to a text column 12 pixels beyond the outermost
388-
visible elbow on that side. The text starts 4 pixels after the column. Every
389-
connector stays on the inner side of the column, so no connector crosses
390-
another label's text.
391-
392-
Labels form one vertical stack on each side of the center, in angular order.
393-
Each label gets `fontSize * 1.4` pixels of height (`fontSize` defaults to
394-
12). When labels overlap, the mark moves them apart as little as
395-
possible, and keeps every label inside the polar container's chart bounds.
396-
When a side has more labels than fit, the labels with the lowest `priority`
397-
are hidden first. `priority` defaults to the numeric `value` field, so the
398-
smallest `pie()` slices are hidden first. On equal priority, the later row is
399-
hidden first. Set `maxShift` to also hide labels that would move more than
400-
that many pixels from their slice; the mark hides the lowest-priority label
401-
in the crowded group and lays out again.
402-
403-
The mark measures each label with the chart's text measurer and keeps every
404-
label inside the left and right chart edges. A label that does not fit even
405-
beside its own elbow is hidden first. While the widest remaining label still
406-
does not fit beside the column, the mark hides the lower-priority one of that
407-
label and the label with the outermost elbow; on equal priority it hides the
408-
outermost-elbow label, because that label sets the column. Long labels and
409-
narrow charts therefore lose labels instead of overflowing; widen the chart,
410-
lower `radiusRatio`, or shorten `text` to keep more labels. Layout runs on every
411-
render, so a resized chart can show labels that a smaller size hid.
412-
413-
The mark also accepts `angleScale`, `radiusScale`, `text`, `key`, `z`,
414-
`color`, fill, font size and weight, opacity, and connector `stroke`,
415-
`strokeOpacity`, and `strokeWidth`. `stroke` defaults to the label fill. Each
416-
visible label emits one interaction point at its final text position. Hidden
417-
labels emit no nodes and no points, so pair the mark with a tooltip or legend
418-
when every slice name must stay available.
419-
420-
The mark emits only label and polyline scene nodes, so SVG, Canvas, and React
421-
Native paint it the same way. Label keys come from `key`; each connector uses
422-
the label key with a `:connector` suffix. With the motion renderer, kept labels
423-
and connectors move to their new layout, and hidden or removed labels fade
424-
out.
425-
426-
```tsx
427-
radialLabels(slices, {
428-
text: (row) => `${row.name} ${row.share}%`,
429-
key: 'name',
430-
color: 'name',
431-
fontSize: 12,
432-
maxShift: 24,
433-
})
434-
```
382+
`radialLabels` renders outside slice labels with elbow connectors and keeps
383+
crowded labels readable.
384+
385+
| Option | Meaning |
386+
| ------------------------------ | -------------------------------------------------------------- |
387+
| `id`, `className` | Stable layer ID and optional class |
388+
| `angle` | Angle channel; defaults to datum `angle` |
389+
| `radius` | Connector start radius; defaults to `1` |
390+
| `text` | Label text |
391+
| `priority` | Lower priority hides first; defaults to datum `value` |
392+
| `maxShift` | Hides a label that would move more pixels from its slice |
393+
| `key` | Stable label identity; defaults to top/nested `id`, then index |
394+
| `z` | Interaction group |
395+
| `color` | Color-scale value; defaults to `z` |
396+
| `fill` | Final constant or datum-derived text paint |
397+
| `fontSize`, `fontWeight` | Text size, default 12, and weight |
398+
| `opacity` | Label and connector opacity |
399+
| `stroke` | Connector paint; defaults to the label fill |
400+
| `strokeOpacity`, `strokeWidth` | Connector opacity and width |
401+
402+
Labels stack on each side of the center in angular order, `fontSize * 1.4`
403+
pixels apart. Overlapping labels move apart as little as possible and stay
404+
inside the chart bounds. Each side places its text in one column outside its
405+
outermost visible connector elbow, so no connector crosses another label.
406+
407+
A label is hidden when its side runs out of height, when its text would cross
408+
the left or right chart edge, or when it would move more than `maxShift`
409+
pixels. Lower-priority labels hide first; on equal priority, the later row
410+
hides first, except that the width check hides the outermost-elbow label
411+
first because it sets the column. Hidden labels emit no nodes or interaction
412+
points, so pair the mark with a tooltip or legend when every slice name must
413+
stay available.
414+
415+
The mark emits label and polyline scene nodes, so SVG, Canvas, and React
416+
Native paint it the same way. Connector keys add `:connector` to the label
417+
key. With the motion renderer, kept labels and connectors move to their new
418+
layout, and hidden labels fade out.
435419

436420
## `radialRule`
437421

‎packages/charts-core/src/polar.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ export interface RadialLabelsOptions<TDatum> extends RadialPathOptions<TDatum> {
18361836
text?: Channel<TDatum, string | number | null | undefined>
18371837
/**
18381838
* Labels with lower priority hide first when labels do not fit. Defaults to
1839-
* the numeric `value` field, so `pie()` rows keep their largest slices.
1839+
* datum `value`, so `pie()` rows keep their largest slices.
18401840
*/
18411841
priority?: Channel<TDatum, number | null | undefined>
18421842
fill?: VisualChannel<TDatum, string>
@@ -1916,7 +1916,7 @@ export function radialLabels<TDatum>(
19161916
typeof options.angle === 'number'
19171917
? data.map(() => options.angle as number)
19181918
: channelValues(data, options.angle, (datum) =>
1919-
numericField(datum, 'angle'),
1919+
numberProperty(datum, 'angle'),
19201920
)
19211921
const radiusValues =
19221922
typeof options.radius === 'number'
@@ -1926,7 +1926,7 @@ export function radialLabels<TDatum>(
19261926
datum == null ? '' : String(datum),
19271927
)
19281928
const priorityValues = channelValues(data, options.priority, (datum) =>
1929-
numericField(datum, 'value'),
1929+
numberProperty(datum, 'value'),
19301930
)
19311931
const groups = channelValues(data, options.z, () => null)
19321932
const colorValues =
@@ -2115,12 +2115,6 @@ export function radialLabels<TDatum>(
21152115
)
21162116
}
21172117

2118-
function numericField(datum: unknown, field: string): number | undefined {
2119-
if (datum == null || typeof datum !== 'object') return undefined
2120-
const value = (datum as Record<string, unknown>)[field]
2121-
return typeof value === 'number' ? value : undefined
2122-
}
2123-
21242118
export interface RadialRuleOptions<
21252119
TDatum,
21262120
> extends ChartMarkMotionOptions<never> {

0 commit comments

Comments
 (0)