Code

D3.js

Scatter Plot

Intermediate Scatter plot with colour-encoded groups, variable radius size, gridlines, and native <title> tooltips. d3-scale/ordinal

const color = d3.scaleOrdinal()
  .domain(['Alpha', 'Beta', 'Gamma'])
  .range(['#4f46e5', '#e11d48', '#16a34a']);

const x = d3.scaleLinear()
  .domain([0, 100]).range([0, iW]);
const y = d3.scaleLinear()
  .domain([0, 100]).range([iH, 0]);

g.selectAll('circle')
  .data(data)
  .join('circle')
  .attr('cx', d => x(d.x))
  .attr('cy', d => y(d.y))
  .attr('r',  d => d.size)        // size encoding
  .attr('fill', d => color(d.group)) // colour encoding
  .attr('opacity', 0.75)
  .attr('stroke', '#fff')
  .attr('stroke-width', 1)
  .append('title')
  .text(d => `${d.group}: (${d.x}, ${d.y})`);
Key APIs
  • d3.scaleOrdinal() — maps categorical values to colours/symbols
  • d3.randomNormal(mean, sd) — Gaussian random number generator
  • Size and colour encoding — encode extra dimensions using r and fill
  • <title> element — native browser tooltip, no JS event needed
  • opacity — reduces overplotting in dense areas