Skip to content
Tutorials Center
My learning

Topics

Lesson 41 of 44 ยท Projects and Practice

Project: An Interactive Sales Dashboard

Build a small sales dashboard with D3: a daily line chart with a brush that filters a category bar chart and KPI numbers, built step by step in one reusable file.

In this project you put together scales, axes, shape generators, data summaries, a brush and transitions into one small dashboard. The result has three linked parts:

  1. A line chart of daily revenue with a brush along the time axis.
  2. A bar chart of revenue per product category for the selected period.
  3. Three KPI numbers (total, daily average, best day) that update with the selection.

Dragging the brush on the line chart filters everything else. This "overview plus detail" pattern is the heart of most dashboards.

Plan the data flow

Good dashboards have one state (here: the selected date range) and several views that redraw from it. Write the flow down before the code:

flowchart TD
  D["rows: date, category, revenue"] --> DAY["daily totals (rollup by date)"]
  S["state: selected range [t0, t1]"] --> F["filter rows in range"]
  D --> F
  F --> CAT["rollup by category"]
  F --> K["KPIs: sum, mean, max"]
  DAY --> L["line chart + brush"]
  CAT --> B["bar chart"]
  L -->|"brush event"| S

The brush changes the state; the state redraws the other views.

Step 1: data

We generate deterministic fake data so the project needs no files: 120 days, three categories. In a real project replace this function with d3.csv(...) (Loading and Cleaning Data).

JavaScript
function makeData() {
  const cats = ["Books", "Games", "Music"];
  const start = new Date(Date.UTC(2025, 0, 1));
  const rows = [];
  for (let i = 0; i < 120; i++) {
    const date = new Date(start.getTime() + i * 864e5);
    cats.forEach((category, c) => {
      const season = 1 + 0.4 * Math.sin(i / 14 + c);
      const revenue = Math.round((40 + c * 15) * season + ((i * (7 + c) * 13) % 17));
      rows.push({ date, category, revenue });
    });
  }
  return rows;
}

Step 2: the layout

One SVG per chart keeps the code simple. A small helper creates a chart area with margins and returns the inner group, so each chart starts the same way.

Step 3: put it together

Interactive dashboard

Drag the shaded region, resize it by its edges, or click outside it to clear it and see the whole year. Everything follows.

How it works

  • One state, one update function. The brush converts its pixel selection into dates with lx.invert and calls update. All three views (bars, KPIs, caption) are written inside that function, so they cannot get out of sync.
  • brush.move sets an initial selection programmatically; this triggers the same event, so the first draw uses the same code path.
  • d3.rollup and d3.sum compute everything from the raw rows; there are no hand-written loops.
  • Keyed join with a transition animates the bars: the by domain changes with the selection, so the scale and axis are updated first, and the bars then slide to their new heights.
  • d3.greatest returns the element with the largest value (not the value), which gives us the whole best-day record.

Ideas to extend it

  1. Add a legend that toggles categories on and off by clicking (store the hidden categories in the state).
  2. Make the line chart show one line per category with d3.group and a color scale.
  3. Add a tooltip with a crosshair to the line chart (Mouse Events and Tooltips).
  4. Click a bar to filter the line chart to one category (two-way linking).
  5. Load the data from a CSV and show an error message if loading fails.
  6. Wrap each chart in a reusable function (Reusable Charts).
  7. Add keyboard support for the brush and a hidden data table (Accessibility).

Common mistakes in dashboards

  • Letting every chart keep its own copy of the filter. Keep one shared state.
  • Redrawing by deleting all elements. Use keyed joins and transitions.
  • Using different color mappings per chart for the same category; define the color scale once.
  • Forgetting an "empty selection" case (zero rows). The code above handles it with || 1 and "-".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main benefit of a single update(state) function in a dashboard?

    When the state changes there is only one place to redraw from, so views cannot disagree.
  2. 2. How does the brush selection (pixels) become a date range?

    selection is in pixel coordinates; lx.invert maps each pixel back to a date.
  3. 3. What does d3.greatest(array, accessor) return?

    Unlike d3.max, which returns the value, greatest returns the element itself.
  4. 4. Why call brush.move once at the start?

    A programmatic move fires brush events, so the other views are drawn without separate startup code.

Summary

  • A dashboard is one state plus several views redrawn from it.
  • Brushing converts a pixel range to a data range with scale.invert, which becomes the state.
  • Summaries come from rollup, sum, max and greatest; keyed joins with transitions animate changes.
  • Define color scales once and share them across views.

Next: Project: A Bar Chart Race