# Category Bar

> A single segmented bar that shows how a total splits into parts, with an optional marker.

Source: https://www.honestui.com/docs/charts/category-bar/static

### Basic Category Bar

```tsx
"use client";

import { CategoryBar, type ChartConfig } from "honestui/charts";

// Scenario: Storage used by file type, out of a 100 GB plan
const data = [
  { type: "video", gigabytes: 38.4 },
  { type: "photos", gigabytes: 21.7 },
  { type: "documents", gigabytes: 9.2 },
  { type: "other", gigabytes: 4.1 },
  { type: "free", gigabytes: 26.6 },
];

const chartConfig = {
  video: { label: "Video", colors: { light: ["#2563eb"], dark: ["#3b82f6"] } },
  photos: { label: "Photos", colors: { light: ["#0d9488"], dark: ["#14b8a6"] } },
  documents: { label: "Documents", colors: { light: ["#d97706"], dark: ["#f59e0b"] } },
  other: { label: "Other", colors: { light: ["#7c3aed"], dark: ["#8b5cf6"] } },
  free: { label: "Available", colors: { light: ["#e5e7eb"], dark: ["#3f3f46"] } },
} satisfies ChartConfig;

export function ExampleCategoryBar() {
  return (
    <div className="grid w-full max-w-md gap-3 text-xs">
      <div className="flex items-baseline justify-between">
        <span className="text-foreground font-medium">Storage</span>
        <span className="text-muted-foreground">
          <span className="text-foreground font-mono tabular-nums">73.4 GB</span> of 100 GB
        </span>
      </div>
      <CategoryBar
        data={data}
        config={chartConfig}
        nameKey="type"
        valueKey="gigabytes"
        valueFormatter={(value) => `${value.toFixed(1)} GB`}
        showLegend
      />
    </div>
  );
}

```

## Overview [#overview]

A category bar divides one bar into segments sized by their share of the total. It is the compact choice for a part-to-whole breakdown inside a card or table: storage by file type, budget by category, survey answers, or a score placed on banded ranges with a marker. With more than five or six segments, or when values are close, a [Bar List](/docs/charts/bar-list/static) or [Bar Chart](/docs/charts/bar-chart/static) is easier to compare.

## Anatomy [#anatomy]

`CategoryBar` reads a name and a value from each row and draws the segments in data order; each name is a `config` key for its label and color. Optional parts sit around the bar: `showLabels` prints the cumulative value at each boundary, dropping any that would collide; `marker` draws a tick at a value on the same scale; and `showLegend` lists the segments under the bar. The bar is drawn with DOM elements, so it stays crisp at any size.

## Accessibility [#accessibility]

The bar is an image whose generated accessible name lists every segment with its value and share, and the marker if there is one, for example "Video 38.4 GB (38.4%), Photos 21.7 GB (21.7%)". Pass `ariaLabel` to replace it. Segment tooltips are pointer-operated, so pair the bar with a legend or written figures when readers need exact values. Choose segment colors with enough contrast against each other and the page.

## Installation [#installation]

<CommandBlock commands="[&#x22;honestui&#x22;]" />

## Usage [#usage]

```tsx
import { CategoryBar, type ChartConfig } from "honestui/charts";
```

```tsx
<CategoryBar
  data={data}
  config={chartConfig}
  nameKey="type"
  valueKey="gigabytes"
  valueFormatter={(value) => `${value} GB`}
  showLegend
/>
```

Segments are proportional to the sum of all values. To show remaining capacity, add a row for it with a neutral color, as the "Available" segment does above.

## Examples [#examples]

### Marker and labels [#marker-and-labels]

### marker and showLabels

```tsx
"use client";

import { CategoryBar, type ChartConfig } from "honestui/charts";

// Scenario: A credit score on banded ranges, with the applicant marked
const bands = [
  { band: "poor", width: 280 },
  { band: "fair", width: 90 },
  { band: "good", width: 70 },
  { band: "veryGood", width: 60 },
  { band: "excellent", width: 50 },
];

const chartConfig = {
  poor: { label: "Poor", colors: { light: ["#ef4444"], dark: ["#ef4444"] } },
  fair: { label: "Fair", colors: { light: ["#f97316"], dark: ["#f97316"] } },
  good: { label: "Good", colors: { light: ["#eab308"], dark: ["#eab308"] } },
  veryGood: { label: "Very good", colors: { light: ["#84cc16"], dark: ["#84cc16"] } },
  excellent: { label: "Excellent", colors: { light: ["#10b981"], dark: ["#10b981"] } },
} satisfies ChartConfig;

// Bands start at 300, so values are offset to that origin.
const ORIGIN = 300;
const score = 742;

export function ExampleCategoryBarMarker() {
  return (
    <div className="grid w-full max-w-md gap-3 text-xs">
      <div className="flex items-baseline justify-between">
        <span className="text-foreground font-medium">Credit score</span>
        <span className="text-foreground font-mono text-base font-medium tabular-nums">{score}</span>
      </div>
      <CategoryBar
        data={bands}
        config={chartConfig}
        nameKey="band"
        valueKey="width"
        height={6}
        marker={{ value: score - ORIGIN, label: "Your score" }}
        showLabels
        valueFormatter={(value) => `${value} pts`}
        labelFormatter={(value) => String(Math.round(value + ORIGIN))}
      />
    </div>
  );
}

```

A marker places one value on the bar, such as a score on banded ranges. The marker uses the same scale as the summed values, so this example offsets the score by the scale's origin and adds it back in `labelFormatter`, which formats positions on the scale (boundary labels and the marker) separately from segment values.

### Percentages [#percentages]

### percent

```tsx
"use client";

import { CategoryBar, type ChartConfig } from "honestui/charts";

// Scenario: Survey responses as shares of the total
const data = [
  { answer: "promoter", responses: 412 },
  { answer: "passive", responses: 238 },
  { answer: "detractor", responses: 96 },
];

const chartConfig = {
  promoter: { label: "Promoters", colors: { light: ["#059669"], dark: ["#10b981"] } },
  passive: { label: "Passives", colors: { light: ["#a1a1aa"], dark: ["#71717a"] } },
  detractor: { label: "Detractors", colors: { light: ["#dc2626"], dark: ["#ef4444"] } },
} satisfies ChartConfig;

export function ExampleCategoryBarPercent() {
  return (
    <div className="grid w-full max-w-md gap-3 text-xs">
      <div className="flex items-baseline justify-between">
        <span className="text-foreground font-medium">Net Promoter Score</span>
        <span className="text-foreground font-mono text-base font-medium tabular-nums">+42</span>
      </div>
      <CategoryBar
        data={data}
        config={chartConfig}
        nameKey="answer"
        valueKey="responses"
        height={10}
        percent
        showLabels
        showLegend
        legendVariant="circle"
      />
    </div>
  );
}

```

With `percent`, labels and tooltips show each share of the total, and the tooltip detail shows the raw value.

### Loading State [#loading-state]

### isLoading='true'

```tsx
"use client";

import { CategoryBar, type ChartConfig } from "honestui/charts";

const chartConfig = {
  used: { label: "Used", colors: { light: ["#2563eb"], dark: ["#3b82f6"] } },
} satisfies ChartConfig;

export function ExampleCategoryBarLoading() {
  return (
    <CategoryBar
      data={[]}
      config={chartConfig}
      nameKey="type"
      valueKey="value"
      isLoading
      className="w-full max-w-md py-4"
    />
  );
}

```

Pass `isLoading` to draw a neutral bar with the same loading treatment as the other Honest UI charts.

## API Reference [#api-reference]

<ApiHeading>
  CategoryBar
</ApiHeading>


  ### `data`

type: `TData[]`

One row per segment, in display order.

  ### `config`

type: `ChartConfig`

One entry per segment name, with its label and colors.

  ### `nameKey`

type: `keyof TData & string`

The segment name, matching a `config` key.

  ### `valueKey`

type: `keyof TData & string`

The segment value. Negative values count as zero.

  ### `height`

type: `number` · default: `8`

Bar height in pixels.

  ### `gap`

type: `number` · default: `2`

Gap between segments in pixels.

  ### `marker`

type: `{ value: number; label?: ReactNode }`

A tick at a value on the summed scale, with its tooltip label.

  ### `showLabels`

type: `boolean` · default: `false`

Prints the cumulative value at each segment boundary.

  ### `percent`

type: `boolean` · default: `false`

Shows shares of the total instead of values in labels and tooltips.

  ### `valueFormatter`



string">
    Formats segment values in tooltips and the accessible name.

  ### `labelFormatter`



string">
    Formats positions on the scale: boundary labels, the marker, and the range shown in each segment tooltip. Defaults to `valueFormatter`.

  ### `tooltip`

type: `boolean` · default: `true`

Shows a tooltip on segment and marker hover.

  ### `tooltipVariant`

type: `&#x22;default&#x22; | &#x22;frosted-glass&#x22;` · default: `&#x22;default&#x22;`

The tooltip surface.

  ### `tooltipRoundness`

type: `&#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;` · default: `&#x22;lg&#x22;`

The tooltip corner radius.

  ### `showLegend`

type: `boolean` · default: `false`

Lists the segments under the bar.

  ### `legendVariant`

type: `LegendVariant` · default: `&#x22;rounded-square&#x22;`

The legend indicator shape, shared with the other charts.

  ### `animation`

type: `boolean` · default: `true`

Grows the segments in on mount. Reduced-motion preferences disable it.

  ### `isLoading`

type: `boolean` · default: `false`

Draws a neutral bar and a loading label.

  ### `ariaLabel`

type: `string`

Accessible name for the bar. A segment summary is generated when omitted.

  ### `className`

type: `string`

Classes for the container.
