本文へ移動
日本語

仕組み

Every segment is the same circle path starting at 12 o'clock with pathLength="1", rotated to its start and revealed with stroke-dashoffset, with round caps and a 5px gap between neighbours. The entrance is one clockwise sweep on a single ease-out curve: each segment gets its own slice of it as an exact cubic-bezier, so it stays pure CSS. After the entrance, data changes move all segments together from where they are. The centre total and legend values count on the same curve.

向いている場面

  • What a whole is made of, in 2 to 5 parts with clear differences: spending by category, time by activity.
  • Progress toward a goal on a card that also explains it: a course, a savings goal, a profile to complete.

向かない場面

  • More than 5 or 6 parts, or close values to compare: use ranked bars.
  • Exactly used versus left with a legend: the gauge chart says it more directly.
  • Shares over time: use a stacked bar chart.

使い方

import { DonutChart } from './DonutChart.jsx'

export function SpendingCard() {
  return (
    <DonutChart
      title="Spending"
      qualifier="November"
      centerLabel="Spent"
      formatValue={(n) => `$${n.toLocaleString('en-US')}`}
      data={[
        { name: 'Housing', value: 1240 },
        { name: 'Food', value: 620 },
        { name: 'Transport', value: 310 },
        { name: 'Leisure', value: 186 },
        { name: 'Other', value: 124 },
      ]}
    />
  )
}

export function CourseCard() {
  return (
    <DonutChart
      title="Course progress"
      tone={8}
      size={120}
      data={[{ name: 'Completed', value: 25 }]}
      total={100}
      subject="Watercolour basics"
      description="Lesson 4 of 16"
      link={{ label: 'Resume lesson', href: '/lessons/4' }}
    />
  )
}

プロパティ

プロパティ型既定値説明
title / qualifierstring—Card title (15px semibold) and grey words after it.
iconReactNode—An 18px icon in a 32px square tinted with the card colour.
onAction / actionIcon / actionLabel / action() => void / 'open' | 'more' / string / ReactNode- / 'open'A round chevron or three-dot button at the right of the title row, its accessible name, or your own element instead.
data{ name: string, value: number, color?: string }[]—Parts clockwise from 12 o'clock. Several items are coloured --ds-chart-N in order starting at tone; a single item takes the tone colour. Zero is not drawn, negatives count as 0, beyond 8 items the rest merge into “Other”.
totalnumber—The whole. When larger than the sum, the rest is a faint track: one item and total={100} is a progress ring.
center'total' | 'percent''percent' for one item, otherwise 'total'What the centre shows at rest: the sum, or the sum as a share of total.
centerLabelstring—Grey caption under the centre number.
subject / description / linkstring / string / { label, href?, onClick? }—Content beside the ring instead of the legend: a heading, a sentence and a link or button, as on a course card.
showLegendbooleantrueShows the legend (dot, name, counted value, share) beside a donut of two or more items.
tone1-81Card colour: the single-item ring, the icon tint and the link; several items start their colours here.
size / thicknessnumber132 / 14Ring diameter and stroke width in px.
formatValue / formatPercent(n) => stringtoLocaleString('en-US') / `${Math.round(p)}%`Number and share formats for the centre, legend and hidden table.
textobject—Replaces built-in words (total, other, open, more, ring, announce); shallow-merged over the English defaults.
labelstring—Accessible name of the ring and caption of the hidden table; defaults to the title.
replayKeyany—Change it to play the sweep again from zero.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the card.

アクセシビリティ

  • The ring is a focusable group whose name gives the chart name, total and number of parts; arrow keys step through parts, Home and End jump, Escape clears, and each step is announced.
  • Legend rows are buttons; focusing or hovering one highlights its segment and shows it in the centre.
  • A visually hidden table lists every part with its value and share, so colour and hover are never needed to read the data.
  • With reduced motion the ring and numbers appear complete at once.

モーション

When 30% of the card is on screen the segments sweep clockwise in order within 900ms, as one ease-out stroke; the centre number and legend values count up on the same curve. Later changes move segments together in 480ms on cubic-bezier(0.22, 1, 0.36, 1); hovering softens the other segments in 160ms and fades the new centre text in.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

DonutChart.jsxdonut-chart.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

グラフ
「三辺測位」は Pro に含まれます。解除するとプレビューが表示されます。