Bar Chart | kamod chartsSkip to content

Charts

Bar Chart

Compare values across categories with vertical or horizontal bars.

Charts / Bar Chart

Preact nativeGrouped & stackedTyped fill accessorsReference areas

Overview

Carrier performance by weekGrouped shipment outcomes with an highlighted review window.W01W02W03W040200400600800

Bar charts compare categorical values along a band or point scale. Kamod keeps legend, tooltip, and reference colors on stable series tokens while per-bar fill accessors can reflect each datum.

import { BarChart } from "@kamod-ch/charts/bar";
Static bundle
Import from @kamod-ch/charts/bar for charts without the motion runtime.
Animation
Motion helpers live behind @kamod-ch/charts/motion when you need enter or update transitions on bar geometry.

API notes

Typed BarChart props — no option objects and no React-only patterns.

color vs fill
color is the stable series token for legend and tooltip swatches. fill (string or accessor) paints individual bars and may vary per datum without changing legend semantics.
Signed values
Do not rely on bar color alone for positive versus negative meaning. Pair fill with valueLabels, tooltips, or explicit description text so the sign is readable without hue.
grouped vs stacked
grouped (default) places series side-by-side within each category band. stacked builds cumulative segments; tooltips and labels still report raw series values, not stack totals.
maxBarSize
Caps bar width on vertical charts and bar height on horizontal charts. Bars stay centered inside the category band; hit-testing and motion use the same limited geometry.
valueLabels
true enables auto placement (outside for grouped, center/outside for stacks). Pass BarValueLabelOptions for position, format, and per-series overrides. On vertical charts, outside sits above positive bars; on horizontal charts, outside extends past the bar tip.
referenceAreas
Semantic x1/x2 always refer to categories and y1/y2 to values — BarChart maps them to the visible axes when orientation="horizontal". Areas render behind grid and bars and do not affect domains or hit-testing.
BarDatumContext
Fill and label accessors receive datum, value, stack bounds, orientation, and an active flag when the bar is keyboard- or pointer-selected.
Theme tokens
Prefer var(--chart-1) … var(--chart-4) and var(--chart-reference-area) so fills adapt in light and dark themes.

Eight functional patterns covering sizing, orientation, stacking, signed fills, value labels, and reference bands. Each preview uses Kamod’s real BarChart API — no copied option objects.

Sized vertical bars

Cap bar width with maxBarSize and soften corners with radius. Bars stay centered inside each category band.

maxBarSizeradiusresponsive

Drag the container edge — the chart reflows while keeping the 32px bar cap.

Daily deploy volumeVertical bars limited to 32px width with rounded corners.MonTueWedThuFriSatSun05101520

Horizontal bars with long labels

Categories map to the Y axis on horizontal charts — useful when labels need more room than a vertical band allows.

orientationhorizontal
Acquisition channelsHorizontal layout keeps lengthy category names readable.050010001500Organic search (brand terms)Partner marketplace listingsLifecycle email nurtureCommunity ambassador referralsPaid social retargeting

Grouped series with legend

Related metrics sit side-by-side. The HTML legend toggles visibility without moving keyboard focus off the chart surface.

groupedshowLegendonBarClick
Product mix by segmentGrouped bars with legend toggles. Click a bar or focus the chart and press Enter.CorePlusEnterpriseTrials0100200300400

Select a bar with pointer or keyboard (Enter).

Stacked composition

Stack series to show part-to-whole relationships. Tooltip rows still expose each series’ raw value.

stackedtooltip
Shipment outcome mixStacked segments per carrier week with raw values in tooltips.W01W02W03W040200400600800

Signed values with typed fill

Use a BarFill accessor for datum-driven color, but pair it with labels or tooltips so sign is not conveyed by hue alone.

fillvalueLabelsa11y
Monthly net cashflowBars show explicit plus or minus prefixes; color reinforces but does not replace the sign.JanFebMarAprMayJun−100102030+24-11+17-8+31+12

Formatted value labels

Pass BarValueLabelOptions for placement, formatting, and styling. Returning null from format suppresses a label.

valueLabelsformat
Deploy count labelsOutside labels with compact number formatting.MonTueWedThuFriSatSun0510152012 deploys18 deploys15 deploys22 deploys19 deploys8 deploys6 deploys

Compact stacked chart

A shorter frame with stacked bars, nearest tooltips, and legend toggles — suited to dense dashboard tiles.

stackedtooltipshowLegend
Units and returnsCompact stacked tile with legend and nearest tooltips.CorePlusEnterpriseTrials0100200300400

Carrier week with MTD band

Highlight a category interval with referenceAreas. x1/x2 always refer to categories even when the chart is horizontal.

referenceAreasgrouped
Carrier SLA reviewMonth-to-date category band behind grouped shipment bars.W01W02W03W040200400600800

Bar chart accessibility

  • Set title and description on every production chart — they render into SVG title and desc elements.
  • Tooltips expose a polite live region for pointer and keyboard updates; bar charts default to nearest mode.
  • Legend buttons toggle series visibility while the chart surface keeps focus for arrow-key navigation.
  • For signed or thresholded data, expose the sign or status in labels or tooltips — not color alone.
  • Reference area labels are decorative (aria-hidden) and never receive tab stops.

Props

BarChart props
PropTypeDefaultDescription
datareadonly TDatum[]—Array of row objects rendered by the chart.
xKeyDataAccessor<TDatum, string | number | Date | null | undefined>—Field or accessor for the x axis.
xScale"linear" | "time" | "band" | "point"—Scale kind for the x axis.
heightnumber—Chart height in pixels.
widthnumber—Fixed width. Prefer responsive + initialWidth for fluid layouts.
initialWidthnumber—SSR / first-paint width before ResizeObserver measures the container.
responsivebooleanfalseStretch to the parent container width.
titlestring—Accessible chart title rendered into SVG title.
descriptionstring—Accessible description rendered into SVG desc.
showGridboolean—Show cartesian grid lines.
showLegendboolean—Show an HTML legend with series toggles. Supported on Line, Scatter, Area, and Bar charts.
tooltipboolean | CartesianTooltipOptions<TDatum>—Enable pointer and keyboard tooltips. `true` uses chart defaults: axis mode for Line and Area, nearest mode for Scatter and Bar. Options may set mode, placement, offset, render, formatValue, and formatLabel. PieChart does not support this prop.
animateboolean | ChartAnimateOptions—Optional data animation. No-op on static entries unless a MotionConfig from @kamod-ch/charts/motion is present. Defaults to on for charts imported from that subpath. Options: enabled, duration (seconds), easing, preset, reducedMotion.
seriesreadonly BarSeriesConfig<TDatum>[]—One or more bar series. BarSeriesConfig extends the cartesian config with fill and per-series valueLabels.
orientation"vertical" | "horizontal""vertical"Bar direction. Vertical maps categories to X and values to Y; horizontal swaps those axes.
stackedbooleanfalseStack series into cumulative segments per category. Tooltips and value labels still report raw series values.
groupedbooleantruePlace series side-by-side within each category band when stacked is false. Ignored when stacked is true.
radiusnumber0Corner radius in pixels for bar rectangles.
minBarSizenumber—Minimum bar thickness along the value axis (width on vertical charts, height on horizontal).
maxBarSizenumber—Maximum bar thickness along the value axis. Bars stay centered inside the category band; hit-testing and motion share the same capped geometry.
valueLabelsboolean | BarValueLabelOptions<TDatum>falseChart-level value label defaults. true enables auto placement. Series may override with BarSeriesConfig.valueLabels.
referenceLinesreadonly CartesianReferenceLine<TDatum>[]—Optional x/y threshold lines. On vertical charts, y is a numeric value and x may be a category string for horizontal orientation.
referenceAreasreadonly CartesianReferenceArea<TDatum>[]—Category or value highlight bands rendered behind grid and bars. x1/x2 always refer to categories; y1/y2 refer to values regardless of orientation.
onBarClick(datum, seriesId, index, value) => void—Pointer click handler for a bar. Keyboard users activate the focused bar with Enter on the chart surface.

BarSeriesConfig

Fields on each series entry
PropTypeDefaultDescription
keyDataAccessor<TDatum, number | null | undefined>—Numeric field or accessor for bar length.
idstring—Stable series identifier for legend toggles and onBarClick. Defaults from key when omitted.
labelstring—Human-readable name for legend and tooltip rows.
colorstring—Stable series color for legend swatches and tooltip accents. Does not change when fill varies per datum.
hiddenbooleanfalseHide the series without removing it from the config (legend can re-enable).
fillBarFill<TDatum>—Per-bar fill: a CSS color string or accessor (context) => string | undefined. Falls back to color when omitted.
valueLabelsboolean | BarValueLabelOptions<TDatum>—Per-series value label override. Merged over chart-level valueLabels.

BarFill

BarFill type alias
PropTypeDefaultDescription
BarFillstring | ((context: BarDatumContext<TDatum>) => string | undefined)—Static token such as var(--chart-1) or a function that receives BarDatumContext and returns a fill color.

BarDatumContext

Passed to fill accessors and extended by BarValueLabelContext for valueLabels.format.

BarDatumContext fields
PropTypeDefaultDescription
datumTDatum—The data row for this bar.
indexnumber—Row index in the data array.
valuenumber—Resolved numeric value for this bar.
valueStartnumber—Stack baseline along the value axis (0 for grouped charts).
valueEndnumber—Stack top along the value axis (value when not stacked).
seriesIdstring—Resolved series id passed to fill and label accessors.
seriesIndexnumber—Index of the series in the series array.
orientation"vertical" | "horizontal"—Chart orientation at render time.
stackedboolean—Whether the chart is in stacked mode.
activeboolean—True when the bar is pointer- or keyboard-selected.

BarValueLabelOptions

Chart-level via valueLabels or per-series via BarSeriesConfig.valueLabels. Series options override chart defaults.

BarValueLabelOptions fields
PropTypeDefaultDescription
position"auto" | "inside" | "outside" | "center""auto"Label placement. auto centers inner stack segments and places the outer segment outside. outside sits beyond the bar along the value axis; inside tucks into the bar end; center uses the bar midpoint.
format(value, context: BarValueLabelContext<TDatum>) => ComponentChildren—Custom label content. Return null to suppress a label for that bar. BarValueLabelContext extends BarDatumContext with defaultLabel.
colorstring—SVG fill color for label text.
classstring—Optional class applied to the label text element.
offsetnumber4Pixel gap between the bar edge and an outside or inside label.

referenceAreas entries

CartesianReferenceArea fields used by BarChart
PropTypeDefaultDescription
idstring—Optional stable id for the area element.
x1string | number | Date—Start category (band scale) for a vertical category band.
x2string | number | Date—End category for the band. Same category as x1 highlights one band.
y1number | Date | string—Lower value bound when highlighting a value range.
y2number | Date | string—Upper value bound for value-range highlights.
labelstring—Decorative label (aria-hidden). Not a tab stop.
fillstring—Fill color. Prefer var(--chart-reference-area) for theme-aware bands.
fillOpacitynumber—Opacity multiplier for the fill color.

Label placement

Value labels follow the value axis regardless of orientation:

  • Vertical: outside sits above positive bars and below negative bars; inside tucks into the bar end; center uses the bar midpoint.
  • Horizontal: outside extends past the bar along X; inside sits inset from the bar tip; center stays at the bar center.
  • Stacked + auto: inner segments use center; the outermost positive (or negative) segment uses outside so totals are not duplicated.

Orientation and reference geometry

Vertical bars (default) use a categorical X axis and numeric Y axis. Horizontal bars swap those roles — categories align on Y so long labels stay readable.

Reference areas always use semantic coordinates: x1 / x2 refer to category keys and y1 / y2 to numeric values. BarChart maps them to the visible axes when orientation="horizontal".

Reference lines follow the same axis mapping as LineChart: referenceLine.y is a numeric threshold on vertical charts (or a category string on horizontal charts).