Static page¶
Setup 1 of 3
You write the dashboard, the data is a file. The browser downloads the file and queries it locally with DuckDB-WASM. There is nothing to install, nothing to build and no server to run.
Choose it when the data fits in a file a browser can reasonably download, and everyone who opens the page may read all of it: reports, documentation, public data, prototypes.
Choose another setup when people must not see the same rows, or the data is too large to download: your backend answers the queries instead. For a dashboard written by a model or by a user, see the sandbox.
Your first dashboard¶
1. Write an HTML file¶
Create my_first_dashboard.html with this content:
<source-table
name="orders"
url="https://unytics.io/bi-components/examples/data/orders.csv"
date-column="date">
</source-table>
<date-range-filter presets="all,90d,365d,ytd"></date-range-filter>
<score-card title="Revenue" table="orders" measure="sum(revenue)" format="eur"></score-card>
<bar-chart
table="orders"
measure="sum(revenue)"
by="country">
</bar-chart>
<line-chart
table="orders"
measure="sum(revenue)"
by="date_trunc('month', date)">
</line-chart>
<script src="https://unytics.io/bi-components/dist/bi-components.js" defer></script>
What each part does:
<source-table>loads a file into the database of the browser and names it.- Each component describes what it shows: a
measure(any SQL aggregate) of atable, splitbya column or an expression. - The
<script>loads BI Components, one file with no code to write. It makes the tags work; DuckDB-WASM is downloaded from jsDelivr when the first table loads. The tag can also go in the<head>.
2. Open it in a browser¶
Open the file in any recent browser. This is the same page, live:
3. Play with it¶
The dashboard is interactive without any code:
- Click a bar: the other components are filtered on that country. Click it again, or click the background of the chart, to remove the filter.
Ctrl/Cmd+ click selects several values.- The period applies to every table that declares a
date-column.
Load your data¶
<source-table> is invisible. It takes its data from an address, or from a script.
<!-- A file: Parquet, CSV or JSON, recognized by its extension -->
<source-table name="orders" url="data/orders.parquet" date-column="date"></source-table>
<!-- Choose or compute columns while loading -->
<source-table name="orders" url="orders.csv" columns="*, date_trunc('month', date) as month, revenue - cost as margin"></source-table>
<!-- An API that returns JSON; `format` says what it is when the address has no extension -->
<source-table name="stars" url="https://example.com/api/stars" format="json"></source-table>
From a script, for data you fetch or compute yourself: leave url out, give the records, then load. Calling load(true) again replaces the data and refreshes every component.
<source-table name="stars"></source-table>
<script type="module">
const table = document.querySelector('source-table[name="stars"]');
table.data = await (await fetch('https://example.com/api/stars')).json(); // an array of records
await table.load(true);
</script>
A SQL view prepares the data once for several components: a join, an aggregation, nested JSON made flat. Use its name as a table.
<source-view name="top_days">
select date, sum(revenue) as revenue from orders group by 1 order by 2 desc limit 5
</source-view>
<data-table table="top_days" by="date" measures="max(revenue)"></data-table>
Things to know:
- The browser downloads the file, so a file on another domain must allow cross-origin requests (CORS).
- Several tables can live in one page. A click filters the components of every table that has the clicked column.
- Parquet and JSON files make DuckDB-WASM download a reader from
extensions.duckdb.orgthe first time; CSV files and records pushed by a script need nothing more.
All the attributes: Data sources.
Measures are SQL¶
In this setup a measure is any DuckDB aggregate, and by any expression:
<score-card title="Customers" table="orders" measure="count(distinct customer_id)" format="int"></score-card>
<bar-chart table="orders" measure="sum(revenue) / count(*)" by="strftime(date, '%Y')" where="channel = 'Web'"></bar-chart>
<data-table table="orders" by="country" measures="sum(revenue) as Revenue, round(avg(revenue), 2) as Average" sort-by="Revenue desc"></data-table>
A trailing as Name gives the displayed name. where restricts one component; filters and the period restrict all of them. The SQL statement itself is always built by the library.
Put it anywhere¶
The page is static, so any host works: your own website or web application, GitHub Pages, Cloudflare Pages, Netlify, an S3 or Cloud Storage bucket.
The tags are standard custom elements. They also work inside:
- a Markdown site such as mkdocs (this one), Docusaurus, Hugo or Jekyll: write the tags in the Markdown file;
- a framework such as React, Vue or Svelte:
npm install bi-components, thenimport 'bi-components/components'.
To keep a page working as it is, load a fixed version of the script rather than the latest one: see Install and versions.
Go further¶
- Components: every tag and its attributes.
- Custom charts and scripts:
window.dashboardgives your scripts the same data and filters, anddashboard.sql()runs any SQL on the local database. - Theme, formats, language.
Examples of this setup:
- Sales dashboard: score cards, charts, a table, filters, a light and dark switch.
- GitHub stars: data fetched from an API by a script.
- Conference proposals: a nested JSON answer made flat by a SQL view.