# Bar Charts

A collection of ready-to-use chart components, drawn as SVG on the server. From basic charts to rich data displays, copy and paste into your apps.

Source: https://webjs.dev/ui/charts/bar

[Documentation](https://webjs.dev/ui/docs/components/chart)

Bar Chart

components/charts/chart-bar-interactive.ts

```ts
/**
 * An interactive bar chart, a chart block ported from shadcn/ui's chart-bar-interactive.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-interactive` at
 * `components/charts/chart-bar-interactive.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarInteractive()}`.
 *
 * The two series buttons are radio inputs, and both charts are served; the
 * card shows the chosen one through `:has()`, so the switch works with
 * JavaScript off. shadcn's re-renders one chart from React state instead.
 */
import { html } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { date: '2024-04-01', desktop: 222, mobile: 150 },
  { date: '2024-04-02', desktop: 97, mobile: 180 },
  { date: '2024-04-03', desktop: 167, mobile: 120 },
  { date: '2024-04-04', desktop: 242, mobile: 260 },
  { date: '2024-04-05', desktop: 373, mobile: 290 },
  { date: '2024-04-06', desktop: 301, mobile: 340 },
  { date: '2024-04-07', desktop: 245, mobile: 180 },
  { date: '2024-04-08', desktop: 409, mobile: 320 },
  { date: '2024-04-09', desktop: 59, mobile: 110 },
  { date: '2024-04-10', desktop: 261, mobile: 190 },
  { date: '2024-04-11', desktop: 327, mobile: 350 },
  { date: '2024-04-12', desktop: 292, mobile: 210 },
  { date: '2024-04-13', desktop: 342, mobile: 380 },
  { date: '2024-04-14', desktop: 137, mobile: 220 },
  { date: '2024-04-15', desktop: 120, mobile: 170 },
  { date: '2024-04-16', desktop: 138, mobile: 190 },
  { date: '2024-04-17', desktop: 446, mobile: 360 },
  { date: '2024-04-18', desktop: 364, mobile: 410 },
  { date: '2024-04-19', desktop: 243, mobile: 180 },
  { date: '2024-04-20', desktop: 89, mobile: 150 },
  { date: '2024-04-21', desktop: 137, mobile: 200 },
  { date: '2024-04-22', desktop: 224, mobile: 170 },
  { date: '2024-04-23', desktop: 138, mobile: 230 },
  { date: '2024-04-24', desktop: 387, mobile: 290 },
  { date: '2024-04-25', desktop: 215, mobile: 250 },
  { date: '2024-04-26', desktop: 75, mobile: 130 },
  { date: '2024-04-27', desktop: 383, mobile: 420 },
  { date: '2024-04-28', desktop: 122, mobile: 180 },
  { date: '2024-04-29', desktop: 315, mobile: 240 },
  { date: '2024-04-30', desktop: 454, mobile: 380 },
  { date: '2024-05-01', desktop: 165, mobile: 220 },
  { date: '2024-05-02', desktop: 293, mobile: 310 },
  { date: '2024-05-03', desktop: 247, mobile: 190 },
  { date: '2024-05-04', desktop: 385, mobile: 420 },
  { date: '2024-05-05', desktop: 481, mobile: 390 },
  { date: '2024-05-06', desktop: 498, mobile: 520 },
  { date: '2024-05-07', desktop: 388, mobile: 300 },
  { date: '2024-05-08', desktop: 149, mobile: 210 },
  { date: '2024-05-09', desktop: 227, mobile: 180 },
  { date: '2024-05-10', desktop: 293, mobile: 330 },
  { date: '2024-05-11', desktop: 335, mobile: 270 },
  { date: '2024-05-12', desktop: 197, mobile: 240 },
  { date: '2024-05-13', desktop: 197, mobile: 160 },
  { date: '2024-05-14', desktop: 448, mobile: 490 },
  { date: '2024-05-15', desktop: 473, mobile: 380 },
  { date: '2024-05-16', desktop: 338, mobile: 400 },
  { date: '2024-05-17', desktop: 499, mobile: 420 },
  { date: '2024-05-18', desktop: 315, mobile: 350 },
  { date: '2024-05-19', desktop: 235, mobile: 180 },
  { date: '2024-05-20', desktop: 177, mobile: 230 },
  { date: '2024-05-21', desktop: 82, mobile: 140 },
  { date: '2024-05-22', desktop: 81, mobile: 120 },
  { date: '2024-05-23', desktop: 252, mobile: 290 },
  { date: '2024-05-24', desktop: 294, mobile: 220 },
  { date: '2024-05-25', desktop: 201, mobile: 250 },
  { date: '2024-05-26', desktop: 213, mobile: 170 },
  { date: '2024-05-27', desktop: 420, mobile: 460 },
  { date: '2024-05-28', desktop: 233, mobile: 190 },
  { date: '2024-05-29', desktop: 78, mobile: 130 },
  { date: '2024-05-30', desktop: 340, mobile: 280 },
  { date: '2024-05-31', desktop: 178, mobile: 230 },
  { date: '2024-06-01', desktop: 178, mobile: 200 },
  { date: '2024-06-02', desktop: 470, mobile: 410 },
  { date: '2024-06-03', desktop: 103, mobile: 160 },
  { date: '2024-06-04', desktop: 439, mobile: 380 },
  { date: '2024-06-05', desktop: 88, mobile: 140 },
  { date: '2024-06-06', desktop: 294, mobile: 250 },
  { date: '2024-06-07', desktop: 323, mobile: 370 },
  { date: '2024-06-08', desktop: 385, mobile: 320 },
  { date: '2024-06-09', desktop: 438, mobile: 480 },
  { date: '2024-06-10', desktop: 155, mobile: 200 },
  { date: '2024-06-11', desktop: 92, mobile: 150 },
  { date: '2024-06-12', desktop: 492, mobile: 420 },
  { date: '2024-06-13', desktop: 81, mobile: 130 },
  { date: '2024-06-14', desktop: 426, mobile: 380 },
  { date: '2024-06-15', desktop: 307, mobile: 350 },
  { date: '2024-06-16', desktop: 371, mobile: 310 },
  { date: '2024-06-17', desktop: 475, mobile: 520 },
  { date: '2024-06-18', desktop: 107, mobile: 170 },
  { date: '2024-06-19', desktop: 341, mobile: 290 },
  { date: '2024-06-20', desktop: 408, mobile: 450 },
  { date: '2024-06-21', desktop: 169, mobile: 210 },
  { date: '2024-06-22', desktop: 317, mobile: 270 },
  { date: '2024-06-23', desktop: 480, mobile: 530 },
  { date: '2024-06-24', desktop: 132, mobile: 180 },
  { date: '2024-06-25', desktop: 141, mobile: 190 },
  { date: '2024-06-26', desktop: 434, mobile: 380 },
  { date: '2024-06-27', desktop: 448, mobile: 490 },
  { date: '2024-06-28', desktop: 149, mobile: 200 },
  { date: '2024-06-29', desktop: 103, mobile: 160 },
  { date: '2024-06-30', desktop: 446, mobile: 400 },
];

const chartConfig = {
  views: {
    label: 'Page Views',
  },
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-2)',
  },
  mobile: {
    label: 'Mobile',
    color: 'var(--chart-1)',
  },
} satisfies ChartConfig;

const total = {
  desktop: chartData.reduce((acc, curr) => acc + curr.desktop, 0),
  mobile: chartData.reduce((acc, curr) => acc + curr.mobile, 0),
};

const day = (value: unknown, opts: Intl.DateTimeFormatOptions) =>
  new Date(String(value)).toLocaleDateString('en-US', { ...opts, timeZone: 'UTC' });

function chart(key: 'desktop' | 'mobile') {
  return barChart({
    config: chartConfig,
    class: 'aspect-auto h-[250px] w-full',
    width: 1260,
    height: 250,
    data: chartData,
    title: `${chartConfig[key].label} page views, April to June 2024`,
    margin: { left: 12, right: 12 },
    grid: { vertical: false },
    xAxis: {
      dataKey: 'date',
      tickLine: false,
      axisLine: false,
      tickMargin: 8,
      minTickGap: 32,
      tickFormatter: (value) => day(value, { month: 'short', day: 'numeric' }),
    },
    tooltip: {
      class: 'w-[150px]',
      nameKey: 'views',
      labelFormatter: (value) => day(value, { month: 'short', day: 'numeric', year: 'numeric' }),
    },
    bars: [{ dataKey: key, fill: `var(--color-${key})` }],
  });
}

export default function ChartBarInteractive() {
  return html`
    <div data-slot="card" class=${cn(cardClass(), 'group/chart py-0')}>
      <div data-slot="card-header" class=${cn(cardHeaderClass(), 'flex flex-col items-stretch border-b p-0! sm:flex-row')}>
        <div class="flex flex-1 flex-col justify-center gap-1 px-6 pt-4 pb-3 sm:py-0!">
          <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Interactive</div>
          <div data-slot="card-description" class=${cardDescriptionClass()}>Showing total visitors for the last 3 months</div>
        </div>
        <div class="flex" role="radiogroup" aria-label="Series">
          ${(['desktop', 'mobile'] as const).map((key) => html`
            <label class="relative z-30 flex flex-1 cursor-pointer flex-col justify-center gap-1 border-t px-6 py-4 text-left even:border-l has-checked:bg-muted/50 has-focus-visible:ring-2 has-focus-visible:ring-ring sm:border-t-0 sm:border-l sm:px-8 sm:py-6">
              <input type="radio" name="chart-bar-interactive" value=${key} ?checked=${key === 'desktop'} class="sr-only">
              <span class="text-xs text-muted-foreground">${chartConfig[key].label}</span>
              <span class="text-lg leading-none font-bold sm:text-3xl">${total[key].toLocaleString('en-US')}</span>
            </label>`)}
        </div>
      </div>
      <div data-slot="card-content" class=${cn(cardContentClass(), 'px-2 sm:p-6')}>
        <div class="group-has-[input[value=mobile]:checked]/chart:hidden">${chart('desktop')}</div>
        <div class="hidden group-has-[input[value=mobile]:checked]/chart:block">${chart('mobile')}</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Interactive

Showing total visitors for the last 3 months

- Apr 1Apr 2Apr 3Apr 4Apr 5Apr 6Apr 7Apr 8Apr 9Apr 10Apr 11Apr 12Apr 13Apr 14Apr 15Apr 16Apr 17Apr 18Apr 19Apr 20Apr 21Apr 22Apr 23Apr 24Apr 25Apr 26Apr 27Apr 28Apr 29Apr 30May 1May 2May 3May 4May 5May 6May 7May 8May 9May 10May 11May 12May 13May 14May 15May 16May 17May 18May 19May 20May 21May 22May 23May 24May 25May 26May 27May 28May 29May 30May 31Jun 1Jun 2Jun 3Jun 4Jun 5Jun 6Jun 7Jun 8Jun 9Jun 10Jun 11Jun 12Jun 13Jun 14Jun 15Jun 16Jun 17Jun 18Jun 19Jun 20Jun 21Jun 22Jun 23Jun 24Jun 25Jun 26Jun 27Jun 28Jun 29Jun 30

- Apr 1Apr 2Apr 3Apr 4Apr 5Apr 6Apr 7Apr 8Apr 9Apr 10Apr 11Apr 12Apr 13Apr 14Apr 15Apr 16Apr 17Apr 18Apr 19Apr 20Apr 21Apr 22Apr 23Apr 24Apr 25Apr 26Apr 27Apr 28Apr 29Apr 30May 1May 2May 3May 4May 5May 6May 7May 8May 9May 10May 11May 12May 13May 14May 15May 16May 17May 18May 19May 20May 21May 22May 23May 24May 25May 26May 27May 28May 29May 30May 31Jun 1Jun 2Jun 3Jun 4Jun 5Jun 6Jun 7Jun 8Jun 9Jun 10Jun 11Jun 12Jun 13Jun 14Jun 15Jun 16Jun 17Jun 18Jun 19Jun 20Jun 21Jun 22Jun 23Jun 24Jun 25Jun 26Jun 27Jun 28Jun 29Jun 30

Bar Chart

components/charts/chart-bar-default.ts

```ts
/**
 * A bar chart, a chart block ported from shadcn/ui's chart-bar-default.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-default` at
 * `components/charts/chart-bar-default.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarDefault()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', desktop: 186 },
  { month: 'February', desktop: 305 },
  { month: 'March', desktop: 237 },
  { month: 'April', desktop: 73 },
  { month: 'May', desktop: 209 },
  { month: 'June', desktop: 214 },
];

const chartConfig = {
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-1)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarDefault() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Desktop visitors, January to June',
          grid: { vertical: false },
          xAxis: { dataKey: 'month', tickLine: false, tickMargin: 10, axisLine: false, tickFormatter: (value) => String(value).slice(0, 3) },
          tooltip: { cursor: false, hideLabel: true },
          bars: [{ dataKey: 'desktop', fill: 'var(--color-desktop)', radius: 8 }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart

January - June 2024

- JanFebMarAprMayJun

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-horizontal.ts

```ts
/**
 * A horizontal bar chart, a chart block ported from shadcn/ui's chart-bar-horizontal.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-horizontal` at
 * `components/charts/chart-bar-horizontal.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarHorizontal()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', desktop: 186 },
  { month: 'February', desktop: 305 },
  { month: 'March', desktop: 237 },
  { month: 'April', desktop: 73 },
  { month: 'May', desktop: 209 },
  { month: 'June', desktop: 214 },
];

const chartConfig = {
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-1)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarHorizontal() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Horizontal</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Desktop visitors, January to June',
          layout: 'vertical',
          margin: { left: -20 },
          xAxis: { type: 'number', dataKey: 'desktop', hide: true },
          yAxis: { dataKey: 'month', type: 'category', tickLine: false, tickMargin: 10, axisLine: false, tickFormatter: (value) => String(value).slice(0, 3) },
          tooltip: { cursor: false, hideLabel: true },
          bars: [{ dataKey: 'desktop', fill: 'var(--color-desktop)', radius: 5 }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Horizontal

January - June 2024

- JanFebMarAprMayJun

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-multiple.ts

```ts
/**
 * A multiple bar chart, a chart block ported from shadcn/ui's chart-bar-multiple.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-multiple` at
 * `components/charts/chart-bar-multiple.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarMultiple()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', desktop: 186, mobile: 80 },
  { month: 'February', desktop: 305, mobile: 200 },
  { month: 'March', desktop: 237, mobile: 120 },
  { month: 'April', desktop: 73, mobile: 190 },
  { month: 'May', desktop: 209, mobile: 130 },
  { month: 'June', desktop: 214, mobile: 140 },
];

const chartConfig = {
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-1)',
  },
  mobile: {
    label: 'Mobile',
    color: 'var(--chart-2)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarMultiple() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Multiple</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Desktop and mobile visitors, January to June',
          grid: { vertical: false },
          xAxis: { dataKey: 'month', tickLine: false, tickMargin: 10, axisLine: false, tickFormatter: (value) => String(value).slice(0, 3) },
          tooltip: { cursor: false, indicator: 'dashed' },
          bars: [
            { dataKey: 'desktop', fill: 'var(--color-desktop)', radius: 4 },
            { dataKey: 'mobile', fill: 'var(--color-mobile)', radius: 4 },
          ],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Multiple

January - June 2024

- JanFebMarAprMayJun

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-stacked.ts

```ts
/**
 * A stacked bar chart with a legend, a chart block ported from shadcn/ui's chart-bar-stacked.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-stacked` at
 * `components/charts/chart-bar-stacked.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarStacked()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', desktop: 186, mobile: 80 },
  { month: 'February', desktop: 305, mobile: 200 },
  { month: 'March', desktop: 237, mobile: 120 },
  { month: 'April', desktop: 73, mobile: 190 },
  { month: 'May', desktop: 209, mobile: 130 },
  { month: 'June', desktop: 214, mobile: 140 },
];

const chartConfig = {
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-1)',
  },
  mobile: {
    label: 'Mobile',
    color: 'var(--chart-2)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarStacked() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Stacked + Legend</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Desktop and mobile visitors, January to June',
          grid: { vertical: false },
          xAxis: { dataKey: 'month', tickLine: false, tickMargin: 10, axisLine: false, tickFormatter: (value) => String(value).slice(0, 3) },
          tooltip: { hideLabel: true },
          legend: {},
          bars: [
            { dataKey: 'desktop', stackId: 'a', fill: 'var(--color-desktop)', radius: [0, 0, 4, 4] },
            { dataKey: 'mobile', stackId: 'a', fill: 'var(--color-mobile)', radius: [4, 4, 0, 0] },
          ],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Stacked + Legend

January - June 2024

Desktop

Mobile

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-label.ts

```ts
/**
 * A bar chart with a label, a chart block ported from shadcn/ui's chart-bar-label.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-label` at
 * `components/charts/chart-bar-label.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarLabel()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', desktop: 186 },
  { month: 'February', desktop: 305 },
  { month: 'March', desktop: 237 },
  { month: 'April', desktop: 73 },
  { month: 'May', desktop: 209 },
  { month: 'June', desktop: 214 },
];

const chartConfig = {
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-1)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarLabel() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Label</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Desktop visitors, January to June',
          margin: { top: 20 },
          grid: { vertical: false },
          xAxis: { dataKey: 'month', tickLine: false, tickMargin: 10, axisLine: false, tickFormatter: (value) => String(value).slice(0, 3) },
          tooltip: { cursor: false, hideLabel: true },
          bars: [{
            dataKey: 'desktop',
            fill: 'var(--color-desktop)',
            radius: 8,
            labels: [{ position: 'top', offset: 12, class: 'fill-foreground', fontSize: 12 }],
          }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Label

January - June 2024

- 18630523773209214JanFebMarAprMayJun

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-label-custom.ts

```ts
/**
 * A bar chart with a custom label, a chart block ported from shadcn/ui's chart-bar-label-custom.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-label-custom` at
 * `components/charts/chart-bar-label-custom.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarLabelCustom()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', desktop: 186, mobile: 80 },
  { month: 'February', desktop: 305, mobile: 200 },
  { month: 'March', desktop: 237, mobile: 120 },
  { month: 'April', desktop: 73, mobile: 190 },
  { month: 'May', desktop: 209, mobile: 130 },
  { month: 'June', desktop: 214, mobile: 140 },
];

const chartConfig = {
  desktop: {
    label: 'Desktop',
    color: 'var(--chart-2)',
  },
  mobile: {
    label: 'Mobile',
    color: 'var(--chart-2)',
  },
  label: {
    color: 'var(--background)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarLabelCustom() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Custom Label</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Desktop visitors, January to June',
          layout: 'vertical',
          margin: { right: 16 },
          grid: { horizontal: false },
          yAxis: { dataKey: 'month', type: 'category', tickLine: false, tickMargin: 10, axisLine: false, tickFormatter: (value) => String(value).slice(0, 3), hide: true },
          xAxis: { dataKey: 'desktop', type: 'number', hide: true },
          tooltip: { cursor: false, indicator: 'line' },
          bars: [{
            dataKey: 'desktop',
            fill: 'var(--color-desktop)',
            radius: 4,
            labels: [
              { dataKey: 'month', position: 'insideLeft', offset: 8, class: 'fill-(--color-label)', fontSize: 12 },
              { dataKey: 'desktop', position: 'right', offset: 8, class: 'fill-foreground', fontSize: 12 },
            ],
          }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Custom Label

January - June 2024

- JanuaryFebruaryMarchAprilMayJune18630523773209214

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-mixed.ts

```ts
/**
 * A mixed bar chart, a chart block ported from shadcn/ui's chart-bar-mixed.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-mixed` at
 * `components/charts/chart-bar-mixed.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarMixed()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { browser: 'chrome', visitors: 275, fill: 'var(--color-chrome)' },
  { browser: 'safari', visitors: 200, fill: 'var(--color-safari)' },
  { browser: 'firefox', visitors: 187, fill: 'var(--color-firefox)' },
  { browser: 'edge', visitors: 173, fill: 'var(--color-edge)' },
  { browser: 'other', visitors: 90, fill: 'var(--color-other)' },
];

const chartConfig = {
  visitors: {
    label: 'Visitors',
  },
  chrome: {
    label: 'Chrome',
    color: 'var(--chart-1)',
  },
  safari: {
    label: 'Safari',
    color: 'var(--chart-2)',
  },
  firefox: {
    label: 'Firefox',
    color: 'var(--chart-3)',
  },
  edge: {
    label: 'Edge',
    color: 'var(--chart-4)',
  },
  other: {
    label: 'Other',
    color: 'var(--chart-5)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarMixed() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Mixed</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Visitors by browser',
          layout: 'vertical',
          margin: { left: 0 },
          yAxis: {
            dataKey: 'browser',
            type: 'category',
            tickLine: false,
            tickMargin: 10,
            axisLine: false,
            tickFormatter: (value) => chartConfig[value as keyof typeof chartConfig]?.label,
          },
          xAxis: { dataKey: 'visitors', type: 'number', hide: true },
          tooltip: { cursor: false, hideLabel: true },
          bars: [{ dataKey: 'visitors', radius: 5 }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Mixed

January - June 2024

- ChromeSafariFirefoxEdgeOther

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-active.ts

```ts
/**
 * A bar chart with an active bar, a chart block ported from shadcn/ui's chart-bar-active.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-active` at
 * `components/charts/chart-bar-active.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarActive()}`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, chartRectangle, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { browser: 'chrome', visitors: 187, fill: 'var(--color-chrome)' },
  { browser: 'safari', visitors: 200, fill: 'var(--color-safari)' },
  { browser: 'firefox', visitors: 275, fill: 'var(--color-firefox)' },
  { browser: 'edge', visitors: 173, fill: 'var(--color-edge)' },
  { browser: 'other', visitors: 90, fill: 'var(--color-other)' },
];

const chartConfig = {
  visitors: {
    label: 'Visitors',
  },
  chrome: {
    label: 'Chrome',
    color: 'var(--chart-1)',
  },
  safari: {
    label: 'Safari',
    color: 'var(--chart-2)',
  },
  firefox: {
    label: 'Firefox',
    color: 'var(--chart-3)',
  },
  edge: {
    label: 'Edge',
    color: 'var(--chart-4)',
  },
  other: {
    label: 'Other',
    color: 'var(--chart-5)',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

const ACTIVE_INDEX = 2;

export default function ChartBarActive() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Active</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          data: chartData,
          title: 'Visitors by browser',
          grid: { vertical: false },
          xAxis: {
            dataKey: 'browser',
            tickLine: false,
            tickMargin: 10,
            axisLine: false,
            tickFormatter: (value) => chartConfig[value as keyof typeof chartConfig]?.label,
          },
          tooltip: { cursor: false, hideLabel: true },
          bars: [{
            dataKey: 'visitors',
            strokeWidth: 2,
            radius: 8,
            shape: ({ index, datum, ...props }) => index === ACTIVE_INDEX
              ? chartRectangle({ ...props, fillOpacity: 0.8, stroke: String(datum.fill), strokeDasharray: 4, strokeDashoffset: 4 })
              : chartRectangle(props),
          }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Active

January - June 2024

- ChromeSafariFirefoxEdgeOther

Trending up by 5.2% this month

Showing total visitors for the last 6 months

Bar Chart

components/charts/chart-bar-negative.ts

```ts
/**
 * A bar chart with negative values, a chart block ported from shadcn/ui's chart-bar-negative.
 *
 * Installed by `npx @webjsdev/ui add chart-bar-negative` at
 * `components/charts/chart-bar-negative.ts`. It is app code you own, so edit it
 * freely, and render it from any page with `${ChartBarNegative()}`.
 *
 * shadcn colours each bar with a `<Cell>`; here each row carries its `fill`.
 */
import { html, svg } from '@webjsdev/core';
import { cn } from '../../lib/utils/cn.ts';
import {
  cardClass, cardContentClass, cardDescriptionClass, cardFooterClass, cardHeaderClass, cardTitleClass,
} from '../ui/card.ts';
import { barChart, type ChartConfig } from '../ui/chart.ts';

const chartData = [
  { month: 'January', visitors: 186 },
  { month: 'February', visitors: 205 },
  { month: 'March', visitors: -207 },
  { month: 'April', visitors: 173 },
  { month: 'May', visitors: -209 },
  { month: 'June', visitors: 214 },
];

const chartConfig = {
  visitors: {
    label: 'Visitors',
  },
} satisfies ChartConfig;

// lucide's trending-up, the icon in shadcn's footer.
const TrendingUp = (cls = '') => svg`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" class=${'lucide lucide-trending-up ' + cls}><path d="M16 7h6v6"></path><path d="m22 7-8.5 8.5-5-5L2 17"></path></svg>`;

export default function ChartBarNegative() {
  return html`
    <div data-slot="card" class=${cardClass()}>
      <div data-slot="card-header" class=${cardHeaderClass()}>
        <div data-slot="card-title" class=${cardTitleClass()}>Bar Chart - Negative</div>
        <div data-slot="card-description" class=${cardDescriptionClass()}>January - June 2024</div>
      </div>
      <div data-slot="card-content" class=${cardContentClass()}>
        ${barChart({
          config: chartConfig,
          // Positive months in one colour, negative ones in another.
          data: chartData.map((item) => ({ ...item, fill: item.visitors > 0 ? 'var(--chart-1)' : 'var(--chart-2)' })),
          title: 'Visitors by month, with the months that lost them',
          grid: { vertical: false },
          tooltip: { cursor: false, hideLabel: true, hideIndicator: true },
          bars: [{ dataKey: 'visitors', labels: [{ position: 'top', dataKey: 'month' }] }],
        })}
      </div>
      <div data-slot="card-footer" class=${cn(cardFooterClass(), 'flex-col items-start gap-2 text-sm')}>
        <div class="flex gap-2 leading-none font-medium">
          Trending up by 5.2% this month ${TrendingUp('h-4 w-4')}
        </div>
        <div class="leading-none text-muted-foreground">Showing total visitors for the last 6 months</div>
      </div>
    </div>
  `;
}
```

Bar Chart - Negative

January - June 2024

- JanuaryFebruaryMarchAprilMayJune

Trending up by 5.2% this month

Showing total visitors for the last 6 months
