React, Vue, Svelte¶
The tags are standard custom elements: a framework renders <bar-chart> as it renders a <div>. Every setup works this way:
<source-table>for a static page;<source-api>for your backend;embedDashboardfor a sandbox, called from a component.
The code on this page was run with Vite, in React 18 and 19, Vue 3 and Svelte 5.
Install¶
npm install bi-components
Import the components once, in the entry point of the application:
import 'bi-components/components';
The file holds the components and ECharts: about 400 KB compressed. To keep it out of the first download, import it when it is needed, as a separate file:
import('bi-components/components');
The components only run in a browser. With server rendering (Next.js, Nuxt, SvelteKit), import them from code that runs in the browser only: an effect, onMounted, onMount.
React¶
import 'bi-components/components';
export function Sales({ measure }) {
return (
<>
<source-table name="orders" url="/orders.csv" date-column="date"></source-table>
<ui-filter table="orders" dimension="channel" type="segmented"></ui-filter>
<score-card title="Revenue" table="orders" measure={measure} format="eur"></score-card>
<bar-chart table="orders" measure="sum(revenue)" by="country" height="300px"></bar-chart>
</>
);
}
Write the attributes as in HTML: measure, by, breakdown-by, date-column. When a value changes, the component follows: it asks again if its query changed, and draws again otherwise.
Data from your state. Give the records to the element, through a ref:
function Teams({ teams }) {
const table = useRef(null);
useEffect(() => {
const element = table.current;
customElements.whenDefined('source-table').then(() => {
element.data = teams; // an array of records
element.load(true); // replaces the data, refreshes the components
});
}, [teams]);
return (
<>
<source-table ref={table} name="teams"></source-table>
<bar-chart table="teams" measure="sum(points)" by="team"></bar-chart>
</>
);
}
whenDefined waits for the components when they are imported later, as a separate file. With a plain import at the start, it resolves at once.
Your backend. body takes an object (React 19), or JSON text (React 18 and 19). Each change asks again. A token goes in headers, through a ref:
function Dashboard({ workspace, getToken }) {
const api = useRef(null);
useEffect(() => {
api.current.headers = () => ({ authorization: `Bearer ${getToken()}` }); // called for each request
}, [getToken]);
return (
<>
<source-api ref={api} url="/api/queries" body={JSON.stringify({ workspace })}></source-api>
<score-card measure="revenue" compare="yoy"></score-card>
<bar-chart measure="revenue" by="country"></bar-chart>
</>
);
}
TypeScript. Declare the tags once, in a .d.ts file of the project:
import type { DetailedHTMLProps, HTMLAttributes } from 'react';
type Tag = DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement> & { [attribute: string]: unknown };
declare module 'react' {
namespace JSX {
interface IntrinsicElements {
'source-table': Tag; 'source-view': Tag; 'source-api': Tag;
'score-card': Tag; 'simple-value': Tag;
'bar-chart': Tag; 'line-chart': Tag; 'pie-chart': Tag; 'doughnut-chart': Tag; 'data-table': Tag;
'ui-filter': Tag; 'date-range-filter': Tag;
}
}
}
Vue¶
Tell Vue that tags with a dash are custom elements, in vite.config.js:
import vue from '@vitejs/plugin-vue';
export default {
plugins: [vue({ template: { compilerOptions: { isCustomElement: (tag) => tag.includes('-') } } })],
};
<script setup>
import { ref, watchEffect } from 'vue';
const measure = ref('sum(revenue)');
const props = defineProps(['teams']);
const teams = ref(null);
watchEffect(() => {
if (teams.value) {
teams.value.data = props.teams;
teams.value.load(true);
}
});
</script>
<template>
<source-table name="orders" url="/orders.csv" date-column="date"></source-table>
<score-card table="orders" :measure="measure"></score-card>
<bar-chart table="orders" measure="sum(revenue)" by="country"></bar-chart>
<source-table ref="teams" name="teams"></source-table>
<bar-chart table="teams" measure="sum(points)" by="team"></bar-chart>
</template>
Svelte¶
Svelte needs no setting.
<script>
let { teams } = $props();
let measure = $state('sum(revenue)');
let table;
$effect(() => {
table.data = teams;
table.load(true);
});
</script>
<source-table name="orders" url="/orders.csv" date-column="date"></source-table>
<score-card table="orders" {measure}></score-card>
<bar-chart table="orders" measure="sum(revenue)" by="country"></bar-chart>
<source-table bind:this={table} name="teams"></source-table>
<bar-chart table="teams" measure="sum(points)" by="team"></bar-chart>
A sandboxed dashboard in a component¶
Create the frame when the component is mounted, and destroy it when it is removed. Each frame loads bi-components.js by its address: components gives the one your bundler serves, as in Install and versions.
import { useEffect, useRef } from 'react';
import { embedDashboard } from 'bi-components';
export function GeneratedDashboard({ html, resolveQuery }) {
const container = useRef(null);
useEffect(() => {
const dashboard = embedDashboard(container.current, {
html, resolveQuery,
components: new URL('bi-components/components', import.meta.url).href,
});
return () => dashboard.destroy();
}, [html]);
return <div ref={container} style={{ height: 600 }} />;
}
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import { embedDashboard } from 'bi-components';
const props = defineProps(['html', 'resolveQuery']);
const container = ref(null);
let dashboard;
onMounted(() => {
dashboard = embedDashboard(container.value, {
html: props.html, resolveQuery: props.resolveQuery,
components: new URL('bi-components/components', import.meta.url).href,
});
});
onBeforeUnmount(() => dashboard?.destroy());
</script>
<template>
<div ref="container" style="height: 600px"></div>
</template>
<script>
import { embedDashboard } from 'bi-components';
let { html, resolveQuery } = $props();
function sandbox(node) {
const dashboard = embedDashboard(node, {
html, resolveQuery,
components: new URL('bi-components/components', import.meta.url).href,
});
return { destroy: () => dashboard.destroy() };
}
</script>
<div use:sandbox style="height: 600px"></div>
To show another dashboard in the same frame, call dashboard.load(otherHtml) instead of creating a new one; dashboard.setState(...) changes the theme or the filters without reloading. See Drive the dashboard.
What to know¶
- Changed attributes are followed. A component asks again when its query changes (
measure,by,table...), and only draws again for the rest (title,height,format). Several attributes changed in one render make one query. A<ui-filter>reads its values again, a<source-table>given anotherurlloads it and refreshes its components. - One dashboard per page. All the components of a page share
window.dashboard: a click on a bar filters every component, in every framework component. A page has either<source-table>elements or one<source-api>. - Changing pages without reloading. Each
<source-api>added to the page starts a new dashboard, with an empty selection. Give filters a value at start withdefault: see In a single-page application. - Your own code can use the same dashboard:
window.dashboard.query(...),window.dashboard.filters,window.dashboard.subscribe(...). See Custom charts and scripts.