Skip to main content

Personal Finance: Budgeting and Debt

Charts for a budgeting app or a bank's dashboard: money flowing out of a paycheck, where it goes, and what a loan, a tax bill or a rent costs over time. Each example builds its numbers from a few lines of arithmetic, so change the rate, the amount or the years in its code and the chart redraws. Saving and Investing has the other half of the genre.

note

The numbers on this page are made up to show the charts. They are not financial advice.


Where the Money Goes​

Paycheck Flow​

Where one monthly paycheck goes: taxes, a 401(k) and benefits come out first, and the take-home pay splits into the month's spending and savings. A Sankey shows the whole path at once, each link as wide as the dollars along it.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const paycheck = {
    nodes: [
      { name: 'Gross pay', color: 'grey' },
      { name: 'Taxes', color: 'danger' },
      { name: '401(k)', color: 'success' },
      { name: 'Benefits', color: 'info' },
      { name: 'Take-home pay', color: 'link' },
      { name: 'Rent', color: 'warning' },
      { name: 'Groceries', color: 'warning' },
      { name: 'Transport', color: 'warning' },
      { name: 'Utilities', color: 'warning' },
      { name: 'Fun', color: 'warning' },
      { name: 'Savings', color: 'success' },
    ],
    links: [
      { source: 0, target: 1, value: 1570 },
      { source: 0, target: 2, value: 390 },
      { source: 0, target: 3, value: 240 },
      { source: 0, target: 4, value: 4300 },
      { source: 4, target: 5, value: 1800 },
      { source: 4, target: 6, value: 600 },
      { source: 4, target: 7, value: 350 },
      { source: 4, target: 8, value: 250 },
      { source: 4, target: 9, value: 500 },
      { source: 4, target: 10, value: 800 },
    ],
  };

  return (
    <Sankey
      label="Where a $6,500 monthly paycheck goes"
      data={paycheck}
      valueFormatter={value => dollars.format(value)}
      height={360}
    />
  );
}

Budget by Needs, Wants and Savings​

A common rule of thumb splits take-home pay 50/30/20: half for needs, 30% for wants and 20% for savings. Here it is for the paycheck above. A donut suits a few parts of one whole, and its center holds the whole.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const takeHome = 4300;
  const budget = [
    { name: 'Needs', value: takeHome * 0.5, color: 'info' },
    { name: 'Wants', value: takeHome * 0.3, color: 'warning' },
    { name: 'Savings', value: takeHome * 0.2, color: 'success' },
  ];

  return (
    <PieChart
      label="A 50/30/20 budget of $4,300 a month"
      data={budget}
      isDonut
      centerLabel={dollars.format(takeHome)}
      valueFormatter={value => dollars.format(value)}
      showLabels
      labelsType="percent"
      hasLegend
      height={280}
    />
  );
}

Spending Breakdown​

Three months of card spending, by month, then category, then merchant. The transactions come from a seeded random generator, and the example sums them into the tree the sunburst takes, the way an app would from its own records. Each category has its own color, which its merchants take.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  // mulberry32, by Tommy Ettinger (2017), dedicated to the public domain
  // under CC0 1.0:
  // https://gist.github.com/tommyettinger/46a874533244883189143505d203312c
  // A seeded random number generator: the same seed gives the same
  // transactions on every render.
  function mulberry32(seed: number) {
    return () => {
      seed = (seed + 0x6d2b79f5) | 0;
      let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
      t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
      return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
    };
  }
  const random = mulberry32(2026);
  const merchants = [
    { category: 'Groceries', name: 'Maple Street Grocer' },
    { category: 'Groceries', name: 'Riverside Market' },
    { category: 'Transport', name: 'Northline Transit' },
    { category: 'Transport', name: 'Pinecrest Fuel' },
    { category: 'Dining out', name: 'Juniper Cafe' },
    { category: 'Dining out', name: 'Lantern Noodle Bar' },
  ];
  const months = ['September', 'October', 'November'];
  const transactions = months.flatMap(month =>
    Array.from({ length: 40 }, () => ({
      month,
      merchant: merchants[Math.floor(random() * merchants.length)].name,
      amount: Math.round(8 + random() * 60),
    }))
  );
  const categories = [
    { name: 'Groceries', color: 'success' },
    { name: 'Transport', color: 'info' },
    { name: 'Dining out', color: 'warning' },
  ];
  const shades = ['grey-dark', 'grey', 'grey-light'];
  const spending = months.map((month, i) => ({
    name: month,
    color: shades[i],
    children: categories.map(category => ({
      ...category,
      children: merchants
        .filter(merchant => merchant.category === category.name)
        .map(merchant => ({
          name: merchant.name,
          value: transactions
            .filter(t => t.month === month && t.merchant === merchant.name)
            .reduce((sum, t) => sum + t.amount, 0),
        })),
    })),
  }));

  return (
    <SunburstChart
      label="Card spending by month, category and merchant"
      data={spending}
      valueFormatter={value => dollars.format(value)}
      height={360}
    />
  );
}


Taxes and Loans​

Tax Brackets, Marginal and Effective​

A made-up bracket schedule, where each rate applies only to the income inside its bracket. The marginal rate jumps at each bracket, so it's drawn as a step (curveType: 'stepAfter'), and the effective rate, all tax over all income, climbs smoothly below it.

function example() {
  // Each rate applies to the income from its `from` up to the next one's.
  const brackets = [
    { from: 0, rate: 10 },
    { from: 10000, rate: 20 },
    { from: 40000, rate: 30 },
    { from: 90000, rate: 35 },
    { from: 200000, rate: 40 },
  ];
  const marginal = (income: number) =>
    brackets.reduce((rate, b) => (income >= b.from ? b.rate : rate), 0);
  const tax = (income: number) =>
    brackets.reduce((total, b, i) => {
      const to = i + 1 < brackets.length ? brackets[i + 1].from : Infinity;
      return (
        total + (Math.max(0, Math.min(income, to) - b.from) * b.rate) / 100
      );
    }, 0);
  const short = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    notation: 'compact',
  });
  const incomes = Array.from({ length: 51 }, (_, i) => {
    const income = i * 10000;
    return {
      income: short.format(income),
      marginal: marginal(income),
      effective: income ? Math.round((tax(income) / income) * 1000) / 10 : 0,
    };
  });

  return (
    <LineChart
      label="Marginal and effective tax rates by income"
      data={incomes}
      dataKey="income"
      series={[
        {
          name: 'marginal',
          label: 'Marginal rate',
          color: 'danger',
          curveType: 'stepAfter',
        },
        { name: 'effective', label: 'Effective rate', color: 'info' },
      ]}
      unit="%"
      xAxisLabel="Income"
      hasDots={false}
      hasLegend
    />
  );
}

Mortgage Amortization​

A $400,000 loan at 6.5% over 30 years, each year's payments split into interest and principal. The payment stays the same, so every stacked bar is the same height, and the split inside it shows the interest shrinking as the balance falls.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const loan = 400000;
  const rate = 0.065 / 12;
  const payment = (loan * rate) / (1 - (1 + rate) ** -360);
  let balance = loan;
  const years = Array.from({ length: 30 }, (_, i) => {
    let principal = 0;
    let interest = 0;
    for (let month = 0; month < 12; month++) {
      const owed = balance * rate;
      interest += owed;
      principal += payment - owed;
      balance -= payment - owed;
    }
    return {
      year: i + 1,
      interest: Math.round(interest),
      principal: Math.round(principal),
    };
  });

  return (
    <BarChart
      label={`A $400,000 mortgage at 6.5%: ${dollars.format(payment)} a month`}
      type="stacked"
      data={years}
      dataKey="year"
      series={[
        { name: 'interest', label: 'Interest', color: 'warning' },
        { name: 'principal', label: 'Principal', color: 'info' },
      ]}
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Year of the loan"
      hasLegend
    />
  );
}

Debt Payoff, Avalanche Against Snowball​

Four debts paid down with $900 a month: the minimums first, then the rest on one debt at a time, either the highest rate first (the avalanche) or the smallest balance first (the snowball). The total balance under each plan shows which is done sooner, and the legend gives the interest each one pays.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const debts = [
    { name: 'Store card', balance: 2000, rate: 0.27, minimum: 60 },
    { name: 'Credit card', balance: 9500, rate: 0.23, minimum: 250 },
    { name: 'Car loan', balance: 7500, rate: 0.08, minimum: 220 },
    { name: 'Medical bill', balance: 1200, rate: 0, minimum: 50 },
  ];
  type Debt = (typeof debts)[number];
  const budget = 900;
  const total = (list: Debt[]) => list.reduce((sum, d) => sum + d.balance, 0);

  // Each month: add the interest, pay every minimum, then put what's left on
  // the debts in `order`. A budget below the minimums pays what it can, and
  // the run stops after 50 years in case it never catches up.
  function payOff(order: (a: Debt, b: Debt) => number) {
    const left = debts.map(debt => ({ ...debt }));
    const totals = [total(left)];
    let interest = 0;
    while (total(left) > 0 && totals.length <= 600) {
      let cash = budget;
      for (const debt of left) {
        const owed = (debt.balance * debt.rate) / 12;
        const paid = Math.min(debt.minimum, debt.balance + owed, cash);
        interest += owed;
        debt.balance += owed - paid;
        cash -= paid;
      }
      for (const debt of [...left].sort(order)) {
        const paid = Math.min(cash, debt.balance);
        debt.balance -= paid;
        cash -= paid;
      }
      totals.push(total(left));
    }
    return { totals, interest };
  }
  const avalanche = payOff((a, b) => b.rate - a.rate);
  const snowball = payOff((a, b) => a.balance - b.balance);
  const months = Array.from(
    { length: Math.max(avalanche.totals.length, snowball.totals.length) },
    (_, month) => ({
      month,
      avalanche: Math.round(avalanche.totals[month] ?? 0),
      snowball: Math.round(snowball.totals[month] ?? 0),
    })
  );

  return (
    <LineChart
      label="Total debt by month, avalanche against snowball"
      data={months}
      dataKey="month"
      series={[
        {
          name: 'avalanche',
          label: `Avalanche: ${dollars.format(avalanche.interest)} interest`,
          color: 'success',
        },
        {
          name: 'snowball',
          label: `Snowball: ${dollars.format(snowball.interest)} interest`,
          color: 'warning',
          strokeDasharray: '5 5',
        },
      ]}
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Months"
      hasDots={false}
      hasLegend
    />
  );
}

Rent Against Buy​

Renting at $2,400 a month, rising 3% a year, against buying a $450,000 home with 20% down at 6.5%. The buying line is what owning has cost so far: the down payment, closing costs, mortgage payments and upkeep, and what the cash paid up front would have earned invested at 5% a year, less what selling would bring back after fees. The reference line marks the year it drops below rent.

function example() {
  const dollars = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    maximumFractionDigits: 0,
  });
  const price = 450000;
  const down = price * 0.2;
  const rate = 0.065 / 12;
  const payment = ((price - down) * rate) / (1 - (1 + rate) ** -360);
  const upkeep = (price * 0.02) / 12; // property tax, insurance and repairs
  let balance = price - down;
  let rent = 2400;
  let rented = 0;
  const upFront = down + price * 0.03; // the down payment and closing costs
  let owned = upFront;
  const years = Array.from({ length: 11 }, (_, year) => {
    if (year > 0) {
      for (let month = 0; month < 12; month++) {
        rented += rent;
        owned += payment + upkeep;
        balance -= payment - balance * rate;
      }
      rent *= 1.03;
    }
    // Selling at 4% a year of growth, less 6% in fees, pays off the loan.
    const equity = price * 1.04 ** year * 0.94 - balance;
    // What the cash paid up front would have earned invested instead.
    const forgone = upFront * (1.05 ** year - 1);
    return {
      year,
      rent: Math.round(rented),
      buy: Math.round(owned + forgone - equity),
    };
  });
  const breakEven = years.find(row => row.buy <= row.rent)?.year;

  return (
    <LineChart
      label="The cost of renting and of buying, year by year"
      data={years}
      dataKey="year"
      series={[
        { name: 'rent', label: 'Renting', color: 'warning' },
        { name: 'buy', label: 'Buying', color: 'link' },
      ]}
      referenceLines={
        breakEven === undefined
          ? []
          : [{ x: breakEven, label: 'Buying pulls ahead', color: 'success' }]
      }
      valueFormatter={value => dollars.format(value)}
      xAxisLabel="Years"
      hasLegend
    />
  );
}