Published
Edited
May 21, 2021
1 fork
5 stars
Insert cell
Insert cell
chart = {
const links = communityData.links;
const nodes = communityData.nodes;
const simulation = d3.forceSimulation(nodes)
.force('link', d3.forceLink(links).id(d => d.id))
.force('charge', d3.forceManyBody())
.force("center", d3.forceCenter(dim / 2, dim / 2));
// some also like to include a centering force, I don't like it
const svg = d3.create('svg')
.attr('viewBox', [0, 0, dim, dim]);

svg.append("defs")
.append("clipPath")
.attr("id", "clip-circle")
.append("circle")
.attr("cx", imgRadius)
.attr("cy", imgRadius)
.attr("r", imgRadius);

const link = svg.append("g")
.attr("stroke", "#999")
.attr("stroke-opacity", 0.1)
.selectAll("line")
.data(links)
.join("line")
.attr("stroke-width", d => Math.sqrt(d.value));

const node = svg
.append("g")
.attr("stroke", "orange")
.attr("stroke-width", 1.5)
.selectAll("g")
.data(nodes)
.join("g")
.call(drag(simulation));
node.append("circle")
.attr("r", weightRadius)
.attr("fill", "orange")
node.append("defs")
.append("clipPath")
.attr("id", (d) => "clip-" + d.id)
.append("circle")
.attr("cx", weightRadius)
.attr("cy", weightRadius)
.attr("r", weightRadius);
node.append("title")
.text(d => d.name);

node
.append("g")
.attr("clip-path", (d) => `url(#clip-${d.id})`)
.attr("transform", (d) => {
const offset = ((d.weight / 10) + imgRadius) / 2;
return `translate(${-offset}, ${-offset})`;
})
.append("image")
.attr(
"xlink:href",
(d) => d.img
)
.attr("width", (d) => (d.weight / 10) + imgRadius);
simulation.on("tick", () => {
link
.attr("x1", d => d.source.x)
.attr("y1", d => d.source.y)
.attr("x2", d => d.target.x)
.attr("y2", d => d.target.y);

node.attr("transform", (d) => `translate(${d.x},${d.y})`);
});

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

return svg.node();
}
Insert cell
weightRadius = (d) => ((d.weight / 10) + imgRadius) / 2;
Insert cell
dim = 1000
Insert cell
imgRadius = 10
Insert cell
communityData = FileAttachment("bitclout@5.json").json();
Insert cell
Insert cell
Insert cell
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