Skip to content

D3.js Cheatsheet

Data-Driven Documents — bind data to DOM elements with SVG.

01

Getting Started

Select

Select DOM elements and chain operations.

d3
d3.select("body")
  .append("p")
  .text("Hello D3!");

Data Binding

Data join pattern: select, data, enter, append.

d3
const data = [10, 20, 30, 40];

d3.select("svg")
  .selectAll("rect")
  .data(data)
  .join("rect")
  .attr("height", d => d)
  .attr("width", 20);

Scale

Scales map data domain to visual range.

d3
const scale = d3.scaleLinear()
  .domain([0, 100])
  .range([0, 500]);

scale(50); // 250
02

Charts

Axis

Axis components for chart axes.

d3
const axis = d3.axisBottom(scale);
d3.select("svg")
  .append("g")
  .attr("transform", "translate(0,200)")
  .call(axis);

Line Chart

Line generator for line charts.

d3
const line = d3.line()
  .x(d => xScale(d.x))
  .y(d => yScale(d.y));

d3.select("svg")
  .append("path")
  .datum(data)
  .attr("d", line);

Was this helpful?