Custom charts and scripts¶
When no component fits, write the chart yourself. A script in a dashboard gets the same two things as the built-in components, in every setup:
window.dashboard: runs queries, holds the filters, the period and the theme;window.echarts: Apache ECharts, with a theme namedappthat follows the colors of the page.
A script written this way works unchanged in a static page, with your backend, and in a sandboxed frame, where it is how a model draws what the components cannot.
A chart of your own¶
<div id="my-chart" style="height: 300px"></div>
<script type="module">
const chart = window.echarts.init(document.getElementById('my-chart'), 'app');
async function render() {
const { rows } = await window.dashboard.query({ table: 'orders', metrics: ['sum(revenue)'], dimensions: ['country'] });
chart.setOption({
xAxis: { type: 'category', data: rows.map((row) => row.country) },
yAxis: {},
series: [{ type: 'bar', data: rows.map((row) => row['sum(revenue)']) }],
});
}
chart.on('click', (event) => window.dashboard.filters.toggle('country', event.name)); // filters the rest of the page
window.dashboard.subscribe(render); // filters, period or theme changed
render();
</script>
Write it as a <script type="module">: it runs after bi-components.js, loaded with defer, so both globals are there.
Ask for data¶
dashboard.query takes the same description a component sends, and returns { rows, meta }:
// SQL expressions on a table: a static page, or a server that accepts them
await dashboard.query({ table: 'orders', metrics: ['sum(revenue)', 'count(*)'], dimensions: ['country'], limit: 10 });
// Named measures: names your server knows (your backend, a sandbox)
await dashboard.query({ metrics: ['revenue', 'orders'], dimensions: ['country'], date_granularity: 'month' });
// A query by its name: a <source-view> of a static page, or a query your server defines
await dashboard.query('top_days');
Leave filters and date_range out and the current selection of the page is used. Unlike a tag, a script may ask for several measures at once. The fields are those of a query; meta.formats and meta.labels describe each measure when your server gives them.
In a static page only, dashboard.sql('select ...') runs any SQL on the local database and returns the rows.
Read and set the selection¶
dashboard.filters.get('country'); // 'France', ['France', 'Spain'] or undefined
dashboard.filters.set('country', ['France', 'Spain']); // null removes the filter
dashboard.filters.toggle('country', 'France'); // what a click does; { multi: true } for Ctrl / Cmd + click
dashboard.filters.clear();
dashboard.filters.getDateRange(); // ['2026-09-01', '2026-11-29'] or null
dashboard.filters.setDateRange(['2026-01-01', '2026-06-30']);
dashboard.filters.getOptions(); // { date_granularity: 'month', ... }
dashboard.filters.setOption('date_granularity', 'week');
Every component follows: a filter set by a script behaves like a click on a chart.
React to changes¶
const unsubscribe = dashboard.subscribe((change) => {
// change.type: 'filters', 'dateRange', 'options', 'variables', 'theme' or 'sourcesReady'
render();
});
After a change of theme, create the chart again to pick up the new colors: echarts.init(element, 'app'). dashboard.theme is { mode: 'light' | 'dark', palette: [...] }.
In a sandboxed frame¶
The same script, with two differences:
- Nothing can be imported. The frame cannot download a module by its address. ECharts is already there; a library you want every dashboard to have goes in the
resourcesof the Host, as a classic script. - A failure is reported to the Host, through
onRuntimeError, which is how a generated script gets corrected.
The system prompt of the playground teaches a model these few lines: see Generative AI. Every member of window.dashboard is listed in the JavaScript API.