Skip to content
Skills Library
Data & analytics · Building & coding

d3-bubble-chart

/d3-bubble-chart

How to build a D3.js v6 force-directed bubble chart with sector clustering, size encoding, color legends, tooltips, and interactive selection. Use this skill whenever the user wants a bubble chart, force simulation, cluster layout, or interactive SVG circles with D3.

Licence
MIT
Last repository change
02.10.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 02.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

How to build a D3.js v6 force-directed bubble chart with sector clustering, size encoding, color legends, tooltips, and interactive selection. Use this skill whenever the user wants a bubble chart, force simulation, cluster layout, or interactive SVG circles with D3.

Original author description

How to build a D3.js v6 force-directed bubble chart with sector clustering, size encoding, color legends, tooltips, and interactive selection. Use this skill whenever the user wants a bubble chart, force simulation, cluster layout, or interactive SVG circles with D3.

How it works

  1. Core Pattern
  2. Sizing Bubbles by Market Cap
  3. Sector Clustering with forceX/forceY
  4. Tick Handler
  5. Color Legend
  6. Tooltip
  7. Click Selection
  8. Key Tips
Original SKILL.md headings · 99 lines, 345 words
SKILL.md preview
---
name: d3-bubble-chart
description: >
  How to build a D3.js v6 force-directed bubble chart with sector clustering,
  size encoding, color legends, tooltips, and interactive selection.
  Use this skill whenever the user wants a bubble chart, force simulation,
  cluster layout, or interactive SVG circles with D3.
---

# D3 v6 Force-Directed Bubble Chart

## Core Pattern

```js
const simulation = d3.forceSimulation(nodes)
  .force("x", d3.forceX(d => sectorX(d.sector)).strength(0.08))
  .force("y", d3.forceY(d => sectorY(d.sector)).strength(0.08))
  .force("collide", d3.forceCollide(d => d.r + 2).strength(0.9))
  .force("charge", d3.forceManyBody().strength(-5))
  .on("tick", ticked);
```

## Sizing Bubbles by Market Cap

Use `d3.scaleSqrt` so that *area* (not radius) encodes value — this is perceptually correct:

```js
const radiusScale = d3.scaleSqrt()
  .domain([0, d3.max(data, d => d.marketCap)])
  .range([minR, maxR]);  // e.g. [8, 60]
```

ETFs (no market cap) get a fixed uniform radius (e.g. 18).

## Sector Clustering with forceX/forceY

Pre-compute sector center positions on a grid or circle, then use them as force targets:

```js
const sectors = [...new Set(data.map(d => d.sector))];
const sectorCenters = {};
sectors.forEach((s, i) => {
  const angle = (i / sectors.length) * 2 * Math.PI;
  sectorCenters[s] = {
    x: cx + Math.cos(angle) * clusterRadius,
    y: cy + Math.sin(angle) * clusterRadius
  };
});
```

Strength 0.05–0.12 creates visible clustering without forcing everything to a single point.

## Tick Handler

```js
function ticked() {
  node.attr("cx", d => d.x).attr("cy", d => d.y);
  labels.attr("x", d => d.x).attr("y", d => d.y);
}
```

## Color Legend

```js
const legend = svg.append("g").attr("transform", `translate(${legendX}, ${legendY})`);
sectors.forEach((sector, i) => {
  legend.append("circle").attr("r", 7).attr("cx", 0).attr("cy", i * 22).attr("fill", color(sector));
  legend.append("text").attr("x", 12).attr("y", i * 22 + 5).text(sector);
});
```

## Tooltip

Create a `div` tooltip with `position: absolute; pointer-events: none`:

```js
const tooltip = d3.select("body").append("div").attr("class", "tooltip");
node.on("mouseover", (event, d) => {
  tooltip.style("display", "block")
    .html(`<b>${d.ticker}</b><br>${d.name}<br>${d.sector}`)
    .style("left", event.pageX + 12 + "px")
    .style("top",  event.pageY - 28 + "px");
}).on("mousemove", (event) => {
  tooltip.style("left", event.pageX + 12 + "px")
         .style("top",  event.pageY - 28 + "px");
}).on("mouseout", () => tooltip.style("display", "none"));
```

## Click Selection

Dispatch a custom event so other components can react:

```js
node.on("click", (event, d) => {
  d3.selectAll(".bubble").classed("selected", false);
  d3.select(event.currentTarget).classed("selected", true);
  document.dispatchEvent(new CustomEvent("stockSelected", { detail: d.ticker }));
});
```

## Key Tips

- Run the simulation with `alphaDecay(0.02)` and `velocityDecay(0.3)` for smoother settling.
- Call `.stop()` then manually run ticks with `simulation.tick(300)` for static initial layout.
- Clamp node positions within the SVG viewBox to prevent overflow.
- Use `viewBox` + `preserveAspectRatio` for responsive sizing.

Versions

The database has no other skills named d3-bubble-chart.

Database snapshot: 13.11.2026 · package version 4d8fc3784802 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 3 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100d3-force-bubble-chart
    How to create a force-directed bubble chart in D3.js (v6+). Use this skill whenever the user asks to build a bubble chart, cluster bubbles by category, or use d3.forceSimulation to position SVG circles without overlap.
  • 100d3-force-bubble-chart
    Build D3.js v6 force-simulation bubble charts where bubbles are sized by a numeric metric, colored by category, and clustered by category using forceX/forceY. Use this skill whenever the user asks for bubble charts, force-layout visualizations, or clustered circle packing with…
  • 100bubble-chart-generator
    This skill generates interactive Chart.js bubble chart visualizations for priority matrices and multi-dimensional data analysis. Use this skill when users need to create scatter plots with variable bubble sizes, particularly for 2x2 priority matrices (Impact vs Effort, Risk vs…
  • 90plot-chart
    Design or critique a data visualization — chart type selection, encoding, and clarity. Use when asked "what chart should I use", "critique this visualization", or "design this chart".
  • 90ui-ux-chart
    Charts, key figures (KPIs) and reporting pages. Use when a chart, a KPI, a statistic, a report or a dashboard is planned, built or reviewed, when numbers over time, shares or comparisons are to be shown, or when colors, axes, legends or tooltips of a chart are involved; together with ui-ux.
  • 96data-chart-images
    Use when delivering a chart image without matplotlib.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.