Bar Chart

A general-purpose stacked bar chart for categorical datasets, with optional line overlays.

Volume by month

6,450

Usage

import { BarChart, type BarChartSeries } from '@epilot/spark-ui/bar-chart'
function VolumeByMonth() {
  return (
    <BarChart
      data={data}
      series={series}
      getCategory={(datum) => datum.month}
      title="Volume by month"
      yAxisUnit="units"
    />
  )
}

Use BarChart when categories are discrete buckets (hours, days, months) and you control the series config. For the energy-source stack with an animated total, use Energy Bar Graph. For week/month/year home energy with fixed solar/battery/grid series, use Energy History Chart. For continuous time series, use Line Chart.

The chart sizes itself to its container width. Negative series values render below the baseline.

Examples

Highlighting a category

highlightedCategory marks a bucket; showHighlightLine draws a vertical dashed line through it (current hour, selected day, and similar).

Volume by month

6,450

Line overlays

lines draw on top of the bars and appear in the legend so users can toggle them. Each line can use its own y-domain (for example a 0–100% ratio overlay).

Volume by month

6,450

API Reference

BarChartProps<TDatum>

PropTypeDefaultDescription
dataTDatum[]One row per category.
seriesBarChartSeries<TDatum>[]Stacked bar segments. Order is bottom to top.
linesBarChartLineSeries<TDatum>[]Optional overlays drawn above the bars and listed in the legend.
getCategory(datum) => string | numberX-axis key for each row.
categories(string | number)[]from dataFull ordered x-domain when it differs from the data order.
categoryTicks(string | number)[]every categorySubset of categories that render an x-axis label (thin dense axes).
formatCategory(c) => stringString(c)X-axis tick labels.
formatTooltipCategory(c) => stringformatCategoryTooltip header. Use a fuller label when axis ticks are abbreviated.
highlightedCategorystring | numberEmphasizes one bucket (dimmed peers).
showHighlightLinebooleanfalseVertical dashed marker through highlightedCategory.
referenceLine{ value, label?, color? }Horizontal threshold across the plot.
titlestringSection title above the legend.
yAxisUnitstringUnit drawn once above the y-axis; ticks use the compact shared formatter.
formatYTick(value: number) => stringcompact ticksOverrides y-axis tick labels.
yAxisWidthnumbermeasuredFixed y-axis gutter width in px.
heightnumber220Plot height in px.
renderTotal(value: number, animated: boolean) => ReactNodeCustom total above the chart. animated is false while a bar is hovered so totals can snap.
formatTotal(value: number) => stringlocale numberDefault total formatter when renderTotal is omitted.
formatValue(value: number) => stringformatTotalPer-series tooltip values.
classNamestringMerges onto the root.

BarChartSeries<TDatum>

FieldTypeDescription
keystringStable series id (legend, tooltip, stack key).
labelstringLegend and tooltip label.
colorstringCSS color for the bar fill.
hoverFillstringOptional fill while that segment is hovered.
getValue(datum: TDatum) => numberValue read from each row. Negatives stack below zero.

BarChartLineSeries<TDatum>

FieldTypeDescription
keystringStable id.
labelstringLegend and tooltip label.
colorstringStroke color.
getValue(datum) => number | nullnull breaks the line (gap for missing readings).
domain[number, number]Own y-domain. Defaults to the bar scale.
hiddenByDefaultbooleanStarts hidden; the user can enable it from the legend.
formatValue(value: number) => stringTooltip formatter for this line. Defaults to the chart formatValue.
strokeDasharraystringDash pattern for the overlay stroke.

Accessibility

  • Bars are not individually focusable. Provide a summary or table when values drive a decision.
  • The legend toggles series with buttons (aria-pressed). Hidden series get a struck-through label and reduced opacity.
  • Hover tooltips are pointer-only; keep critical totals visible via title / renderTotal.

On this page