Segment bar
One bar split into toned segments, with a legend of values and shares and a total.
Demo
Usage
import { SegmentBar } from '@tuttn/slant'
// Parts of a whole: each segment's share of the bar is its share of the sum.
<SegmentBar label="Tokens this run" unit="tokens" segments={[
{ id: 'input', label: 'Input', value: 1240 },
{ id: 'cached', label: 'Cached', value: 3120 },
{ id: 'output', label: 'Output', value: 410 },
]} />
// Against a larger whole: the rest of the bar stays empty, the header reads
// "310 of 500 USD", and the legend lists what remains.
<SegmentBar label="Budget" unit="USD" total={500} format={value => value.toLocaleString('en')}
segments={[{ id: 'spent', label: 'Spent', value: 310, tone: 'warning' }]} />
// Thin, with the values in a hidden list instead of the legend.
<SegmentBar label="Disk" unit="GB" size="sm" legend={false} segments={disk} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
segments | readonly SegmentBarSegment[] | — | The segments, in order from the inline start. |
label | string | — | The bar's accessible name and visible caption: "Tokens by kind". |
unit | string | — | Read after each value and the total, e.g. "tokens". |
format | (value: number) => string | value => String(value) | Formats each value and the total. |
total | number | — | The whole the segments are part of, when it is more than their sum (a budget, a quota): the rest of the bar stays empty and the legend lists what remains. |
legend | boolean | true | Lists each segment with its value and share under the bar. |
size | 'sm' | 'md' | 'lg' | 'md' | The bar's thickness. |
SegmentBarSegment | { id: string; label: string; value: number; color?: 1 | 2 | 3 | 4 | 5; tone?: Tone } | — | One entry of segments. tone wins over color; by default a segment takes the colour slot of its position. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root <div>, the group. |
Accessibility
- The root is a group named by its visible caption (
aria-labelledby), so the label is shown and read the same. - The bar itself is hidden from screen readers. The legend is a list that holds every segment’s name, value with
unit, and share (messages.chartShare, a whole percent), so nothing rests on colour or on hover; withlegend={false}the same list is there, visually hidden. - The header reads the total as
messages.segmentTotaland the sum (“Total 4770 tokens”), or withtotal, asmessages.segmentOf(“310 of 500 USD”); the legend then addsmessages.segmentRemainingwith what is left. - Segments take
--sl-chart-1to--sl-chart-5by position, or theircolorslot;tonegives a semantic colour instead. The legend’s keys are slanted pixels in the same colour, and its text stays in the text colours on every theme.
The segments share the bar in proportion to their values, in order from the inline start, separated by a 2px gap of the surface behind, with the track’s ends rounded. A zero value is listed in the legend at 0% but takes no room; a negative or non-finite one is left out of the bar, the total and the legend.
With total above the sum, the shares are of the total and what remains is the empty track. A sum above total fills the bar (the shares are then of the sum), the header still reads “600 of 500 USD”, and nothing remains.
Hovering a segment or its legend row lights both and fades the others. It is visual only: the legend already says every value. The bar is HTML, not a physical chart, so under dir="rtl" it fills from the right and the legend follows. size sets the bar’s thickness: 6, 10 or 16px.