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
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
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
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
<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
<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>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>
);
}<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('-') } } }) --><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>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
| Property | Type | Default |
|---|---|---|
segmentsproperty only | { label, value, tone }[] | [] |
value | string | "" |
label | string | "Chart" |
| Event | When |
|---|---|
valuechange | The selected label changed; detail is the label, or "" for none. |
| Slot | For |
|---|---|
(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.