Public
Edited
Oct 24, 2024
4 forks
22 stars
Insert cell
Insert cell
plot = Plot.dot(penguins, {x: "body_mass_g", y: "culmen_length_mm", stroke: "species"}).plot()
Insert cell
Insert cell
color = plot.scale("color")
Insert cell
Insert cell
viewof filtered = Inputs.checkbox(
new Map(color.domain.map((d, i) => [
htl.html`<div style=${{...styles, background: color.range[i]}}></div> ${d}`, d
])),
{value: color.domain, format: d => d[0]}
)
Insert cell
Insert cell
Plot.dot(
penguins.filter(({ species }) => filtered.includes(species)),
{ x: "body_mass_g", y: "culmen_length_mm", stroke: "species" }
).plot({color})
Insert cell
Insert cell
domain = ["Adelie", "Chinstrap", "Gentoo"]
Insert cell
range = ["#4e79a7", "#f28e2c", "#e15759"]
Insert cell
viewof filtered1 = Inputs.checkbox(
new Map(domain.map((d, i) => [
htl.html`<div style=${{...styles, background: range[i]}}></div> ${d}`, d
])),
{value: domain, format: d => d[0]}
)
Insert cell
Plot.dot(
penguins.filter(({ species }) => filtered1.includes(species)),
{ x: "body_mass_g", y: "culmen_length_mm", stroke: "species" }
).plot({color: {domain, range}})
Insert cell
Insert cell
viewof filtered2 = filterLegend(domain, range)
Insert cell
Plot.dot(
penguins.filter(({ species }) => filtered2.includes(species)),
{ x: "body_mass_g", y: "culmen_length_mm", stroke: "species" }
).plot({color: {domain, range}})
Insert cell
filterLegend = (domain, range) => {
const value = new Map(domain.map(d => [d, true]));
const _set = () => set(node, [...value].filter(d => d[1]).map(d => d[0]));
const oninput = e => (value.set(e.target.id, e.target.checked), _set());
const node = htl.html`<div style="font-family: var(--sans-serif); font-size: 13px; display: flex; gap: 1em;">
${domain.map((d, i) => htl.html`<div style="display: flex;">
<input type="checkbox" id="${d}" name="${d}" checked style="accent-color: ${range[i]}" oninput=${oninput}>
<label for="${d}">${d}</label>
</div>`)}
</div>`;
_set();
return node;
}
Insert cell
Insert cell
styles = ({width: "1em", height: "1em", display: "inline-block", verticalAlign: "middle"})
Insert cell
import {set} from "@observablehq/synchronized-inputs"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more