Pie Chart

Shows how parts make up a whole, as slices of a circle.

Usage

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

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

Examples

No Stroke

Remove the separating stroke between pie sectors.

Label Inside

Show each value inside its pie sector.

Label Outside

Display each name and percentage just outside its sector.

Legend

Add a swatch legend for the three named sectors.

linuxmacwindows

Point Label

Place value labels at the ends of guide lines.

Start Angle

Sweep the language distribution across the upper half of a circle.

Props

Data

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

Slices

PropDefaultTypeDescription
innerRadius0numberHole radius, in pixels. Leave at zero for a pie.
outerRadiusSee belownumberPie 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, so not on a pie.
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.

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

Labels and center text

PropDefaultTypeDescription
label—'inside' | 'outside' | 'point'Label on each slice.
labelFormatter—functionFormat each label.
centerTitle—stringLarge text at the center.
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}
  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.

centerTitle and centerDescription are meant for a donut. On a pie the text is drawn on top of the slices.

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 pie 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-pie" data={data} label="inside" legend />
Code
.share-pie {
  --leverui-radial-chart-sector-stroke: #f0faf8;
  --leverui-radial-chart-legend-gap: 1rem;
}

.share-pie .leverui-radial-chart__label {
  font-weight: 600;
}

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: pie with no hole, donut with one.
  • data-label-placement: the value of label.
  • data-legend: present when legend is on.
  • data-detached: present when detached is on.