Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 31 ยท Projects

Project: Sales Dashboard

Build a small sales dashboard step by step: tidy the data, compute KPI cards, draw a bar and a line chart, add a region filter and lay it all out on one page.

Time to put the pieces together. In this project you build the kind of page that shows up in almost every company: a sales dashboard. It has a row of headline numbers (KPI cards), a bar chart that compares regions, a line chart that shows revenue over time, and a filter that changes everything at once.

The real libraries (Plotly with Dash, Bokeh, Altair) cannot run on this site, so their code appears in read-only blocks. The runnable parts are stand-ins: a Python program that prepares the data and the spec (the plain data structure that describes the dashboard), and a small JavaScript renderer that turns a spec into SVG. That split is exactly how the real tools work, so what you learn here carries over. If you want the background first, read how a chart works inside and tidy data.

The plan

A dashboard answers a handful of questions at a glance. Before writing code, write them down:

  1. How much did we sell in total, and how many units?
  2. Which region sells the most?
  3. Is revenue going up or down lately?
  4. Does the picture change if I look at one region only?

Each question maps to a visual element: the first three to KPI cards, charts for comparison and trend, and the fourth to a filter. Writing the questions first stops you from adding charts nobody needs.

flowchart LR
    O["Orders<br/>(tidy table)"] --> P["Prepare<br/>filter and summarize"]
    P --> K["KPI cards"]
    P --> B["Bar chart<br/>revenue by region"]
    P --> L["Line chart<br/>revenue by month"]
    F["Region filter"] --> P

One tidy table feeds every element. The filter changes the input of the preparation step, so all pieces stay consistent.

Step 1: tidy data

Every chart in the dashboard comes from one tidy table: one row per observation (here, one region in one month), one column per variable. Our table has month, region, units and revenue. Tidy data means you never reshape per chart; you only filter and summarize. The next program builds 18 rows, computes the KPIs and the two summaries, and prints the whole dashboard spec as JSON.

Python Prepare the data and the dashboard spec

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the output from the top. Eighteen rows is three regions times six months. The KPI for June vs May is -3.3: revenue fell from 273.0 to 264.0, a drop of 3.3 percent. A good KPI card always states what it is compared with; a bare number such as "264" tells the reader nothing. Notice also that by_month has integer keys in Python but string keys after json.dumps, because JSON object keys are always strings. Real chart specs have the same quirk, so be careful when you read them back.

Step 2: the same dashboard in real libraries

With the summaries ready, the real code is short. These blocks are read-only here.

Plotly Express builds one figure per chart. You place them with Dash components or a make_subplots grid.

Python
import plotly.express as px

bar = px.bar(by_region_df, x="region", y="revenue", title="Revenue by region")
line = px.line(by_month_df, x="month", y="revenue", markers=True,
               title="Revenue by month")

Altair keeps the data and lets the chart summarize it. Its strength is that a selection can drive several charts, so the filter costs almost nothing:

Python
import altair as alt

pick = alt.selection_point(fields=["region"])

bar = (
    alt.Chart(orders)
    .mark_bar()
    .encode(
        x="region:N",
        y="sum(revenue):Q",
        opacity=alt.condition(pick, alt.value(1), alt.value(0.4)),
    )
    .add_params(pick)
)

line = (
    alt.Chart(orders)
    .mark_line(point=True)
    .encode(x="month:O", y="sum(revenue):Q")
    .transform_filter(pick)
)

dashboard = alt.hconcat(bar, line)

Click a bar and the line chart redraws for that region only. Plotly achieves the same with a Dash callback, as in the Dash lesson: a dropdown's value is the input, and the figures are the outputs. Bokeh would link the views with a shared ColumnDataSource or a CustomJS callback.

Whatever the library, the structure is the same: one data source, a filter state, and views that read both. Choose the library by where the dashboard will live (a notebook, a Dash web app, a static HTML file), not by the chart types, because all three can draw bars and lines.

Step 3: render a spec yourself

Here is the stand-in that makes the structure visible. The page below keeps the 18 orders (generated with the same formula as the Python program), applies the region filter, recomputes the KPIs, and draws the two charts as SVG. It is a tiny renderer: a few functions that take a spec-like object and return markup.

Pay attention to three design decisions, because they are the ones that make a dashboard good rather than merely busy:

  • Consistent scales. The bar chart always shows all regions, so you can still compare, and highlights the selected one. The line chart's y axis starts at zero, so a small dip does not look like a collapse.
  • Direct labels. Values sit on the bars, so the reader does not have to trace a gridline.
  • One accent color. The selected region is dark blue; everything else is lighter.
A sales dashboard with a region filter

Choose "North" and compare. The line chart now has a different shape but the same y-axis logic, the KPI cards recompute, and the bar chart keeps all three regions visible so you never lose the comparison. Pick "All" and the revenue card shows 1556.5, the number the Python program printed. That agreement matters: when two parts of a dashboard disagree, readers stop trusting both.

Step 4: layout and finishing touches

A dashboard is read, not studied. Follow a few layout rules:

  • Top to bottom, summary to detail. KPI cards first, then the comparison, then the trend.
  • One idea per chart. Give each a title that states what it shows, as the storytelling lesson explains.
  • Align and size consistently. Same margins, same fonts, same colors for the same meaning.
  • Do not hide the zero. Bars must start at zero; for lines it is acceptable not to, but say so.
  • Accessibility. Every chart has a text alternative (role="img" and aria-label above), colors have enough contrast, and the numbers are also available as text. See color and accessibility.

Before you ship, test with data you did not design for: an empty filter result, a region with a single month, a very large number. A renderer that divides by zero or prints NaN on the first odd input is not finished. The testing lesson shows how to assert on the spec instead of on pixels.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the dashboard use one tidy table for every element?

    With one source of truth, KPI cards and charts are different summaries of the same rows, so they cannot disagree.
  2. 2. What is the best way to make a KPI card useful?

    A number without context cannot be judged. A comparison such as -3.3% against last month turns it into information.
  3. 3. In the Altair example, what makes a click on a bar change the line chart?

    alt.selection_point defines a parameter. The bar chart sets it on click, and the line chart filters its rows by the same parameter.
  4. 4. Why is it useful that json.dumps turned the month keys into strings?

    JSON allows only string keys. When you read a spec back, convert numeric keys if you need numbers.

Summary

  • Start from the questions the reader should answer, then map each to a KPI card, a chart or a filter.
  • Keep one tidy table as the single source and derive every number and chart from it.
  • A spec is data; a renderer turns it into pixels. Libraries differ in syntax but not in this split.
  • Altair links views with a shared selection, Plotly with Dash callbacks, Bokeh with shared sources or CustomJS.
  • Good layout: summary first, one idea per chart, consistent scales, zero baselines for bars, text alternatives.
  • Test the edge cases (empty filter, single point) before calling a dashboard finished.

Next: Project: Trend Explorer with Linked Views