D3 force clustering
Techniques for creating clustered bubble charts using D3 force simulations (v6).From its SKILL.md
npx -y skills add cxcscmu/SkillLearnBench --skill d3-force-clusteringAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
SKILL.md
1.5 KB, 384 tokens by cl100k_base, as published. Nobody here has run it
D3.js Force Simulation for Clustered Bubble Charts
D3's force simulation is essential for creating bubble charts where elements are grouped by categories (clusters) and prevented from overlapping.
Key Forces
- forceSimulation: The engine that updates positions.
- forceX / forceY: Attracts nodes to specific coordinates. Use this to create clusters by mapping categories to center points.
- forceCollide: Prevents nodes from overlapping by specifying a radius.
- forceCenter: Keeps the entire group of nodes centered in the SVG.
Usage Pattern
const simulation = d3.forceSimulation(data)
.force("x", d3.forceX(d => clusterCenters[d.sector].x).strength(0.1))
.force("y", d3.forceY(d => clusterCenters[d.sector].y).strength(0.1))
.force("collide", d3.forceCollide(d => radiusScale(d.value) + 2))
.force("center", d3.forceCenter(width / 2, height / 2))
.on("tick", ticked);
function ticked() {
nodes
.attr("cx", d => d.x)
.attr("cy", d => d.y);
labels
.attr("x", d => d.x)
.attr("y", d => d.y);
}
Clustering Strategy
To group nodes by a "Sector" attribute:
- Define a set of center points for each sector (e.g., arranged in a grid or circle).
- Apply
forceXandforceYtargeting those centers. - Use a moderate
strength(e.g., 0.1) to allow collision force to resolve overlaps while maintaining the cluster shape.
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.