Skip to content
Tutorials Center
My learning

Topics

Lesson 83 of 87 ยท Projects

Project: Reporting Dashboard with Composition API

Build a reporting dashboard in Vue 3 with the Composition API: composables for data, filters and tables, KPI cards, a sortable paginated table, then SVG charts, provide/inject widgets and a CSV export.

Dashboards are where the Composition API earns its keep. A typical one loads a lot of data, lets users slice it with filters, and shows the same numbers in several forms: tiles, a table, charts, an export. With the Options API such a component grows into hundreds of lines of mixed data, computed and methods. With composables, each concern becomes a small function with a clear name, and the dashboard reads like a table of contents.

We build it in two stages. Stage 1 creates three composables (useOrders, useFilters, useTable) and uses them for KPI tiles and a sortable, paginated table. Stage 2 adds a chart component, a regional breakdown, a CSV export and widgets that get their data with provide/inject, so the dashboard is assembled from independent parts. Background: composables, composable patterns, computed, provide and inject and component design.

Design first

Sketch the data flow before the code. The raw orders come from one source. Filters narrow them to filtered. Everything on the screen, the KPIs, the table and each chart, is a view of filtered. Nothing on the screen holds its own copy of the data.

flowchart LR
    O["useOrders<br/>orders, status"] --> F["useFilters<br/>region, range<br/>filtered (computed)"]
    F --> K["KPI tiles<br/>(computed)"]
    F --> T["useTable<br/>sort, page"]
    F --> C["Charts<br/>(computed series)"]
    F --> X["CSV export"]

Change a filter and every view updates, because they all derive from the same filtered.

Each composable has one job and one clear contract:

Composable Takes Returns
useOrders() nothing orders, status, reload
useFilters(orders) a ref or getter of all orders region, range, filtered, option lists
useTable(rows, options) the rows to show sortKey, sortDir, page, pageRows, pages, sortBy

A composable that receives its input (instead of importing a global) is easy to test and to reuse: useTable does not know about orders and could power any table in the app (testing composables).

Stage 1: composables, KPIs and a table

The data is generated by a seeded pseudo-random function, so every visitor sees the same 120 orders and the numbers are predictable. useOrders pretends to load them from a server (500 ms) and exposes a status.

Stage 1: useOrders, useFilters, useTable, KPIs and a sortable table

Wait for the loading line to finish, then click the Amount header to sort by value (click again to reverse). Choose a region or a period: the KPI tiles, the table and the page count all change, and you are returned to page 1.

Why this structure works:

  1. useTable knows nothing about orders. It sorts by any key, so it could drive a users table tomorrow. Pass it a ref or a getter and it uses toValue to read it either way (composable patterns).
  2. Sorting copies the array ([...rows].sort), because sorting in place would reorder the filtered list that the KPIs also read.
  3. watch resets the page when the number of rows changes. Without it, narrowing a filter while on page 5 would leave you on an empty page.
  4. The dashboard component has almost no logic. setup calls three composables and derives four KPI values. Everything else is in named functions you can read, reuse and test separately.
  5. useOrders is the only place that talks to the "server". Swapping the generator for a fetch (fetching data) changes one function.

The weakness: the dashboard is one big template, and numbers are shown only as figures. A dashboard that has charts and an export should be built from widgets.

Stage 2: charts, widgets and an export

Now we split the screen into widgets: KpiCard, LineChart, RegionBars and CsvExport. To stop the root component from passing filtered as a prop to each of them (prop drilling gets tedious on a big dashboard), the root provides one context object and each widget injects what it needs (provide and inject). Widgets become independent: you can add, remove or reorder them without touching the others.

Two new computed views of filtered appear:

  • monthly: revenue per month. The grouping key is the first seven characters of the ISO date (2026-03), which sorts correctly as a string.
  • byRegion: revenue per region, sorted descending.

The LineChart is plain SVG. It converts each { label, value } to coordinates with a scale function (x spread across the width, y flipped because SVG's y axis points down) and draws a polyline plus a dot per month. The CSV export builds a string with quoting for commas and quotes, and shows it in a preview box. In a normal page you could turn the same string into a download with a Blob and a temporary link; the preview sandbox blocks downloads, so we display it instead.

Stage 2: widgets with provide/inject, SVG line chart, breakdown and CSV

Switch Period to "First half" and Region to "East": the KPIs, the line (now with six points), the bars and the CSV preview all change together, and the root component only wrote two <select> elements. Hover a dot to see the exact value in a tooltip (an SVG <title>).

What to take from Stage 2:

  • Widgets are independent. Each one injects the context and derives its own view. Adding a "Top products" widget means writing one component and adding one line to the template.
  • The context is the contract. The provided object lists what widgets may use (filtered, revenue, monthly, byRegion). For a larger app, give the provide key a name constant or a Symbol and a TypeScript type (TypeScript with Vue). Provide refs and computed values, not their .value, so injected values stay reactive.
  • Scales are just arithmetic. x is index ร— step, y is padding + height ร— (1 โˆ’ value / max). Once you see that, a chart library is a convenience, not magic; reach for one (Chart.js, ECharts) when you need axes, legends and animations.
  • CSV needs quoting. A cell containing a comma, a quote or a newline must be wrapped in quotes with inner quotes doubled. Skipping this is the classic export bug.
  • Accessibility. The chart has role="img" and an aria-label, and the same numbers are available as text in the bars and the CSV, so no information is only visual (accessibility).

Where to go next

  • Load the orders from an API with a loading skeleton and an error state, and let useOrders poll every minute (clear the timer with onScopeDispose).
  • Put the filters in the URL so a filtered report can be bookmarked, like the movie search project.
  • Add a date range picker, a product filter and a "compare with previous period" KPI that shows a percentage change.
  • Make useTable support a search box and column visibility, and virtualize very long tables (large lists).
  • Lazy-load the chart widgets with defineAsyncComponent so the first paint is fast.
  • Move the shared context into a Pinia store when widgets live in different pages.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does useTable accept rows as an argument instead of importing the orders?

    Passing the dependency in keeps the composable generic. The same useTable can sort users, invoices or anything else.
  2. 2. Why does the table composable watch the number of rows and reset the page?

    If you are on page 5 and a filter leaves two pages, page 5 would be empty. Resetting to page 1 avoids that.
  3. 3. What is the main advantage of provide/inject for the dashboard widgets?

    The root provides a context once. Each widget injects what it needs, so you can add or remove widgets without changing the others.
  4. 4. In the SVG line chart, why is y calculated as pad.t + (H - pad.t - pad.b) * (1 - value / max)?

    In SVG, y = 0 is the top. The 1 - value/max part flips the scale so that big values appear high on the chart.
  5. 5. What must a CSV exporter do with a cell that contains a comma or a quote?

    Quoting keeps the cell in one column. Embedded quotes are escaped by doubling them.

Summary

  • Split a dashboard by concern into composables: loading (useOrders), narrowing (useFilters), and presentation helpers (useTable, useReports).
  • Make everything on screen a computed view of one filtered list, so changing a filter updates every widget.
  • Give composables their input as an argument (use toValue to accept refs or getters), so they are reusable and testable.
  • Copy before sorting, and reset pagination when the data size changes.
  • Assemble the dashboard from independent widgets that inject a provided context of refs and computed values.
  • A chart is arithmetic plus SVG; quote CSV cells correctly; keep information available as text, not only as pictures.

Next: practise what you learned with Practice Exercises.