Published
Edited
Apr 1, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const [svgNode, svg] = getSVG(width, height, margin, true);

const circleGroup = svg
.append("g")
.style('fill', "steelblue")
.style('opacity', 0.5)
.style('stroke', "black");

const drawCircles = () => {
const t = circleGroup.transition().duration(750);
circleGroup
.selectAll("circle")
.data(getRandomizedData(), d => d.id)
.join(
enter =>
enter
.append("circle")
.attr("r", rad)
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.style("fill", "black"),
update => {
update.call(selection =>
selection
.transition(t)
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.style("fill", d => colorScale(d.id))
);
},
exit =>
exit.style("fill", "none").call(selection =>
selection
.transition(t)
.attr("cy", height)
.remove()
)
);
};
drawCircles(getRandomizedData());

d3.select("#update_3").on("click", drawCircles);
return svgNode;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable feedback = []
Insert cell
width = 600
Insert cell
height = 300
Insert cell
margin = 20
Insert cell
rad = 10
Insert cell
W = width - 2 * margin
Insert cell
H = height - 2 * margin
Insert cell
chars = "abcdefghij".split("");
Insert cell
colorScale = d3
.scaleOrdinal()
.domain(chars)
.range(d3.schemeCategory10)
Insert cell
getRandomizedData = () => {
const N = Math.ceil(Math.random() * 10);
const step = W / N;
const randomChars = d3.shuffle(chars.map(d => d)); //.sort(d3.ascending);
const data = d3.range(N).map((d, i) => ({
x: (i * W) / N,
y: Math.random() * H,
id: randomChars[i]
}));
mutable feedback = data.map(d => d.id);
return data;
}
Insert cell
md `### External Libraries and Imports`
Insert cell
import {getSVG} from "@spattana/topojson-and-geovisualization"
Insert cell
import {Button, Checkbox, Toggle, Radio, Range, Select, Text, Textarea, Search, Table} from "@observablehq/inputs"
Insert cell
import {columns} from "@bcardiff/observable-columns"
Insert cell
d3 = require("d3@6")
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