Donut Chart

Shows how parts make up a whole, as slices of a ring, with room for a title in the middle.

Usage

Code
import { RadialChart } from '@leverui/components';
Code
<RadialChart
  data={[
    { name: 'Solid', value: 60, color: 'teal.solid' },
    { name: 'Other', value: 40, color: 'blue.solid' },
  ]}
  innerRadius={80}
  outerRadius={100}
  aria-label="Framework share"
/>

There is no DonutChart or PieChart export. Set innerRadius above zero for a donut. Leave it out for a Pie Chart.

Examples

Point Label

Place value labels outside the ring with guide lines.

Centered Text

Place the total and its unit at the center of the ring.

1,200users

Angle Padding

Separate three rounded-looking ring segments by eight degrees.

Start And End Angle

Draw a half-circle donut with a thicker ring.

Detached Segment

Expand a sector outward when it receives focus or hover.

Other Label

Combine browser values below 100 into one labeled Other sector.

Props

Data

PropDefaultTypeDescription
data—readonly RadialDatum[]Slices to draw. Required.

Slices

PropDefaultTypeDescription
innerRadius0numberHole radius, in pixels. Above zero for a donut.
outerRadiusSee belownumberOuter radius, in pixels.
startAngle0numberStart of the first slice, in degrees.
endAngle360numberEnd of the last slice, in degrees.
paddingAngle0numberGap after each slice, in degrees.
cornerRadius0numberRound the slice corners. Needs a hole.
strokewhitestringSlice outline color. A CSS color, or none.
strokeWidth1numberSlice outline thickness.
detachedfalsebooleanPush the hovered or focused slice out 20 pixels.

Without outerRadius, the radius is 40% of size after 10 pixels are taken off: 76 at the default size. A legend takes off another 28 pixels.

An innerRadius larger than outerRadius is cut down to match.

Angle 0 points right, at 3 o'clock. Angles grow counterclockwise. startAngle={180} with endAngle={0} draws a half ring across the top.

Labels and center text

PropDefaultTypeDescription
label—'inside' | 'outside' | 'point'Label on each slice.
labelFormatter—functionFormat each label.
centerTitle—stringLarge text in the hole, such as a total.
centerDescription—stringSmall text below centerTitle. Needs centerTitle.

label values: inside (middle of the slice), outside (just past the outer edge), point (past the outer edge, in bold, at the end of a short line).

Labels show name by default. labelFormatter gets the data item and its share of the total, 0 to 1, and returns the text.

Code
<RadialChart
  data={data}
  innerRadius={60}
  outerRadius={80}
  label="outside"
  labelFormatter={(item, share) => `${item.name} ${(share * 100).toFixed(1)}%`}
/>

Labels past the outer edge need room: a larger size, a smaller outerRadius, or plotOffsetX.

Legend and tooltip

PropDefaultTypeDescription
legendfalsebooleanShow a legend along the bottom.
tooltipfalsebooleanShow name and value of the hovered or focused slice.

The legend lists items in the reverse order of data.

Size

PropDefaultTypeDescription
size200numberWidth and height, in pixels.
plotOffsetX0numberMove the ring right, in pixels. Negative moves left.

The chart shrinks to fit a narrower container.

Other attributes and event handlers go to the root element. Without aria-label, the chart is announced as "Radial chart".

Data items

FieldTypeDescription
namestringName in labels, the legend and the tooltip. Required.
valuenumberSlice size, compared with the others. Required.
colorstringColor token such as teal.solid, or a CSS color. Required.

Only an item with a finite value above zero gets a slice.

Keyboard

Each slice takes focus and is announced with its name and value. With tooltip on, a focused slice shows its tooltip.

KeyAction
Tab Shift+TabNext or previous slice.
EscapeClose the tooltip.

Styling

Code
<RadialChart
  class="share-donut"
  data={data}
  innerRadius={80}
  outerRadius={100}
  centerTitle="1,200"
/>
Code
.share-donut {
  --leverui-radial-chart-sector-stroke: #f0faf8;
  --leverui-radial-chart-center-title-font-size: 28px;
}

.share-donut .leverui-radial-chart__centerTitle {
  font-weight: 700;
}

Variables

VariableDefaultDescription
--leverui-radial-chart-sector-strokewhiteSlice outline color.
--leverui-radial-chart-sector-stroke-width1Slice outline thickness.
--leverui-radial-chart-inside-label-colorwhiteColor of inside labels.
--leverui-radial-chart-outside-label-colorMuted text colorColor of outside and point labels.
--leverui-radial-chart-label-font-size12pxLabel text size.
--leverui-radial-chart-label-line-colorStronger border colorColor of the line to a point label.
--leverui-radial-chart-center-colorText colorColor of centerTitle.
--leverui-radial-chart-center-title-font-size32pxText size of centerTitle.
--leverui-radial-chart-center-description-colorMuted text colorColor of centerDescription.
--leverui-radial-chart-center-description-font-size12pxText size of centerDescription.
--leverui-radial-chart-legend-gap12pxSpace between legend items.
--leverui-radial-chart-legend-font-size12pxLegend text size.
--leverui-radial-chart-legend-item-gap5pxSpace between a swatch and its name.
--leverui-radial-chart-legend-swatch-size8pxSwatch width and height.
--leverui-radial-chart-legend-swatch-radius2pxSwatch corner radius.

The stroke and strokeWidth props win over the two slice variables.

VariableDefaultDescription
--leverui-chart-tooltip-bgPanel colorTooltip background.
--leverui-chart-tooltip-padding4px 10pxSpace inside the tooltip.
--leverui-chart-tooltip-gap4pxSpace between rows.
--leverui-chart-tooltip-radius4pxTooltip corner radius.
--leverui-chart-tooltip-shadowMedium shadowTooltip shadow.
--leverui-chart-tooltip-font-size12pxTooltip text size.

Classes

Root: .leverui-radial-chart.

Parts, each prefixed with .leverui-radial-chart__: svg, sector, labels, labelLine, label, center, centerTitle, centerDescription, legend, legendItem, legendSwatch.

Tooltip: .leverui-chart-tooltip. Parts, each prefixed with .leverui-chart-tooltip__: item, swatch, values, name, value.

Root attributes:

  • data-chart-shape: donut with a hole, pie without.
  • data-label-placement: the value of label.
  • data-legend: present when legend is on.
  • data-detached: present when detached is on.