Zubra UI
Code for
GitHub

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

Compiled (any project)
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
One file, no dependencies (3.4 KB gzipped)
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
.zb source (to edit it with Zubra)
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
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/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

index.html
<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>
Example.jsx
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>
  );
}
Example.vue
<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('-') } } }) -->
Example.svelte
<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>
example.component.ts
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

Properties (each is also an HTML attribute of the same name, unless marked)
PropertyTypeDefault
items
property only
{ label, value }[][]
labelstring""
unitstring""
maxnumber0
sortedbooleanfalse

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.

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