liveBook · Manning

Coordinate system

As explained in D3.js in Action, Third Edition

A coordinate system is a reference framework that locates points using an origin and coordinate values.

A coordinate system gives SVG shapes a shared way to locate points. The SVG container’s origin is its top-left corner: x increases to the right, and y increases downward. That downward y direction is easy to forget when positioning shapes. section 1.2.2

Place a line

In `<line x1="50" y1="45" x2="140" y2="225" />`, the first endpoint is 50 units right and 45 down from the origin. The second is 140 right and 225 down. SVG shape attributes supply these positions. section 1.2.2

A chart often needs room around its plotted area for labels and legends. Rather than add the margins to every shape’s coordinates, place the chart elements in a group and translate that group by the left and top margins. The inner chart then has its own coordinate system. section 4.1.1

const innerChart = svg
  .append("g")
  .attr("transform", `translate(${margin.left}, ${margin.top})`);

The group’s `translate(${margin.left}, ${margin.top})` moves the inner chart’s coordinate system into its reserved area. section 4.1.1

How D3.js in Action, Third Edition covers it

How deep it goes
Explains the SVG origin and axis directions, then implements an inner coordinate system with a translated group.
Examples
Uses positioned SVG lines and shapes, plus a translated group for a line chart.
What it assumes
Introduces SVG positioning from the origin before using D3 to append a group and set its transform attribute.
What you can do after
Build it from scratch: position SVG shapes and offset a chart’s plotting area without adjusting every element individually.
How it connects
Connects SVG shapes, the margin convention, SVG groups, and translation.

Also explained in

Compare how 4 books explain coordinate system →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.