Dinosaur finds
Five linked charts on 21,253 dinosaur fossil finds from the Paleobiology Database: a stacked time series, a map, ranked bars, a part-of-whole bar and an area chart. Drag across the time chart or draw a box on the map to filter the other charts. Click a diet to select it, and click without dragging to clear a brush.
ts
import { bin, createDashboard, fromColumns, lngLat, top } from "@crossplot/core";
import { countDisplay } from "@crossplot/core/dom";
import { plotRenderer } from "@crossplot/plot";
import { DIET_COLORS, GROUP_COLORS, loadDinosaurs } from "./dinosaurs.js";
import { $, type MountExample } from "./theme.js";
export const mount: MountExample = async (el, theme) => {
const legend = Object.entries(GROUP_COLORS)
.map(([group, color]) => `<span><i style="background:${color}"></i>${group}</span>`)
.join("");
el.innerHTML = `
<p class="crossplot-count" data-count></p>
<div class="cp-grid">
<div class="cp-wide">
<div data-age></div>
<p class="cp-legend">${legend}</p>
</div>
<div class="cp-wide" data-map></div>
<div data-country></div>
<div>
<div data-diet></div>
<div data-reported></div>
</div>
</div>`;
const dash = await createDashboard({
data: fromColumns(await loadDinosaurs()),
engine: "local",
renderer: plotRenderer({ defaults: { color: theme.color, mutedColor: theme.muted } }),
});
// Deep time, stacked by group. Brush a stretch of it: the Late Jurassic is long-necks from
// Colorado, Portugal and Tanzania; the end of the Cretaceous is duck-bills and horned faces.
dash.chart($(el, "[data-age]"), {
dimension: [bin("age", { step: 2, extent: [64, 252] }), "group"],
view: {
title: "Finds by age, millions of years ago",
mark: "stackY",
reverse: true,
colors: GROUP_COLORS,
series: Object.keys(GROUP_COLORS),
height: 220,
xLabel: null,
yLabel: null,
},
});
// Where the finds were made, one dot per 3° cell. Drag a box to filter both axes at once.
dash.chart($(el, "[data-map]"), {
dimension: lngLat("lng", "lat", { step: 3 }),
view: { title: "Where they were found", mark: "map", radius: 9 },
});
// The ten countries with the most finds, ranked by the current selection.
dash.chart($(el, "[data-country]"), {
dimension: top("country", 10),
view: { title: "Top countries", sort: "-value", omit: ["Other"], labels: true, xLabel: null },
});
// One bar split into its parts; click a part to select it.
dash.chart($(el, "[data-diet]"), {
dimension: "diet",
view: { title: "Diet", mark: "shareX", colors: DIET_COLORS },
});
// When each find was published. Brush the last two decades and Spain, China and Argentina climb the ranking.
dash.chart($(el, "[data-reported]"), {
dimension: bin("reported", { step: 5, extent: [1820, 2025] }),
view: { title: "Finds by year published", mark: "areaY", height: 150, xLabel: null, yLabel: null },
});
const stopCount = countDisplay(dash, $(el, "[data-count]"), {
format: (s, t) => `${s.toLocaleString("en-US")} of ${t.toLocaleString("en-US")} dinosaur finds selected`,
});
return () => {
stopCount();
dash.destroy();
};
};Data loader
ts
/**
* Dinosaur fossil finds for the live examples: 21,253 records from the Paleobiology Database
* (paleobiodb.org, CC BY 4.0), the same data as the dinosaur demo with only the columns charted here.
* The JSON is one object of arrays, which `fromColumns` takes as it is.
*/
// A type alias rather than an interface: column types need to be assignable to Record<string, Value[]>.
export type Dinosaurs = {
/** When the animal lived, in millions of years ago (the midpoint of the rock's dated range). */
age: number[];
/** Broad group, such as "Theropods" or "Long-necks". */
group: string[];
/** "Plant-eater", "Meat-eater", "Omnivore" or "Unknown". */
diet: string[];
/** Country of the find. */
country: (string | null)[];
/** Year the find was published. */
reported: (number | null)[];
/** Where the find was made, in degrees. */
lng: number[];
lat: number[];
};
/** Groups in a fixed order, with one color each that reads on light and dark backgrounds. */
export const GROUP_COLORS: Record<string, string> = {
Theropods: "#d1435b",
"Long-necks": "#3b6cf0",
"Duck-bills and kin": "#13a39b",
"Horned and dome-headed": "#d39a00",
"Armored and plated": "#7a5cd6",
"Not identified": "#9a9ea8",
};
/** Diets, colored to match the groups that mostly have them. */
export const DIET_COLORS: Record<string, string> = {
"Plant-eater": "#13a39b",
"Meat-eater": "#d1435b",
Omnivore: "#d39a00",
Unknown: "#9a9ea8",
};
let cached: Promise<Dinosaurs> | undefined;
/** Fetch the columns once per page; every example shares them. */
export function loadDinosaurs(): Promise<Dinosaurs> {
cached ??= fetch(`${import.meta.env.BASE_URL}dinosaurs.json`).then((r) => {
if (!r.ok) throw new Error(`dinosaurs.json: ${r.status}`);
return r.json() as Promise<Dinosaurs>;
});
return cached;
}