Public
Edited
Apr 29, 2023
Fork of Simple D3
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
voronoi.cellPolygon(debugCell)
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
cells = {
const cells = [...voronoi.cellPolygons()].map((cell, i)=>{
return {
id: i,
centroid: centroids[i],
neighbors: [...voronoi.neighbors(i)],
area: Math.abs(d3.polygonArea(cell)),
distance: Infinity,
r: null,
// cell: cell,
}
})
const rank = {
raw: cells,
neighborRank: Array.from(cells).sort((a,b)=>d3.descending(a.neighbors.length, b.neighbors.length)),
areaRank: Array.from(cells).sort((a,b)=>d3.descending(a.area, b.area))
}

function calculateDistance(){
const queue = []
queue.push(rank.neighborRank[0])
rank.neighborRank[0].distance = 0
while (queue.length > 0){
const cell = queue.shift()
for (let n of cell.neighbors){
if (cells[n].distance > cell.distance+1){
cells[n].distance = cell.distance + 1
queue.push(cells[n])
cells[n].r = cell.id
}
}
}
rank['distanceRank'] = Array.from(cells).sort((a,b) => d3.descending(a.distance, b.distance))
}
calculateDistance()
return rank
}
Insert cell
centroids = [...voronoi.cellPolygons()].map(it=> d3.polygonCentroid(it))
Insert cell
svg = {
const svg = d3.create("svg")
.attr("width", mapSize.width)
.attr("height", mapSize.height)
.attr("viewBox", [-mapSize.width / 2 -50, -mapSize.height / 2 -50, mapSize.width +100, mapSize.height +100])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");
const [hide, show, solo] = ['Hide', 'Show', 'Solo']
const nosolo = Object.values(layers).filter(it=> it === "Solo").length === 0
const points = delaunay.points

if (layers.cells === solo || (nosolo && layers.cells === show)){
const voi = svg.append('g').selectAll('path').data(d3.range(rawPoints.length)).join('path')
.attr('fill', d=>{
if (tags[d].includes('Industry')) return 'tan'
if (tags[d].includes('Commercial')) return 'lightblue'
if (tags[d].includes('Residential')) return 'limegreen'
if (tags[d].includes('Outskirts')) return 'lightyellow'
if (tags[d].includes('Corporations')) return 'steelblue'
if (tags[d].includes('Downtown')) return 'gold'
return 'transparent'
})
.attr('d', d => voronoi.renderCell(d))
}
if (layers.debug === solo || (nosolo && layers.debug === show)){
const debugNeighbors = voronoi.neighbors(debugCell)
const debug = svg.append('g')
debug.append('path')
.attr('fill', 'darkgray')
.attr('d', voronoi.renderCell(debugCell))
debug.append('path')
.attr('fill', 'lightgray')
.attr('d', [...debugNeighbors].map(it=>voronoi.renderCell(it)).join(' '))
}
if (layers.cells === solo || (nosolo && layers.cells === show)){
const voi = svg.append('path')
.attr('stroke', 'gray')
.attr('d', voronoi.render())
}
if (layers.links === solo || (nosolo && layers.links === show)){
const lines = svg.append('g')
.selectAll('line').data(links).join('line')
.attr('stroke', 'cyan')
.attr('stroke-width', '2px')
.attr('x1', d=>points[d.source*2])
.attr('y1', d=>points[d.source*2+1])
.attr('x2', d=>points[d.target*2])
.attr('y2', d=>points[d.target*2+1])
}
// dots should be on top
if (layers.raw === solo || (nosolo && layers.raw === show)){
svg.append('g')
.attr('fill', 'gray')
.selectAll('circle').data(rawPoints).join('circle')
.attr('cx', d=>d.x)
.attr('cy', d=>d.y)
.attr('r', 2)
}
if (layers.relaxed === solo || (nosolo && layers.relaxed === show)){
const dots = svg.append('path')
.attr('d', delaunay.renderPoints())
}
if (layers.centroids === solo || (nosolo && layers.centroids === show)){
svg.append('g')
.attr('fill', 'transparent')
.attr('stroke', 'blue')
.attr('stroke-width', '1')
.selectAll('circle').data(centroids).join('circle')
.attr('cx', d=>d[0])
.attr('cy', d=>d[1])
.attr('r', 3)
// index label
svg.append('g')
.attr('text-anchor', 'middle')
.selectAll('text').data(centroids).join('text')
.attr('x', d=>d[0])
.attr('y', d=>d[1]-8)
.text((d,i)=> i+": "+ tags[i].join(', '))
}
return svg
}
Insert cell
Insert cell
relax = (type = 1) => {
// we have two options here,
// 1. iterate every points and relax it
// 2. iterate links and only relax those whose length exceeds cetain value.

function relax(delaunay, voronoi, i, o = omega) {
const points = delaunay.points;
const cell = voronoi.cellPolygon(i);
if (cell === null) return;
const center = d3.polygonCentroid(cell);
const old = [points[i * 2], points[i * 2 + 1]];
const newPos = calcRelaxedPos(old[0], old[1], center[0], center[1], o);
points[i * 2] = newPos[0];
points[i * 2 + 1] = newPos[1];
}

function calcRelaxedPos(x1, y1, x2, y2, omega) {
return [x1 + (x2 - x1) * omega, y1 + (y2 - y1) * omega];
}
function dot(x1, y1, x2, y2) {
return x1 * x2 + y1 * y2;
}
if (type === 1) {
// 1.
const points = delaunay.points;
for (let i = 0; i < n; i++) {
relax(delaunay, voronoi, i);
}
} else {
// 2.
const points = delaunay.points;
for (let i = 0; i < n; i++) {
for (let neighbor of voronoi.neighbors(i)) {
if (neighbor <= i) continue;
const [x1, y1] = [points[i * 2], points[i * 2 + 1]];
const [x2, y2] = [points[neighbor * 2], points[neighbor * 2 + 1]];
if (dot(x2 - x1, y2 - y1, x2 - x1, y2 - y1) > mindistance**2) {
continue;
}
relax(delaunay, voronoi, i, omega / 2);
relax(delaunay, voronoi, neighbor, omega / 2);
}
}
}
voronoi.update();
}
Insert cell
delaunay = d3.Delaunay.from(rawPoints, d=>d.x, d=>d.y);
Insert cell
voronoi = delaunay.voronoi([-mapSize.width/2, -mapSize.height/2, mapSize.width/2, mapSize.height/2])
Insert cell
Insert cell
mapSize = {
return {
width: 800,
height: 800
}
}
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