Skip to content
Tutorials Center
My learning

Topics

Lesson 79 of 87 ยท Projects

Project: Expense Tracker with Charts

Build an expense tracker in Vue 3: a validated form, money kept in cents, computed totals, a safe localStorage composable, category filters and a hand-made SVG bar chart component.

An expense tracker is the kind of app people actually build for themselves, and it exercises exactly what business apps need: a form with rules, a list that changes, totals that must always be correct, saved data and a chart. We will build it without a chart library, so you see how little it takes to turn numbers into a picture.

The plan has two stages. Stage 1 is the form, the list and the totals. Stage 2 adds category filters, a chart component drawn with SVG, and saving to the browser. Skills you will use: forms and v-model, validation, computed properties, components and props and composables.

Design first

Store money as integer cents. 0.1 + 0.2 is 0.30000000000000004 in JavaScript, and a total that is off by a hair is a bug nobody forgives. Keep amountCents (an integer), do arithmetic on integers and convert to a display string only at the edge with Intl.NumberFormat.

An entry is { id, title, amountCents, category, type, date }, where type is "income" or "expense". The state is the list of entries (plus the form fields and the filter). Everything shown is computed: the totals, the balance, the filtered list and the chart data.

flowchart LR
    F["Form fields"] -- "validate, add()" --> E["entries (state)"]
    E --> T["income, expenses, balance (computed)"]
    E --> C["byCategory (computed)"]
    C --> G["BarChart component (props)"]
    FL["filter (state)"] --> V["visibleEntries (computed)"]
    E --> V

One list of entries feeds every number and picture on the screen.

Stage 1: form, list and totals

The form validates before it adds. Invalid input never reaches the list, and each rule produces a visible message. The amount field is converted with Math.round(Number(value) * 100), which turns 12.5 into 1250.

Stage 1: add entries, see totals

Add "Coffee" for 3.10, then 0.1 and 0.2 as two entries: the balance stays exact, because every sum is made of integers. Try an empty description or the amount abc to see both messages at once.

Points to copy:

  1. Validation collects all problems instead of stopping at the first, so the user fixes everything in one go.
  2. Number(" ") is 0, and Number("") is 0, so the positive-amount check also catches blank input.
  3. Totals are computed. Deleting an entry changes entries, and every figure follows with no extra code.
  4. The form keeps its type after adding, because people tend to enter several expenses in a row.

Stage 2: categories, a chart component and saved data

Now three upgrades. First, each entry gets a category, and a computed byCategory totals the expenses per category. Second, a small BarChart component receives that data as a prop and draws SVG bars: the component knows nothing about expenses, only { label, value } pairs, so you could reuse it anywhere. Third, the entries are saved.

Saving needs care. localStorage can throw (private browsing, blocked storage, a full disk) and the preview here cannot use it at all, so the composable reads and writes inside try/catch and falls back to memory. In your own project, on a normal page, the same code persists across reloads (composable patterns explains why storage should be injectable).

Stage 2: category filter, SVG bar chart and storage

Add a "Dinner" expense in Food: the Food bar grows and the bars re-sort, because byCategory is computed from entries. Click a category chip to filter the list; the chart still shows all spending, because it reads entries, not visible. That is a deliberate choice: decide per view which list it shows.

How the pieces fit:

  • BarChart is a pure presentational component (component design): data in, drawing out. The format prop lets the parent decide how values are written, so the chart has no idea they are dollars.
  • Scaling is computed. The longest bar always fills the available width (max is the largest value, or 1 to avoid dividing by zero), so any amount range fits.
  • watch(state, ..., { deep: true }) saves on every change, including edits inside an entry. useStoredRef returns a normal ref, so nothing else in the app knows about storage.
  • SVG gets role="img" and an aria-label, so screen readers announce what the picture is. For a real product also provide the numbers as text, which the list does.

Where to go next

  • Add a month picker and group by month. Store the date as an ISO string (2026-11-10) and compare strings.
  • Edit an entry: reuse the form, fill it from the clicked entry, and save with the same id.
  • Export to CSV: build a string from entries, then create a Blob and a download link.
  • Replace the hand-made chart with a charting library (for example Chart.js through a thin wrapper component) when you need tooltips, axes and animation. Keep the same { label, value } prop so the rest of the app does not change.
  • Replace the storage composable with a server API (fetching data).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the tracker store amounts as integer cents?

    0.1 + 0.2 is 0.30000000000000004 in JavaScript. Integer arithmetic on cents is exact, and you format to dollars only for display.
  2. 2. Why is BarChart given { label, value } pairs rather than the list of expenses?

    Keeping the component presentational and generic makes it reusable and easy to test. The parent shapes the data.
  3. 3. Why is localStorage access wrapped in try/catch?

    Private modes, blocked cookies and quota errors make storage unreliable. The app should degrade gracefully to memory.
  4. 4. The category filter changes the list but not the chart. Why?

    Each view chooses which data it shows. Both derive from the same entries, but only the list uses the filter.

Summary

  • Store money as integer cents; format only at the edge with Intl.NumberFormat.
  • Validate in the action and collect all messages; invalid data never enters the list.
  • Totals, category sums, filtered lists and chart data are all computed from one entries list.
  • A presentational BarChart takes { label, value } pairs and draws SVG with computed scales; give it an accessible label.
  • Wrap storage in try/catch inside a composable and save with a deep watch.

Next: debounced search and routing against an API, Project: Movie Search.