Published
Edited
Jul 17, 2019
1 fork
Insert cell
md`# Colorforms`
Insert cell
{
const svg = DOM.svg(width, height);
const svgSelection = d3.select(svg).append('g');
let data = generateNodes();
var simulation = d3.forceSimulation()
.force("collide", d3.forceCollide(17))
.force("center", d3.forceCenter(width / 2, height / 2))
.nodes(data)
.on("tick", tick);
svgSelection.selectAll(".nodes")
.data(data, function(d,i) {
return i;
}).enter()
.append("circle")
.attr("class", "node")
.attr("r", (d,i)=> i*0.2)
.attr("fill", (d,i) => colors(i));
return svg;
}
Insert cell
function generateNodes() {
let nodes = [];
for (let i=0; i<size; i++){
nodes.push({})
}
return nodes;
}
Insert cell
colors = d3.scaleSequential().domain([0,size])
.interpolator(d3.interpolatePuRd);
Insert cell
function tick() {
d3.selectAll(".node")
.attr("cx", function(d) {
return d.x
})
.attr("cy", function(d) {
return d.y
})
}
Insert cell
size = 200
Insert cell
width = 600
Insert cell
height = 600
Insert cell
d3 = require("d3")
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