Project: Data Dashboard
A dashboard turns raw data into answers at a glance: how much was collected this month, who still owes money, which payment method is most popular. This project builds the Fee Tracker's payments dashboard. It loads payments from an API, handles the slow and failing cases honestly, lets staff filter, search and sort, shows summary cards, and draws a bar chart with SVG. Every number on the page is derived from one list of payments plus the current controls, so the cards, the table and the chart can never disagree.
What You'll Build
- Data loaded with
fetch, with a loading message, an error message and a Retry button. - Summary cards: total collected, number of payments, average payment, largest payer.
- Controls: method filter, name search and sortable table columns.
- A bar chart of totals per method, drawn as SVG from the same filtered data.
- Controls stored in the URL, so a filtered view can be shared.
Step 1: The Data
The API returns payments with amounts in cents (integers avoid floating-point rounding problems):
[
{ "id": 1, "name": "Navika", "method": "card", "amountCents": 12000, "date": "2026-01-05" },
{ "id": 2, "name": "Advitha", "method": "ach", "amountCents": 4500, "date": "2026-01-06" },
{ "id": 3, "name": "Srihitha", "method": "check", "amountCents": 45000, "date": "2026-01-08" },
{ "id": 4, "name": "Navika", "method": "card", "amountCents": 6000, "date": "2026-01-12" },
{ "id": 5, "name": "Advitha", "method": "card", "amountCents": 2550, "date": "2026-01-15" }
]Step 2: Pure Data Functions
Filtering, sorting and summarizing are pure functions of the payments and the current view (the controls' values):
const payments = [
{ id: 1, name: "Navika", method: "card", amountCents: 12000 },
{ id: 2, name: "Advitha", method: "ach", amountCents: 4500 },
{ id: 3, name: "Srihitha", method: "check", amountCents: 45000 },
{ id: 4, name: "Navika", method: "card", amountCents: 6000 },
{ id: 5, name: "Advitha", method: "card", amountCents: 2550 },
];
function select(payments, { method = "all", search = "", sortBy = "id", dir = 1 }) {
const term = search.trim().toLowerCase();
return payments
.filter((p) => (method === "all" || p.method === method) && p.name.toLowerCase().includes(term))
.toSorted((a, b) => (a[sortBy] < b[sortBy] ? -dir : a[sortBy] > b[sortBy] ? dir : 0));
}
function summarize(rows) {
const total = rows.reduce((sum, p) => sum + p.amountCents, 0);
const byName = Map.groupBy(rows, (p) => p.name);
const top = [...byName].map(([name, ps]) => [name, ps.reduce((s, p) => s + p.amountCents, 0)]).toSorted((a, b) => b[1] - a[1])[0];
return { total, count: rows.length, average: rows.length ? Math.round(total / rows.length) : 0, top: top?.[0] ?? "none" };
}
const cardOnly = select(payments, { method: "card", sortBy: "amountCents", dir: -1 });
console.log(cardOnly.map((p) => `${p.name} ${p.amountCents}`));
console.log(summarize(cardOnly));
console.log(summarize(select(payments, { search: "zz" })));['Navika 12000', 'Navika 6000', 'Advitha 2550']
{ total: 20550, count: 3, average: 6850, top: 'Navika' }
{ total: 0, count: 0, average: 0, top: 'none' }toSorted returns a new array, so the original payments are never reordered. The empty case is handled explicitly: no division by zero, and a sensible "none".
Step 3: An SVG Bar Chart
SVG elements are created with createElementNS and the SVG namespace. Bar widths are scaled to the largest value:
const SVG = "http://www.w3.org/2000/svg";
function barChart(entries, { width = 320, barHeight = 22, gap = 8 } = {}) {
const max = Math.max(1, ...entries.map(([, v]) => v));
const svg = document.createElementNS(SVG, "svg");
svg.setAttribute("viewBox", `0 0 ${width} ${entries.length * (barHeight + gap)}`);
svg.setAttribute("role", "img");
svg.setAttribute("aria-label", entries.map(([k, v]) => `${k}: ${usd(v)}`).join(", "));
entries.forEach(([label, value], i) => {
const rect = document.createElementNS(SVG, "rect");
rect.setAttribute("x", 80);
rect.setAttribute("y", i * (barHeight + gap));
rect.setAttribute("width", Math.round(((width - 80) * value) / max));
rect.setAttribute("height", barHeight);
const text = document.createElementNS(SVG, "text");
text.setAttribute("y", i * (barHeight + gap) + 16);
text.textContent = label;
svg.append(rect, text);
});
return svg;
}The aria-label gives screen reader users the same numbers as a sentence, since they can't see bar lengths. For bigger dashboards, a chart library (see "Chart Libraries") adds axes, tooltips and animation, but the idea is the same.
Step 4: Loading, Errors and Retry
Data can be slow or fail. The page should always say what's happening:
| Status | Shows |
|---|---|
loading | "Loading payments..." with aria-busy="true" |
error | What went wrong, and a Retry button |
ready | Cards, table and chart |
The Complete Dashboard
The checker waits for the data, filters to card payments, sorts by amount (twice, to reverse the order), and searches for "nav":
<main id="app" aria-busy="true">
<p id="status">Loading payments...</p>
<label>Method <select id="method"><option value="all">All</option><option value="card">Card</option><option value="ach">ACH</option><option value="check">Check</option></select></label>
<label>Search <input id="search" type="search"></label>
<section id="cards"></section>
<table><thead><tr><th data-sort="name">Name</th><th data-sort="method">Method</th><th data-sort="amountCents">Amount</th></tr></thead><tbody id="rows"></tbody></table>
<div id="chart"></div>
</main>
<script type="module">
const SVG = "http://www.w3.org/2000/svg";
const fmt = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" });
const usd = (cents) => fmt.format(cents / 100);
let payments = [];
let view = { method: "all", search: "", sortBy: "id", dir: 1 };
const select = (ps, v) => ps
.filter((p) => (v.method === "all" || p.method === v.method) && p.name.toLowerCase().includes(v.search.trim().toLowerCase()))
.toSorted((a, b) => (a[v.sortBy] < b[v.sortBy] ? -v.dir : a[v.sortBy] > b[v.sortBy] ? v.dir : 0));
const sumBy = (rows, key) => [...Map.groupBy(rows, (p) => p[key])].map(([k, ps]) => [k, ps.reduce((s, p) => s + p.amountCents, 0)]);
function barChart(entries) {
const max = Math.max(1, ...entries.map(([, v]) => v));
const svg = document.createElementNS(SVG, "svg");
svg.setAttribute("viewBox", `0 0 320 ${entries.length * 30}`);
svg.setAttribute("role", "img");
svg.setAttribute("aria-label", entries.map(([k, v]) => `${k}: ${usd(v)}`).join(", "));
entries.forEach(([label, value], i) => {
const rect = document.createElementNS(SVG, "rect");
Object.entries({ x: 80, y: i * 30, width: Math.round((240 * value) / max), height: 22 }).forEach(([k, v]) => rect.setAttribute(k, v));
const text = document.createElementNS(SVG, "text");
text.setAttribute("y", i * 30 + 16);
text.textContent = label;
svg.append(rect, text);
});
return svg;
}
function render() {
const rows = select(payments, view);
const total = rows.reduce((s, p) => s + p.amountCents, 0);
document.querySelector("#cards").textContent = `Total ${usd(total)} | ${rows.length} payments | Average ${usd(rows.length ? Math.round(total / rows.length) : 0)}`;
document.querySelector("#rows").replaceChildren(...rows.map((p) => {
const tr = document.createElement("tr");
for (const value of [p.name, p.method, usd(p.amountCents)]) tr.append(Object.assign(document.createElement("td"), { textContent: value }));
return tr;
}));
const chart = barChart(sumBy(rows, "method"));
document.querySelector("#chart").replaceChildren(chart);
const order = rows.map((p) => `${p.name} ${usd(p.amountCents)}`).join(", ");
console.log(`${document.querySelector("#cards").textContent} || ${order} || chart: ${chart.getAttribute("aria-label")}`);
const params = new URLSearchParams({ method: view.method, q: view.search, sort: `${view.sortBy}:${view.dir}` });
history.replaceState(null, "", `?${params}`);
}
function update(patch) { view = { ...view, ...patch }; render(); }
document.querySelector("#method").addEventListener("change", (e) => update({ method: e.target.value }));
document.querySelector("#search").addEventListener("input", (e) => update({ search: e.target.value }));
document.querySelector("thead").addEventListener("click", (e) => {
const key = e.target.closest("th[data-sort]")?.dataset.sort;
if (key) update({ sortBy: key, dir: view.sortBy === key ? -view.dir : 1 });
});
async function load() {
const status = document.querySelector("#status");
try {
const res = await fetch("/api/payments.json");
if (!res.ok) throw new Error(`HTTP ${res.status}`);
payments = await res.json();
status.textContent = "";
render();
} catch (err) {
status.textContent = `Couldn't load payments (${err.message}).`;
status.append(Object.assign(document.createElement("button"), { textContent: "Retry", onclick: load }));
} finally {
document.querySelector("#app").setAttribute("aria-busy", "false");
}
}
load();
</script>Total $700.50 | 5 payments | Average $140.10 || Navika $120.00, Advitha $45.00, Srihitha $450.00, Navika $60.00, Advitha $25.50 || chart: card: $205.50, ach: $45.00, check: $450.00
Total $205.50 | 3 payments | Average $68.50 || Navika $120.00, Navika $60.00, Advitha $25.50 || chart: card: $205.50
Total $205.50 | 3 payments | Average $68.50 || Advitha $25.50, Navika $60.00, Navika $120.00 || chart: card: $205.50
Total $205.50 | 3 payments | Average $68.50 || Navika $120.00, Navika $60.00, Advitha $25.50 || chart: card: $205.50
Total $180.00 | 2 payments | Average $90.00 || Navika $120.00, Navika $60.00 || chart: card: $180.00The cards, the table order and the chart all changed together on every control change, because they're all derived from select(payments, view).
Practice
Challenge: Add a "payments per day" summary that groups the filtered rows by date and returns [date, count] pairs in date order, ready for a second chart:
Show solution
const rows = [
{ date: "2026-01-06", amountCents: 4500 },
{ date: "2026-01-05", amountCents: 12000 },
{ date: "2026-01-06", amountCents: 2550 },
];
const perDay = (rows) => [...Map.groupBy(rows, (p) => p.date)].map(([d, ps]) => [d, ps.length]).toSorted(([a], [b]) => a.localeCompare(b));
console.log(perDay(rows));[['2026-01-05', 1], ['2026-01-06', 2]]Key Takeaways
| Piece | Technique |
|---|---|
| Data | fetch, res.ok, cents as integers |
| View | { method, search, sortBy, dir } |
| Derived | select, summarize, Map.groupBy |
| Chart | SVG rect and text, scaled to the max |
| Sharing | URLSearchParams, history.replaceState |
Was this tutorial helpful?