Published
Edited
Jul 30, 2020
2 forks
6 stars
Also listed in…
Visualization Concepts
Insert cell
Insert cell
Insert cell
Insert cell
jitter= () => Math.random()*jitterValue
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, iwidth, height]);

svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
svg.append("g")
.attr("stroke-width", 1.5)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(data)
.join("g")
.attr("transform", d => `translate(${x(d.x)+jitter()},${y(d.y)+jitter()})`)
.call(g => g.append("circle")
.attr("stroke", "steelblue")
.attr("fill", "steelblue")
.style("opacity", "0.3")
.attr("r", r))
.call(g => g.append("text")
.attr("dy", "0.35em")
.attr("x", 7)
.text(d => d.name));

return svg.node();
}
Insert cell
md`## Option 2. Use the force
You can create a forceSimulation that tries to send the circles to their desired locations, but then avoid collisions between them`
Insert cell
Insert cell
chartForce = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, iwidth, height]);

svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
// d3.forceSimulation uses d.x and d.y, so we need to reformat the data
const dataFixed = data.map(d => ({px:d.x, py:d.y}));
console.log("before", dataFixed[0]);
const simulation = d3.forceSimulation(dataFixed)
.force("x", d3.forceX(d=> x(d.px)))
.force("y", d3.forceY(d=> y(d.py)))
.force("collide", d3.forceCollide(r + collisionR).iterations(4))
// Run the simulation for 100 steps
for(let i=0; i<100; i++)
simulation.tick();
simulation.stop();
console.log(dataFixed);

svg.append("g")
.attr("stroke-width", 1.5)
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(dataFixed)
.join("g")
.attr("transform", d => `translate(${d.x},${d.y})`)
.call(g => g.append("circle")
.attr("stroke", "steelblue")
.attr("fill", "steelblue")
.style("opacity", "0.3")
.attr("r", r))
.call(g => g.append("text")
.attr("dy", "0.35em")
.attr("x", 7)
.text(d => d.name));

return svg.node();
}
Insert cell
iwidth = width/2
Insert cell
height = iwidth*.7
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 40})
Insert cell
x = d3.scaleLinear()
.domain([0, d3.max(data, d => d.x)]).nice()
.range([margin.left, iwidth - margin.right])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.y)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x))
.call(g => g.select(".domain").remove())
.call(g => g.append("text")
.attr("x", width - margin.right)
.attr("y", -4)
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "end")
.attr("font-size", "12pt")
.text(data.x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 4)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.attr("font-size", "12pt")
.text(data.y))
Insert cell
data = d3.range(250).map(d => ({
x: Math.floor(Math.random()*5)+1,
y: Math.floor(Math.random()*5)+1,
}))
Insert cell
d3 = require("d3@5")
Insert cell
import {slider} from "@jashkenas/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