Zubra UI
Code for
GitHub

donut-chart

A donut chart of segments ([{ label, value, tone }], tone 0–7), with the total in the middle and a legend. Choosing a segment (or its legend entry) selects it; again clears it.

Try it: events appear here (valuechange).

Source: donut-chart.zb, and its registry item.

Install

Compiled (any project)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/donut-chart.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/donut-chart.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/donut-chart.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/donut-chart.json
One file, no dependencies (5.2 KB gzipped)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/donut-chart.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/donut-chart.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/donut-chart.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/donut-chart.json
.zb source (to edit it with Zubra)
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/donut-chart.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/donut-chart.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/donut-chart.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/donut-chart.json
No build: a script tag, straight from this site
HTML
<link rel="stylesheet" href="https://zubra-js.github.io/zubra-ui/themes/default.css">
<script type="module" src="https://zubra-js.github.io/zubra-ui/cdn/donut-chart.js"></script>

The compiled install writes src/components/zubra/donut-chart.js and the theme, and adds @zubra/runtime to your dependencies. The one-file install writes the same path as a plain custom element that needs nothing installed (or download it and load it with a <script type="module">); it's smaller on a page with one to three components, the shared runtime with more.

Usage

index.html
<donut-chart label="November spending"></donut-chart>

<script type="module">
  import './components/zubra/donut-chart.js';

  const element = document.querySelector('donut-chart');
  element.segments = [
    { label: 'Rent', value: 1450, tone: 0 },
    { label: 'Food', value: 520, tone: 1 },
    { label: 'Transport', value: 160, tone: 2 },
    { label: 'Fun', value: 240, tone: 3 },
  ];
  element.addEventListener('valuechange', (event) => console.log('valuechange', event.detail));
</script>
Example.jsx
import './components/zubra/donut-chart.js';

const segments = [
  { label: 'Rent', value: 1450, tone: 0 },
  { label: 'Food', value: 520, tone: 1 },
  { label: 'Transport', value: 160, tone: 2 },
  { label: 'Fun', value: 240, tone: 3 },
];

export function DonutChartExample() {
  return (
    <donut-chart
      label="November spending"
      segments={segments}
      onvaluechange={(event) => console.log('valuechange', event.detail)}
    ></donut-chart>
  );
}
Example.vue
<script setup>
import './components/zubra/donut-chart.js';
const segments = [
  { label: 'Rent', value: 1450, tone: 0 },
  { label: 'Food', value: 520, tone: 1 },
  { label: 'Transport', value: 160, tone: 2 },
  { label: 'Fun', value: 240, tone: 3 },
];
const onValuechange = (event) => console.log('valuechange', event.detail);
</script>

<template>
  <donut-chart
    label="November spending"
    :segments.prop="segments"
    @valuechange="onValuechange"
  ></donut-chart>
</template>

<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) -->
Example.svelte
<script>
  import './components/zubra/donut-chart.js';
  const segments = [
    { label: 'Rent', value: 1450, tone: 0 },
    { label: 'Food', value: 520, tone: 1 },
    { label: 'Transport', value: 160, tone: 2 },
    { label: 'Fun', value: 240, tone: 3 },
  ];
</script>

<donut-chart
  label="November spending"
  {segments}
  onvaluechange={(event) => console.log('valuechange', event.detail)}
></donut-chart>
example.component.ts
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/donut-chart.js';

@Component({
  selector: 'app-donut-chart-example',
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  template: `
    <donut-chart
      label="November spending"
      [segments]="segments"
      (valuechange)="onValuechange($event)"
    ></donut-chart>
  `,
})
export class DonutChartExample {
  segments = [
    { label: 'Rent', value: 1450, tone: 0 },
    { label: 'Food', value: 520, tone: 1 },
    { label: 'Transport', value: 160, tone: 2 },
    { label: 'Fun', value: 240, tone: 3 },
  ];

  onValuechange(event: Event) {
    console.log('valuechange', (event as CustomEvent).detail);
  }
}

API

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
segments
property only
{ label, value, tone }[][]
valuestring""
labelstring"Chart"
Events
EventWhen
valuechangeThe selected label changed; detail is the label, or "" for none.
Slots
SlotFor
(default)What's in the middle; the total by default.

Size

1.7 KB gzipped for the component, plus the runtime, which every Zubra component on a page shares (up to 22.1 KB, all of it). On its own, with the parts of the runtime it uses, it's 17.9 KB.

Theme

Every component reads shadcn/ui's CSS variables, so any shadcn theme works. Pick a look or a colour, or paste a theme's CSS.

Look: colours, fonts, corners and motion

Accent colour