Sub topic 7
From query results to charts and dashboards
This chapter covers turning a query result into a chart and arranging charts into a Dune dashboard. It assumes you can write a DuneSQL query with GROUP BY and have met query parameters in the previous chapter. You should also know that dex.trades is a curated table with one row per swap.
How results become charts
A Dune visualization does not run SQL. It reads the result table that a query produced and maps its columns to chart elements. The query decides what data exists. The visualization only decides how to draw it. Most chart problems are really query-shape problems, so shape the result before opening the chart editor.
Each saved query can have several visualizations. A dashboard then places those visualizations as widgets.
Choosing a chart type
Pick the type from the shape of the question, not from what looks impressive.
| Question | Result shape | Chart type |
|---|---|---|
| How did one value change over time? | one date column, one numeric column | Line or area |
| How do categories compare on one day? | one category column, one numeric column | Bar |
| How is a total split over time? | date, category, numeric | Stacked bar or area |
| What is the current headline number? | one row, one numeric column | Counter |
| Which exact rows matter? | any | Table |
| How do two measures relate? | two numeric columns | Scatter |
Pie charts are available too. Use them only for a handful of slices that add up to a meaningful whole. A pie of 40 DEX projects is unreadable.
Shaping a result for a multi-series chart
Dune can draw one series per distinct value of a column. This is the "group by" option in the chart editor. The result needs a long (tidy) format: one row per date and category, not one column per category.
Suppose you chart volume per project from the parameterized query in the previous chapter. On Ethereum there can be dozens of projects, and the chart becomes a smear of thin colored lines. Collapse the tail into an Other bucket in SQL:
WITH daily AS (
SELECT
block_date,
project,
SUM(amount_usd) AS volume_usd
FROM dex.trades
WHERE blockchain = '{{blockchain}}'
AND block_date >= current_date - INTERVAL '30' DAY
GROUP BY 1, 2
),
ranked AS (
SELECT
project,
ROW_NUMBER() OVER (ORDER BY SUM(volume_usd) DESC) AS rnk
FROM daily
GROUP BY project
)
SELECT
d.block_date,
CASE WHEN r.rnk <= 5 THEN d.project ELSE 'Other' END AS project,
SUM(d.volume_usd) AS volume_usd
FROM daily AS d
JOIN ranked AS r ON d.project = r.project
GROUP BY 1, 2
ORDER BY 1, 3 DESCThe daily CTE keeps the block_date filter, so the scan stays small. The ranked CTE ranks projects by their 30-day total. The final SELECT renames every project outside the top five to Other and sums again, so each date has at most six rows.
In the chart editor, set the type to stacked bar or area, then configure it as follows:
- Set the X column to
block_date. - Set the Y column to
volume_usd. - Set "group by" to
project. - Turn on stacking. Choose percent stacking if you care about market share rather than absolute volume.
Counters and single-number results
A counter shows one cell from the first row of the result, so the query should return exactly one row. Use it for headline figures such as 24-hour volume:
SELECT
SUM(amount_usd) AS volume_24h_usd
FROM dex.trades
WHERE blockchain = '{{blockchain}}'
AND block_date >= current_date - INTERVAL '1' DAY
AND block_time >= now() - INTERVAL '24' HOURThe block_date condition lets the engine skip old partitions. The block_time condition then trims the result to a rolling 24 hours. In the counter settings you can set a title, a prefix such as $, and a number format. Compact formats such as 0.0a show 1.2b instead of 1234567890.
Formatting that prevents misreading
Small formatting choices change what readers conclude.
- Axis labels and units. Put the unit in the title, such as "Daily DEX volume (USD)". Without it, a reader cannot tell whether 3.2b means dollars or token units.
- Number formats. Use thousands separators or compact notation for large values. Avoid showing 12 decimals for dollar amounts.
- Time zone.
block_dateandblock_timeare UTC. State this in a text widget if your audience is in another time zone. - The current, partial day. A daily series that includes today ends with a bar that looks like a crash, because the day is incomplete. Either exclude today with
block_date < current_date, or label it clearly. - Log scale. A logarithmic Y axis helps when series differ by orders of magnitude. It also misleads readers who expect equal heights to mean equal amounts, so say when you use it.
- Nulls and gaps. Days with no rows do not appear in the result, so a line chart connects the neighbors across the gap. For sparse data, build a calendar of days with
UNNEST(sequence(...))and left-join to it.
Building a dashboard
A dashboard is a page of widgets. You create one from the Dune interface and add items to it. A widget is one of these:
- A visualization from one of your queries, or from someone else's public query.
- A text widget, written in Markdown, for titles, explanations, definitions, and caveats.
- A parameter widget, which lets viewers change a query's parameter.
You drag widgets to move them and resize them on a grid. A workable layout puts headline counters across the top, one or two wide time-series charts below, and detail tables at the bottom.
Parameters on a dashboard
When a query uses {{blockchain}}, the dashboard shows an input for it. Viewers can then switch chains without editing SQL. Two points matter here:
- Each query on the dashboard takes its own parameter values. If three queries all use
{{blockchain}}, check that changing the input updates all three charts and not just one. A chart that did not update shows the wrong chain and nothing warns the viewer. Use the same parameter name in every query so they behave consistently. - A text parameter is substituted as raw text into the query. Wrapping it in quotes, as in
'{{blockchain}}', makes it a string literal. A viewer who typesEthereuminstead ofethereumgets an empty chart, because the filter is case-sensitive. A list parameter, which offers fixed choices, avoids this.
Refreshing and cost
Each query run uses compute, and on paid plans that consumes credits. Dashboards show the last saved result of each query. Opening a dashboard does not necessarily rerun everything, but a refresh does, and a scheduled refresh reruns on a timer. Consider this before scheduling:
- A query that scans 30 days of
dex.tradescosts far more than a counter over 24 hours. - Refreshing every hour a chart that is only meaningful daily spends credits for no benefit.
- Heavy queries sit best behind a slower schedule, with light counters refreshed more often.
If a dashboard feels slow, fix the query first. The date filters and column pruning from the efficiency chapter reduce both run time and cost.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Chart shows one series when you expected many | No "group by" column set, or the result is in wide format | Reshape to long format and set the group column |
| Series overlap instead of stacking | Stacking is off | Turn on stacking in the Y axis options |
| X axis is sorted as text (`1, 10, 2`) | The date column was cast to `varchar` | Keep the column as `date` or `timestamp` |
| Counter shows the wrong number | The query returns several rows | Aggregate to one row |
| Empty chart after changing a parameter | Value does not match the data's case or spelling | Use a list parameter or `lower()` on both sides |
| Last bar looks like a collapse | Partial current day included | Filter `block_date < current_date` |
Trust notes for shared dashboards
A dashboard is a claim about the chain. Each widget should let a reader trace it to its source:
- Name the source table, such as
dex.trades, in a text widget. - State the window and the time zone.
- Link or fork the underlying queries so others can audit them. A forked query is a copy you can change, and the original stays intact.
The final chapter returns to these points in more detail.
Exercise
A stacked area chart of volume per project has 40 thin series, and the legend is longer than the chart. Describe the change you would make to the query, and the chart settings you would use afterward.
Show answer
Rank projects by total volume in a CTE, keep the top five names, and map every other project to the label 'Other' with a CASE expression, then sum again by date and project so each date has at most six rows. In the chart editor set X to blockdate, Y to volumeusd, group by project, and turn on stacking, using percent stacking if the goal is to show market share.
Exercise
A counter for 24-hour volume on your dashboard shows a number that is much too small, and the query returns three rows. What is wrong, and how do you fix it?
Show answer
The counter displays a cell from the first row only, so with several rows it can show a partial figure, such as one project's volume. Remove the grouping, or wrap the query in an outer SELECT SUM(...), so the result is a single row with the total.
Exercise
You switch the blockchain parameter on a dashboard from ethereum to Arbitrum and one chart goes blank while the others update. Name two causes to check.
Show answer
First, the parameter value may not match the data's spelling or case, since the filter compares text and the stored value is lowercase (arbitrum). Second, that chart's query may use a differently named parameter, so the dashboard input is not connected to it.