bar-chart
Values compared side by side as horizontal bars, longest-to-shortest if sorted. Each bar has its label and value in text, and the chart is a list screen readers read in order, so nothing depends on seeing the bars. items is [{ label, value }]; unit goes after each value ("%", " visits"). max sets the full length (defaults to the largest value).
This one has no events: it shows what it's given.
Source: bar-chart.zb, and its registry item.
Install
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/bar-chart.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/bar-chart.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/bar-chart.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/bar-chart.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/bar-chart.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/bar-chart.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/bar-chart.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/element/bar-chart.json
npx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/bar-chart.jsonpnpm dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/bar-chart.jsonyarn dlx shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/bar-chart.jsonbunx --bun shadcn@latest add https://zubra-js.github.io/zubra-ui/r/src/bar-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/bar-chart.js"></script>The compiled install writes src/components/zubra/bar-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
<bar-chart label="Visits by source" unit="%"></bar-chart>
<script type="module">
import './components/zubra/bar-chart.js';
const element = document.querySelector('bar-chart');
element.sorted = true;
element.items = [
{ label: 'Search', value: 42 },
{ label: 'Direct', value: 23 },
{ label: 'Social', value: 18 },
{ label: 'Email', value: 11 },
{ label: 'Referral', value: 6 },
];
</script>import './components/zubra/bar-chart.js';
const sorted = true;
const items = [
{ label: 'Search', value: 42 },
{ label: 'Direct', value: 23 },
{ label: 'Social', value: 18 },
{ label: 'Email', value: 11 },
{ label: 'Referral', value: 6 },
];
export function BarChartExample() {
return (
<bar-chart label="Visits by source" unit="%" sorted={sorted} items={items}></bar-chart>
);
}<script setup>
import './components/zubra/bar-chart.js';
const sorted = true;
const items = [
{ label: 'Search', value: 42 },
{ label: 'Direct', value: 23 },
{ label: 'Social', value: 18 },
{ label: 'Email', value: 11 },
{ label: 'Referral', value: 6 },
];
</script>
<template>
<bar-chart
label="Visits by source"
unit="%"
:sorted.prop="sorted"
:items.prop="items"
></bar-chart>
</template>
<!-- vite.config.js: vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } }) --><script>
import './components/zubra/bar-chart.js';
const sorted = true;
const items = [
{ label: 'Search', value: 42 },
{ label: 'Direct', value: 23 },
{ label: 'Social', value: 18 },
{ label: 'Email', value: 11 },
{ label: 'Referral', value: 6 },
];
</script>
<bar-chart label="Visits by source" unit="%" {sorted} {items}></bar-chart>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import './components/zubra/bar-chart.js';
@Component({
selector: 'app-bar-chart-example',
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<bar-chart
label="Visits by source"
unit="%"
[sorted]="sorted"
[items]="items"
></bar-chart>
`,
})
export class BarChartExample {
sorted = true;
items = [
{ label: 'Search', value: 42 },
{ label: 'Direct', value: 23 },
{ label: 'Social', value: 18 },
{ label: 'Email', value: 11 },
{ label: 'Referral', value: 6 },
];
}API
| Property | Type | Default |
|---|---|---|
itemsproperty only | { label, value }[] | [] |
label | string | "" |
unit | string | "" |
max | number | 0 |
sorted | boolean | false |
Size
1.3 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.5 KB.