Unlisted
Edited
Feb 27
Insert cell
Insert cell
Insert cell
{
const chart = Plot.plot({
marks: [
Plot.barY(byState, {
x: "state",
y: "population",
sort: { x: "y", reverse: true },
opacity: 0.1
}),
Plot.dot(
byState,
Plot.pointerX({
x: "state",
y: "population",
fill: "black",
r: 5,
sort: { x: "y", reverse: true },
render(index, scales, values, dimensions, context, next) {
const g = next(index, scales, values, dimensions, context);
g.addEventListener("pointermove", (event) => {
event.stopImmediatePropagation(); // prevent click-to-stick
const i = event.target.__data__;
console.log({ i, index });
d3.select(context.ownerSVGElement)
.append("circle")
.attr("r", 10)
.attr("stroke", "red")
.attr("fill", "none")
.attr("cx", values.x[i] + scales.scales.x.bandwidth / 2)
.attr("cy", values.y[i]);
});
return g;
}
})
)
],
width
});

return chart;
}
Insert cell
Insert cell
{
const chart = Plot.plot({
marks: [
Plot.barY(byState, {
x: "state",
y: "population",
sort: { x: "y", reverse: true },
opacity: 0.1,
tip: {
anchor: "left"
}
}),
Plot.dot(
byState,
Plot.pointer({
x: "state",
y: "population",
fill: "black",
r: 5,
sort: { x: "y", reverse: true },
render(index, scales, values, dimensions, context, next) {
if (!next) return null;
const g = next(index, scales, values, dimensions, context);
if (!g) return null;
let clicked = false;
g.addEventListener("mouseenter", (event) => {
if (clicked) return;
console.log("mouseneter", index);
event.stopImmediatePropagation(); // prevent click-to-stick
d3.select(context.ownerSVGElement)
.append("circle")
.attr("r", 10)
.attr("stroke", "red")
.attr("fill", "none")
.attr("cx", values.x[index[0]] + scales.scales.x.bandwidth / 2)
.attr("cy", values.y[index[0]]);
clicked = true;
});

return g;
}
})
)
],
width
});

return chart;
}
Insert cell
Insert cell
{
const chart = Plot.plot({
marks: [
Plot.barY(byState, {
x: "state",
y: "population",
sort: { x: "y", reverse: true },
tip: true,
render(index, scales, values, dimensions, context, next) {
const g = next(index, scales, values, dimensions, context);
g.addEventListener("pointerdown", (event) => {
event.stopPropagation();
const i = event.target.__data__;
d3.select(context.ownerSVGElement)
.append("circle")
.attr("r", 5)
.attr("fill", "red")
.attr("cx", values.x[i] + scales.scales.x.bandwidth / 2)
.attr("cy", values.y2[i]);
});
return g;
}
})
],
width
});

// chart.addEventListener("pointerdown", (event) => {
// event.stopPropagation();
// const tip = chart.querySelector("[aria-label='tip']");
// console.log({ tip, data: tip.__data__ });
// });

return chart;
}
Insert cell
Insert cell
import { tidy } from "@observablehq/plot-stacked-bar-chart"
Insert cell
byState = Array.from(
d3.rollup(
tidy,
(values) => d3.sum(values, (d) => d.population), // Sum population per state
(d) => d.state // Group by state
),
([state, population]) => ({ state, population }) // Convert Map to array of objects
)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more