Pie Chart

A series as slices of a whole, each in its category's color: a pie, a donut with your text in its center, rings. Names and colors come from the chart's config; the keyboard moves through the rows, and the tooltip says their values.

ChartsExperimental

Installation

It has its own entry point, @semantic-components/ui/charts, built on d3-scale d3-shape — an optional peer of ui: install it where you use the component.

ts
import { ScChart, ScChartPlot, ScChartPie } from '@semantic-components/ui/charts';

Usage

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  type ScChartConfig,  ScChartLegend,  ScChartLegendIndicator,  ScChartLegendItem,  ScChartPie,  ScChartPlot,  ScChartTooltip,  ScChartTooltipIndicator,  ScChartTooltipItem,  ScChartTooltipName,  ScChartTooltipPortal,  ScChartTooltipProvider,  ScChartTooltipValue,} from '@semantic-components/ui/charts';
@Component({  selector: 'app-pie-chart-usage-demo',  imports: [    ScChart,    ScChartPlot,    ScChartPie,    ScChartTooltip,    ScChartTooltipPortal,    ScChartTooltipProvider,    ScChartLegend,    DecimalPipe,    ScChartTooltipIndicator,    ScChartTooltipItem,    ScChartTooltipName,    ScChartTooltipValue,    ScChartLegendItem,    ScChartLegendIndicator,  ],  template: `    <div      scChart      class="mx-auto aspect-square max-h-72"      [config]="config"      [data]="data"    >      <svg scChartPlot categoryKey="browser" aria-label="Visitors by browser">        <g scChartPie key="visitors" />      </svg>      <div scChartTooltipProvider>        <ng-template scChartTooltipPortal>          <div scChartTooltip #tooltip="scChartTooltip">            @for (item of tooltip.payload(); track item.dataKey) {              <div scChartTooltipItem [item]="item">                <span scChartTooltipIndicator></span>                <span scChartTooltipName>{{ item.name }}</span>                <span scChartTooltipValue>{{ item.value | number }}</span>              </div>            }          </div>        </ng-template>      </div>      <div scChartLegend #legend="scChartLegend" class="flex-wrap">        @for (item of legend.payload(); track item.dataKey) {          <div scChartLegendItem [item]="item">            <span scChartLegendIndicator></span>            {{ item.name }}          </div>        }      </div>    </div>  `,  host: { class: 'block w-full max-w-xl' },  encapsulation: ViewEncapsulation.None,})export class PieChartUsageDemo {  private readonly data = [    { browser: 'chrome', visitors: 275 },    { browser: 'safari', visitors: 200 },    { browser: 'firefox', visitors: 187 },    { browser: 'edge', visitors: 173 },    { browser: 'other', visitors: 90 },  ];  private readonly config: ScChartConfig = {    visitors: { label: 'Visitors' },    chrome: { label: 'Chrome', color: 'var(--chart-1)' },    safari: { label: 'Safari', color: 'var(--chart-2)' },    firefox: { label: 'Firefox', color: 'var(--chart-3)' },    edge: { label: 'Edge', color: 'var(--chart-4)' },    other: { label: 'Other', color: 'var(--chart-5)' },  };}

Anatomy

A chart (its rows and config), its plot with the series, and the tooltip and legend you write from their state.

import { ScChart, ScChartPlot, ScChartPie, ScChartTooltip, ScChartTooltipIndicator, ScChartTooltipItem, ScChartTooltipName, ScChartTooltipPortal, ScChartTooltipProvider, ScChartTooltipValue, ScChartLegend, ScChartLegendIndicator, ScChartLegendItem } from '@semantic-components/ui/charts';
<div scChart [config]="…" [data]="…">  <svg scChartPlot categoryKey="…" aria-label="…">    <g scChartPie key="…" />  </svg>  <div scChartTooltipProvider>    <ng-template scChartTooltipPortal>      <div scChartTooltip #tooltip="scChartTooltip">        <div scChartTooltipItem [item]="…">          <span scChartTooltipIndicator></span>          <span scChartTooltipName>…</span>          <span scChartTooltipValue>…</span>        </div>      </div>    </ng-template>  </div>  <div scChartLegend #legend="scChartLegend">    <div scChartLegendItem [item]="…">      <span scChartLegendIndicator></span>    </div>  </div></div>

Accessibility

  • The plot is one tab stop, a group named by its aria-label: the arrow keys, Home and End move through its slices; Escape closes the tooltip.
  • As the keyboard moves, screen readers hear what the tooltip says, through the CDK's LiveAnnouncer (politely).
  • Right to left, with the app following the direction (charts.followDirection), it is mirrored — from 9 o’clock, clockwise — and ← goes to the next.
  • With reduced motion (prefers-reduced-motion), it appears and changes at once.

Examples

Donut, its total in the center

0 Visitors
import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import { ScNumberTicker } from '@semantic-components/ui';import {  ScChart,  ScChartCenter,  type ScChartConfig,  ScChartPie,  ScChartPlot,  ScChartTooltip,

Rings, two series

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  type ScChartConfig,  ScChartPie,  ScChartPlot,  ScChartTooltip,  ScChartTooltipIndicator,  ScChartTooltipItem,

With labels

import { DecimalPipe } from '@angular/common';import { Component, ViewEncapsulation } from '@angular/core';import {  ScChart,  type ScChartConfig,  ScChartLabels,  ScChartPie,  ScChartPlot,  ScChartTooltip,  ScChartTooltipIndicator,

RTL

Chrome
Safari
Firefox
Edge
أخرى
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation } from '@angular/core';import {  ScChart,  type ScChartConfig,  ScChartLegend,  ScChartLegendIndicator,  ScChartLegendItem,  ScChartPie,  ScChartPlot,

API reference

The series' inputs. The chart, its plot, axes, tooltip and legend are on the Chart page.

InputTypeDefaultDescription
keystring (required)— each slice's value
innerRadiusnumber0 its hole, a share of the radius
outerRadiusnumber1 its edge, a share of the radius
padAnglenumber0 the gap between slices, in degrees
cornerRadiusnumber— Its slices' corners' rounding, in px — unset, the style's (--sc-chart-bar-radius, as the bars'; square in Lyra and Sera). A whole pie's outer corners only: its slices meet in the middle.
strokeWidthnumber2 the line between slices (0: none)
activeOffsetnumber6 how far the slice pointed at grows
isAnimationActivebooleantrue it sweeps round from its start angle when it appears (Recharts' entrance)
animationBeginnumber400 the wait before it starts, in ms
animationDurationnumber1500 how long it lasts, in ms
animationEasingease, ease-in, ease-out, ease-in-out, linearease
animationMatchBy'index' (by position, stretched), 'append' (the first with the first) or (row, index) => key'append' how its shapes before and after a change are paired (Recharts')
classstring'' Merged over its own classes.