Skip to content

CategoryBreakdown

Ranked bars, not a pie.
Implemented
Status
Implemented
Family
7 of 13
Registry
@lopifi/category-breakdown
npx shadcn@latest add @lopifi/theme @lopifi/category-breakdown

The component, rendering

Real data, the shipped component. Switch the theme to check the claims the spec makes below.

Preview

The four-layer spec

Ranked horizontal bars with values and percentages. Not a pie chart. Humans compare lengths accurately and angles poorly; a pie with more than three slices is decoration.

Top N + "Other" (max 6 series). Percentages use largest-remainder allocation so they sum to exactly 100%.

Lifted verbatim from Dashboard & charts. The doc and this page are the same paragraphs, so they cannot disagree.

What lands in your repository

One component is never one file. Everything below is copied into your project, where you own it.

Files

components/ledger/category-breakdown.tsx

Lopifi dependencies · 8
allocatechart-utilscnformat-moneymoneymoney.typespercenttooltip
npm packages
@base-ui-components/reactclass-variance-authorityclsxtailwind-merge
Registry entry
/r/category-breakdown.json

The whole component

No compiled bundle, no black box. This is the file the registry copies, and the file you will edit.

components/ledger/category-breakdown.tsx
"use client";

import { allocatePercentages } from "@/lib/ledger/allocate";
import { vizColorFor } from "@/lib/ledger/chart-utils";
import { cn } from "@/lib/ledger/cn";
import type { MoneyValue } from "@/lib/ledger/money.types";
import { Money } from "@/components/ledger/money";
import { Percent } from "@/components/ledger/percent";

export type Category = { key: string; label: string; amount: number };

export interface CategoryBreakdownProps {
  categories: Category[];
  className?: string;
  currency: string;
  /** Max 6 series; the rest group into "Other". Beyond that is a data problem. */
  maxSeries?: number;
  title?: string;
}

/**
 * Ranked horizontal bars. **Not a pie chart** — humans compare lengths
 * accurately and angles poorly.
 *
 * Percentages use largest-remainder allocation, so they sum to exactly 100%.
 */
export function CategoryBreakdown({
  categories,
  currency,
  maxSeries = 6,
  className,
  title,
}: CategoryBreakdownProps) {
  const sorted = [...categories].sort((a, b) => b.amount - a.amount);
  const top = sorted.slice(0, maxSeries - (sorted.length > maxSeries ? 1 : 0));
  const rest = sorted.slice(top.length);
  const rows =
    rest.length > 0
      ? [...top, { amount: rest.reduce((s, c) => s + c.amount, 0), key: "other", label: "Other" }]
      : top;

  const percentages = allocatePercentages(rows.map((r) => r.amount));
  const max = Math.max(...rows.map((r) => r.amount), 1);

  return (
    <div className={cn("w-full", className)}>
      <table className="w-full">
        {title ? <caption className="sr-only">{title}</caption> : null}
        <tbody>
          {rows.map((row, i) => (
            <tr key={row.key}>
              <th className="w-[35%] max-w-0 truncate py-2 pr-3 text-left text-body font-normal" scope="row">
                {row.label}
              </th>
              <td className="py-2">
                <div
                  aria-hidden="true"
                  className="h-4 rounded-xs"
                  style={{
                    background: vizColorFor(row.key),
                    width: `${(row.amount / max) * 100}%`,
                  }}
                />
              </td>
              <td className="w-24 py-2 pl-3 text-right">
                <Money value={{ amount: row.amount, currency } as MoneyValue} />
              </td>
              <td className="w-16 py-2 pl-3 text-right">
                <Percent
                  className="text-text-secondary"
                  signed={false}
                  value={{ kind: "relative", value: percentages[i] }}
                />
              </td>
            </tr>
          ))}
        </tbody>
      </table>
      <p className="mt-2 text-caption text-text-tertiary">
        Shares allocated by largest remainder, so they sum to exactly 100%.
      </p>
    </div>
  );
}

Also in Dashboard & charts

See also

A component is specified in one place, defined in another, and running in a third. All three, from here.

Elsewhere in the system

Where CategoryBreakdown sits in the rest of the system — the live section it renders in, the calculators that compute what it displays, and the ledger payloads it is fed from.

Components are copied into your repository and meant to be edited — rounding policy, fiscal calendar and compliance copy differ per company.

Told when Dashboard & charts changes

Components are copied into your repo, so a fix here does not reach you on its own. One email when a component in this family changes in a way that needs a migration — and nothing when it does not.

One address, no list rental, unsubscribe in one click.

Works in your QuickBooks Online or Xero file

Get your books closed every month

Lopifi is building AI bookkeeping that keeps your books closed on US GAAP as transactions happen, with a CPA review every quarter, for $200 a month. We’re not open yet.

$200/month when we open. No card now.