Interactive Answers

Diverging Bars

Stacked daily columns with one series hanging below zero on its own scale — triaged vs escalated, solved vs handed off.

3 variantsMIT

Preview

Installation

Copies the component into components/ui and installs its dependencies and theme tokens.

pnpm dlx shadcn@latest add https://uilab.tmih06.com/r/diverging-bars.json

Usage

Pass your own data and wire the callbacks to your agent runtime.

import { DivergingBars } from "@/components/ui/diverging-bars";

// A security agent's month: alerts by source, escalations hanging below.
export function TriageVolume({ report }: { report: TriageReport }) {
  return (
    <DivergingBars
      title="Alerts by source"
      subtitle="Security agent · last 30 days"
      labels={report.days.map((d) => d.label)}
      series={report.sources.map((s) => ({
        id: s.id,
        label: s.name,
        values: report.days.map((d) => d.bySource[s.id] ?? 0),
      }))}
      below={{
        id: "escalated",
        label: "Escalated",
        values: report.days.map((d) => d.escalated),
      }}
      headline={{
        value: report.total.toLocaleString("en-US"),
        caption: "alerts triaged in 30 days",
      }}
      aside={`${report.autoClosedPct}% closed without a human`}
      onSelect={(i) => openDay(report.days[i].date)}
    />
  );
}

// Pass null while the series request is in flight; keep stale data and set
// loading when refetching.
export function AlertState({ query }: { query: AlertsQuery }) {
  return (
    <DivergingBars
      title="Alerts by severity"
      labels={query.data?.labels ?? null}
      series={query.data?.series ?? null}
      below={query.data?.below ?? null}
      loading={query.isRefetching}
      skeleton={{ count: 30, series: 7, below: true }}
    />
  );
}

Props

PropTypeDefaultDescription
labels*string[] | null—One label per column, e.g. a date. Every few are drawn under the chart.
series*DivergingSeries[] | null—Stacked upward as separate segments, first series at the bottom. Colors default to the palette by position.
belowDivergingSeries | null—One series that hangs below the zero line on its own smaller scale. Defaults to the negative color.
loadingbooleanfalseForce the fixed-footprint skeleton while refetching, even when stale series data is available.
skeleton{ count?: number; series?: number; below?: boolean }—Loading footprint hints for bar positions, legend entries, and whether the optional lower strip is expected.
title*string—Header title.
subtitlestring—Secondary line under the title.
headline{ value: string; caption?: string } | null—Big number above the chart with a caption. Rolls when it changes.
asideReactNode | null—Right side of the headline row, e.g. a secondary stat.
format(value: number) => stringcompact (1.2k)Formats ticks, the tooltip and the hidden table.
onSelect(index: number) => void—Called when a column is hovered, clicked or reached with the arrow keys.
stringsPartial<DivergingBarsLabels>—Your own UI strings (loading announcement, legend group, screen-reader text), for translating. Right-to-left works under `dir="rtl"`; the plot still runs left to right.
classNamestring—Extra classes for the root element.
type DivergingSeries = {
  id: string;
  label: string;
  values: number[]; // one per label
  color?: string; // any CSS color
};

// Legend chips toggle series; at least one stacked series stays on.
// The tooltip total is the stack; the series below is listed but not added in.