Bar Chart
Bar Chart component
Install
npx ui8kit add bar-chartPulls in: box, compose, kit-box, kit-text, text, utils
Preview
Code
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework svelte --dir src --registry-url https://registry.buildy.tw/svelte --strict-cdnnpx ui8kit add bar-chart<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image
for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
-->
<script lang="ts" module>
import type { Snippet } from "svelte";
import type { Bar } from "./bar-chart.types";
export type BarChartProps = {
bars: Bar[];
label: string;
max?: number;
height?: number;
class?: string;
};
</script>
<script lang="ts">
import Box from "../compose/Box.svelte";
import Text from "../compose/Text.svelte";
import { cn, cssValue } from "../utils";
let { bars, label, max, height = 160, class: className }: BarChartProps = $props();
const top = $derived(Math.max(1, (max ?? bars.reduce((m, i) => Math.max(m, i.value), 0))));
</script>
<Box class={cn("min-w-0", className)} data-slot="bar-chart">
<Box class="flex min-w-0 items-end gap-2" role="img" aria-label={label} style={`height:${cssValue(height)}px`}>
{#each bars as b (b.id)}
<Box class="flex h-full min-w-0 flex-1 items-end" title={(b.title ?? b.label)} data-slot="bar">
<Box
class={cn("w-full rounded", (b.muted ? "bg-muted-foreground" : "bg-primary"))}
style={`height:${cssValue(Math.max(2, Math.round(((b.value / top) * 100))))}%`}
data-bar={b.id}
/>
</Box>
{/each}
</Box>
<Box class="mt-2 flex min-w-0 gap-2" aria-hidden="true">
{#each bars as b (b.id)}
<Box class="relative h-4 min-w-0 flex-1" data-slot="bar">
<Text class={"absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground"}>
{b.label}
</Text>
</Box>
{/each}
</Box>
</Box>
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
export type Bar = {
id: string;
label: string;
value: number;
title?: string;
muted?: boolean;
};
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework react --dir src --registry-url https://registry.buildy.tw/react --strict-cdnnpx ui8kit add bar-chart// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
* A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image
* for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
*/
import { type ReactNode } from "react";
import { Box, Text } from "../compose";
import { cn, cssValue } from "../utils";
import type { Bar } from "./bar-chart.types";
export type BarChartProps = {
bars: Bar[];
label: string;
max?: number;
height?: number;
className?: string;
};
export function BarChart({ bars, label, max, height = 160, className }: BarChartProps) {
const top = Math.max(1, (max ?? bars.reduce((m, i) => Math.max(m, i.value), 0)));
return (
<Box className={cn("min-w-0", className)} data-slot="bar-chart">
<Box
className="flex min-w-0 items-end gap-2"
role="img"
aria-label={label}
style={{ height: `${cssValue(height)}px` }}
>
{(bars ?? []).map((b) => (
<Box key={b.id} className="flex h-full min-w-0 flex-1 items-end" title={(b.title ?? b.label)} data-slot="bar">
<Box
className={cn("w-full rounded", (b.muted ? "bg-muted-foreground" : "bg-primary"))}
style={{ height: `${cssValue(Math.max(2, Math.round(((b.value / top) * 100))))}%` }}
data-bar={b.id}
/>
</Box>
))}
</Box>
<Box className="mt-2 flex min-w-0 gap-2" aria-hidden="true">
{(bars ?? []).map((b) => (
<Box key={b.id} className="relative h-4 min-w-0 flex-1" data-slot="bar">
<Text className={"absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground"}>
{b.label}
</Text>
</Box>
))}
</Box>
</Box>
);
}
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
export type Bar = {
id: string;
label: string;
value: number;
title?: string;
muted?: boolean;
};
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework vue --dir src --registry-url https://registry.buildy.tw/vue --strict-cdnnpx ui8kit add bar-chart// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
export type Bar = {
id: string;
label: string;
value: number;
title?: string;
muted?: boolean;
};
<!-- Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT. -->
<!--
A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image
for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
-->
<script lang="ts">
import type { Bar } from "./bar-chart.types";
export type BarChartProps = {
bars: Bar[];
label: string;
max?: number;
height?: number;
class?: string;
};
</script>
<script setup lang="ts">
import { computed } from "vue";
import Box from "../compose/Box.vue";
import Text from "../compose/Text.vue";
import { cn, cssValue } from "../utils";
const { bars, label, max, height = 160, class: className } = defineProps<BarChartProps>();
const top = computed(() => Math.max(1, (max ?? bars.reduce((m, i) => Math.max(m, i.value), 0))));
</script>
<template>
<Box :class="cn('min-w-0', className)" data-slot="bar-chart">
<Box
class="flex min-w-0 items-end gap-2"
role="img"
:aria-label="label"
:style="`height:${cssValue(height)}px`"
>
<Box
v-for="b in bars"
:key="b.id"
class="flex h-full min-w-0 flex-1 items-end"
:title="(b.title ?? b.label)"
data-slot="bar"
>
<Box
:class="cn('w-full rounded', (b.muted ? 'bg-muted-foreground' : 'bg-primary'))"
:style="`height:${cssValue(Math.max(2, Math.round(((b.value / top) * 100))))}%`"
:data-bar="b.id"
/>
</Box>
</Box>
<Box class="mt-2 flex min-w-0 gap-2" aria-hidden="true">
<Box v-for="b in bars" :key="b.id" class="relative h-4 min-w-0 flex-1" data-slot="bar">
<Text :class="'absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground'">
{{ b.label }}
</Text>
</Box>
</Box>
</Box>
</template>
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework solid --dir src --registry-url https://registry.buildy.tw/solid --strict-cdnnpx ui8kit add bar-chart// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
/**
* A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image
* for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
*/
import { For, type JSX } from "solid-js";
import { Box, Text } from "../compose";
import { cn, cssValue } from "../utils";
import type { Bar } from "./bar-chart.types";
export type BarChartProps = {
bars: Bar[];
label: string;
max?: number;
height?: number;
class?: string;
};
export function BarChart({ bars, label, max, height = 160, class: className }: BarChartProps) {
const top = Math.max(1, (max ?? bars.reduce((m, i) => Math.max(m, i.value), 0)));
return (
<Box class={cn("min-w-0", className)} data-slot="bar-chart">
<Box class="flex min-w-0 items-end gap-2" role="img" aria-label={label} style={`height:${cssValue(height)}px`}>
<For each={bars ?? []}>{(b) => (
<Box class="flex h-full min-w-0 flex-1 items-end" title={(b.title ?? b.label)} data-slot="bar">
<Box
class={cn("w-full rounded", (b.muted ? "bg-muted-foreground" : "bg-primary"))}
style={`height:${cssValue(Math.max(2, Math.round(((b.value / top) * 100))))}%`}
data-bar={b.id}
/>
</Box>
)}</For>
</Box>
<Box class="mt-2 flex min-w-0 gap-2" aria-hidden="true">
<For each={bars ?? []}>{(b) => (
<Box class="relative h-4 min-w-0 flex-1" data-slot="bar">
<Text class={"absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground"}>
{b.label}
</Text>
</Box>
)}</For>
</Box>
</Box>
);
}
// Code generated by packages/kit-compose/src/organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
export type Bar = {
id: string;
label: string;
value: number;
title?: string;
muted?: boolean;
};
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework templ --dir src --registry-url https://registry.buildy.tw/templ --strict-cdnnpx ui8kit add bar-chart// Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
// Source: packages/compose-gen/specs/parts/bar-chart.json
package blocks
import (
"github.com/ui8kit/codegen/generated/templ/ui/box"
"github.com/ui8kit/codegen/generated/templ/compose"
"github.com/ui8kit/codegen/generated/templ/ui/text"
uiutils "github.com/ui8kit/codegen/generated/templ/orgutils"
)
// BarChartProps are the props of BarChart; the values are the JSON shapes of the TypeScript props.
type BarChartProps struct {
Bars any `json:"bars"`
Label any `json:"label"`
Max any `json:"max"`
Height any `json:"height"`
Class string `json:"className"`
}
// A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
templ BarChart(p BarChartProps) {
{{ vBars := p.Bars }}
{{ vLabel := p.Label }}
{{ vMax := p.Max }}
{{ vHeight := uiutils.Def(p.Height, float64(160)) }}
{{ vTop := any(uiutils.Max(float64(1), uiutils.Coalesce(vMax, uiutils.MaxBy(vBars, "value")))) }}
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: uiutils.S(uiutils.Cnv("min-w-0", p.Class)), Attrs: uiutils.Attrs("data-slot", "bar-chart")}}) {
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: "flex min-w-0 items-end gap-2", Attrs: uiutils.Attrs("role", "img", "aria-label", vLabel, "style", uiutils.Css("height", vHeight, "px"))}}) {
for _, vB := range uiutils.L(vBars) {
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: "flex h-full min-w-0 flex-1 items-end", Attrs: uiutils.Attrs("title", uiutils.Coalesce(uiutils.Get(vB, "title"), uiutils.Get(vB, "label")), "data-slot", "bar")}}) {
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: uiutils.S(uiutils.Cnv("w-full rounded", uiutils.Cond(uiutils.T(uiutils.Get(vB, "muted")), "bg-muted-foreground", "bg-primary"))), Attrs: uiutils.Attrs("style", uiutils.Css("height", uiutils.Max(float64(2), uiutils.Round(uiutils.Mul(uiutils.Div(uiutils.Get(vB, "value"), vTop), float64(100)))), "%"), "data-bar", uiutils.Get(vB, "id"))}})
}
}
}
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: "mt-2 flex min-w-0 gap-2", Attrs: uiutils.Attrs("aria-hidden", "true")}}) {
for _, vB := range uiutils.L(vBars) {
@compose.Box(compose.BoxProps{BoxProps: box.BoxProps{Class: "relative h-4 min-w-0 flex-1", Attrs: uiutils.Attrs("data-slot", "bar")}}) {
@compose.Text(compose.TextProps{TextProps: text.TextProps{Class: "absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground"}}) {
{ uiutils.Text(uiutils.Get(vB, "label")) }
}
}
}
}
}
}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework twig --dir src --registry-url https://registry.buildy.tw/twig --strict-cdnnpx ui8kit add bar-chart{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
Source: packages/compose-gen/specs/parts/bar-chart.json
A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
Props (p): bars, label, max, height, className. -#}
{%- set v_bars = (p.bars ?? null) -%}
{%- set v_label = (p.label ?? null) -%}
{%- set v_max = (p.max ?? null) -%}
{%- set v_height = (p.height ?? 160) -%}
{%- set v_top = og_max([1, og_coalesce([v_max, og_max_by(v_bars, 'value')])]) -%}
{%- set c_6 -%}
{%- set c_2 -%}
{%- for v_b in og_l(v_bars) %}
{%- set c_1 -%}
{{ include('compose/Box.html.twig', {'class': og_s(og_cnv(['w-full rounded', (og_t(og_get(v_b, ['muted'])) ? 'bg-muted-foreground' : 'bg-primary')])), 'attrs': og_attrs(['style', og_css(['height', og_max([2, og_round(og_mul(og_div(og_get(v_b, ['value']), v_top), 100))]), '%']), 'data-bar', og_get(v_b, ['id'])])}, with_context = false) }}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': 'flex h-full min-w-0 flex-1 items-end', 'attrs': og_attrs(['title', og_coalesce([og_get(v_b, ['title']), og_get(v_b, ['label'])]), 'data-slot', 'bar']), 'children': c_1}, with_context = false) }}
{%- endfor %}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': 'flex min-w-0 items-end gap-2', 'attrs': og_attrs(['role', 'img', 'aria-label', v_label, 'style', og_css(['height', v_height, 'px'])]), 'children': c_2}, with_context = false) }}
{%- set c_5 -%}
{%- for v_b in og_l(v_bars) %}
{%- set c_4 -%}
{%- set c_3 -%}
{{ og_text(og_get(v_b, ['label'])) }}
{%- endset -%}
{{ include('compose/Text.html.twig', {'class': 'absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground', 'children': c_3}, with_context = false) }}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': 'relative h-4 min-w-0 flex-1', 'attrs': og_attrs(['data-slot', 'bar']), 'children': c_4}, with_context = false) }}
{%- endfor %}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': 'mt-2 flex min-w-0 gap-2', 'attrs': og_attrs(['aria-hidden', 'true']), 'children': c_5}, with_context = false) }}
{%- endset -%}
{{ include('compose/Box.html.twig', {'class': og_s(og_cnv(['min-w-0', (p.className ?? '')])), 'attrs': og_attrs(['data-slot', 'bar-chart']), 'children': c_6}, with_context = false) }}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework latte --dir src --registry-url https://registry.buildy.tw/latte --strict-cdnnpx ui8kit add bar-chart{* Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
Source: packages/compose-gen/specs/parts/bar-chart.json
A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
Props (p): bars, label, max, height, className. *}
{var $v_bars = ($p['bars'] ?? null)}
{var $v_label = ($p['label'] ?? null)}
{var $v_max = ($p['max'] ?? null)}
{var $v_height = ($p['height'] ?? 160)}
{var $v_top = og_max([1, og_coalesce([$v_max, og_max_by($v_bars, 'value')])])}
{capture $c_6}
{capture $c_2}
{foreach og_l($v_bars) as $v_b}
{capture $c_1}
{=og_include('compose/Box.latte', ['class' => og_s(og_cnv(['w-full rounded', (og_t(og_get($v_b, ['muted'])) ? 'bg-muted-foreground' : 'bg-primary')])), 'attrs' => og_attrs(['style', og_css(['height', og_max([2, og_round(og_mul(og_div(og_get($v_b, ['value']), $v_top), 100))]), '%']), 'data-bar', og_get($v_b, ['id'])])])|noescape}
{/capture}
{=og_include('compose/Box.latte', ['class' => 'flex h-full min-w-0 flex-1 items-end', 'attrs' => og_attrs(['title', og_coalesce([og_get($v_b, ['title']), og_get($v_b, ['label'])]), 'data-slot', 'bar']), 'children' => (string) $c_1])|noescape}
{/foreach}
{/capture}
{=og_include('compose/Box.latte', ['class' => 'flex min-w-0 items-end gap-2', 'attrs' => og_attrs(['role', 'img', 'aria-label', $v_label, 'style', og_css(['height', $v_height, 'px'])]), 'children' => (string) $c_2])|noescape}
{capture $c_5}
{foreach og_l($v_bars) as $v_b}
{capture $c_4}
{capture $c_3}
{=og_text(og_get($v_b, ['label']))}
{/capture}
{=og_include('compose/Text.latte', ['class' => 'absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground', 'children' => (string) $c_3])|noescape}
{/capture}
{=og_include('compose/Box.latte', ['class' => 'relative h-4 min-w-0 flex-1', 'attrs' => og_attrs(['data-slot', 'bar']), 'children' => (string) $c_4])|noescape}
{/foreach}
{/capture}
{=og_include('compose/Box.latte', ['class' => 'mt-2 flex min-w-0 gap-2', 'attrs' => og_attrs(['aria-hidden', 'true']), 'children' => (string) $c_5])|noescape}
{/capture}
{=og_include('compose/Box.latte', ['class' => og_s(og_cnv(['min-w-0', ($p['className'] ?? '')])), 'attrs' => og_attrs(['data-slot', 'bar-chart']), 'children' => (string) $c_6])|noescape}
Point the CLI at this runtime once, then add the item:
npx ui8kit init --yes --framework jinja2 --dir src --registry-url https://registry.buildy.tw/jinja2 --strict-cdnnpx ui8kit add bar-chart{#- Code generated by packages/kit-compose/src/server-organisms.ts from packages/compose-gen/specs (compose-gen organism emitters on kit-compose). DO NOT EDIT.
Source: packages/compose-gen/specs/parts/bar-chart.json
A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
Props (p): bars, label, max, height, className. -#}
{%- set v_bars = og_def(p['bars'], none) -%}
{%- set v_label = og_def(p['label'], none) -%}
{%- set v_max = og_def(p['max'], none) -%}
{%- set v_height = og_def(p['height'], 160) -%}
{%- set v_top = og_max([1, og_coalesce([v_max, og_max_by(v_bars, 'value')])]) -%}
{%- set c_6 -%}
{%- set c_2 -%}
{%- for v_b in og_l(v_bars) %}
{%- set c_1 -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': og_s(og_cnv(['w-full rounded', ('bg-muted-foreground' if og_t(og_get(v_b, ['muted'])) else 'bg-primary')])), 'Attrs': og_attrs(['style', og_css(['height', og_max([2, og_round(og_mul(og_div(og_get(v_b, ['value']), v_top), 100))]), '%']), 'data-bar', og_get(v_b, ['id'])])}}) }}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': 'flex h-full min-w-0 flex-1 items-end', 'Attrs': og_attrs(['title', og_coalesce([og_get(v_b, ['title']), og_get(v_b, ['label'])]), 'data-slot', 'bar'])}, 'children': c_1}) }}
{%- endfor %}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': 'flex min-w-0 items-end gap-2', 'Attrs': og_attrs(['role', 'img', 'aria-label', v_label, 'style', og_css(['height', v_height, 'px'])])}, 'children': c_2}) }}
{%- set c_5 -%}
{%- for v_b in og_l(v_bars) %}
{%- set c_4 -%}
{%- set c_3 -%}
{{ og_text(og_get(v_b, ['label'])) }}
{%- endset -%}
{{ og_include('compose/text.html.j2', {'p': {'Class': 'absolute left-1/2 top-0 m-0 -translate-x-1/2 whitespace-nowrap text-xs font-medium text-muted-foreground'}, 'children': c_3}) }}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': 'relative h-4 min-w-0 flex-1', 'Attrs': og_attrs(['data-slot', 'bar'])}, 'children': c_4}) }}
{%- endfor %}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': 'mt-2 flex min-w-0 gap-2', 'Attrs': og_attrs(['aria-hidden', 'true'])}, 'children': c_5}) }}
{%- endset -%}
{{ og_include('compose/box.html.j2', {'p': {'Class': og_s(og_cnv(['min-w-0', og_def(p['className'], '')])), 'Attrs': og_attrs(['data-slot', 'bar-chart'])}, 'children': c_6}) }}
Props
Names and types come from the xgen specs of the source. Standard HTML attributes (id, role, tabIndex, aria-*) pass through to the root element; the class prop is className in React.
A row of vertical bars (sales by day, orders by week) with a caption under each bar. The chart is one image for assistive technology: `label` carries the summary, the bar values are in the captions and titles.
| Prop | Type | Default | Description |
|---|---|---|---|
| bars (required) | Bar[] | - | id, caption, value; `title` is the tooltip, `muted` dims a period without data |
| label (required) | string | - | the summary read to assistive technology |
| max | number | - | the value of a full-height bar; the largest value by default |
| height | number | 160 | height of the plot area in px |