Published
Edited
Apr 3, 2019
2 forks
5 stars
Insert cell
Insert cell
chart = {
// Styling Params
const scale = 3
const textOffset = 10
const links = data[0].links.map(d => Object.create(d));

const nodes = data[0].nodes.map(d => Object.create(d));
const simulation = d3.forceSimulation(nodes)
.force("link", d3.forceLink(links).id(d => d.id))
.force("charge", d3.forceManyBody())
.force("x", d3.forceX())
.force("y", d3.forceY());
const svg = d3.select(DOM.svg(width, height))
.attr("viewBox", [-width / 2, -height / 2, width, height]);

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.6)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));
const node = svg.append("g")
.selectAll(".node")
.data(nodes)
.join("g")
.attr("class", "node")
.call(drag(simulation));

const circle = node.append("circle")
.attr("r", d => d.size)
.attr("fill", color)
.call(drag(simulation));


const text = node.append("text")
.text(d => d.id)
.attr("stroke", "#999");

simulation.on("tick", () => {
link
.attr("x1", d => d.source.x*scale)
.attr("y1", d => d.source.y*scale)
.attr("x2", d => d.target.x*scale)
.attr("y2", d => d.target.y*scale);
text
.attr("x", d => d.x*scale+textOffset)
.attr("y", d => d.y*scale);

circle
.attr("cx", d => d.x*scale)
.attr("cy", d => d.y*scale);
});

invalidation.then(() => simulation.stop());

return svg.node();
}
Insert cell
data = [{
"nodes": [
{"id": "Urban IoT", "group": 1, "size": 10},
{"id": "Security", "group": 1, "size": 5},
{"id": "Safety", "group": 1, "size": 5},
{"id": "Weather Services", "group": 1, "size": 5},
{"id": "Outdoor Air Quality", "group": 1, "size": 5},
{"id": "Noise Sensors", "group": 1, "size": 5},
{"id": "Urban Spatial", "group": 1, "size": 10},
{"id": "Roads", "group": 1, "size": 5},
{"id": "Building", "group": 1, "size": 5},
{"id": "Outdoor Zones", "group": 2, "size": 5},
{"id": "Vegetation", "group": 2, "size": 5},
{"id": "PJ", "group": 3, "size": 5},
{"id": "Clayton", "group": 5, "size": 5},


],
"links": [
{"source": "Security", "target": "Urban IoT", "value": 1},
{"source": "Safety", "target": "Urban IoT", "value": 8},
{"source": "Weather Services", "target": "Urban IoT", "value": 10},
{"source": "Outdoor Air Quality", "target": "Urban IoT", "value": 1},
{"source": "Noise Sensors", "target": "Urban IoT", "value": 1},
{"source": "Roads", "target": "Urban Spatial", "value": 2},
{"source": "Building", "target": "Urban Spatial", "value": 1},
{"source": "Outdoor Zones", "target": "Urban Spatial", "value": 1},
{"source": "Vegetation", "target": "Urban Spatial", "value": 1},
{"source": "PJ", "target": "Urban Spatial", "value": 1}
]
}];

Insert cell
height = 680
Insert cell
color = {
const scale = d3.scaleOrdinal(d3.schemeCategory10);
return d => scale(d.group);
}
Insert cell
drag = simulation => {
function dragstarted(d) {
if (!d3.event.active) simulation.alphaTarget(0.3).restart();
d.fx = d.x;
d.fy = d.y;
}
function dragged(d) {
d.fx = d3.event.x;
d.fy = d3.event.y;
}
function dragended(d) {
if (!d3.event.active) simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended);
}
Insert cell
d3 = require("d3@5")
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