Getting started
Install
Install the core, a renderer, and the renderer's plotting library:
npm install @crossplot/core @crossplot/plot @observablehq/plotpnpm add @crossplot/core @crossplot/plot @observablehq/plotyarn add @crossplot/core @crossplot/plot @observablehq/plot@crossplot/core has no dependencies of its own. @crossplot/plot lists @observablehq/plot as a peer dependency, so you control its version.
Your first dashboard
Start with some elements to draw into:
<p id="count"></p>
<div id="hour"></div>
<div id="delay"></div>
<div id="carrier"></div>Then create a dashboard from an array of row objects and add charts:
import { bin, category, createDashboard } from "@crossplot/core";
import { countDisplay } from "@crossplot/core/dom";
import { plotRenderer } from "@crossplot/plot";
type Flight = { hour: number; delay: number; carrier: string };
const flights: Flight[] = await (await fetch("/flights.json")).json();
const dash = await createDashboard({ data: flights, renderer: plotRenderer() });
dash.chart("#hour", { dimension: bin("hour", { step: 1, extent: [0, 24] }) });
dash.chart("#delay", { dimension: bin("delay", { step: 10, extent: [-60, 180], clamp: true }) });
dash.chart("#carrier", { dimension: category("carrier") });
countDisplay(dash, "#count"); // "12,345 of 40,000 rows selected", with a reset buttonThe three charts are now linked. Drag across the hour chart and the delay and carrier charts redraw for the flights in that range. Click a carrier bar to select it.
What these calls set up:
- The rows were converted once into typed-array columns. Numbers and dates become
Float64Arrays, strings become dictionary-encoded categories. - Each chart got a dimension:
binfor numbers and dates,categoryfor strings. A chart with novaluecounts rows; see aggregates forsumandmean. - Each chart got an id, the field name by default. That id is also its key in the filter store.
- The engine started in a Web Worker. See the engine for when to run it on the main thread instead.
Field names are typed
createDashboard infers field names from your row type, so a typo is a compile error:
dash.chart("#hour", { dimension: bin("hours") });
// Error: Type '"hours"' is not assignable to type 'keyof Flight'. Did you mean '"hour"'?Bundlers and the worker
The worker engine starts with new Worker(new URL("../worker.js", import.meta.url), { type: "module" }). Vite, webpack 5 and Parcel pick that up without configuration. If yours doesn't, start the worker yourself and pass it in:
import { createDashboard, workerEngine } from "@crossplot/core";
const worker = new Worker(new URL("@crossplot/core/worker", import.meta.url), { type: "module" });
const dash = await createDashboard({ data, engine: workerEngine(worker), renderer: plotRenderer() });Next steps
- Data: row objects, columns and types.
- Dimensions and aggregates: every way to bin a field.
- Filters: reading, writing and saving filter state.
- Examples: more live dashboards to copy from.