Skip to documentation content

Category Bar

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

Basic Category Bar

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 or Bar Chart is easier to compare.

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

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

npm install honestui

Usage

import { CategoryBar, type ChartConfig } from "honestui/charts";
<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

Marker and labels

marker and showLabels

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

percent

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

Loading State

isLoading='true'

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

API Reference

CategoryBar
PropTypeDefaultDescription
data*TData[]–

One row per segment, in display order.

config*ChartConfig–

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

nameKey*keyof TData & string–

The segment name, matching a config key.

valueKey*keyof TData & string–

The segment value. Negative values count as zero.

heightnumber8

Bar height in pixels.

gapnumber2

Gap between segments in pixels.

marker{ value: number; label?: ReactNode }–

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

showLabelsbooleanfalse

Prints the cumulative value at each segment boundary.

percentbooleanfalse

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

valueFormatter(value: number) => string–

Formats segment values in tooltips and the accessible name.

labelFormatter(value: number) => string–

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

tooltipbooleantrue

Shows a tooltip on segment and marker hover.

tooltipVariantdefault|frosted-glass"default"

The tooltip surface.

tooltipRoundnesssm|md|lg|xl"lg"

The tooltip corner radius.

showLegendbooleanfalse

Lists the segments under the bar.

legendVariantLegendVariant"rounded-square"

The legend indicator shape, shared with the other charts.

animationbooleantrue

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

isLoadingbooleanfalse

Draws a neutral bar and a loading label.

ariaLabelstring–

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

classNamestring–

Classes for the container.