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 is a reference framework for locating points. In SVG, the drawing area is a two-dimensional plane with perpendicular x and y axes. The origin is the SVG container’s top-left corner: x increases to the right, while y increases downward. SVG shape attributes use these values to place visible elements. section 1.2.2

Figure 1.11 Coordinate system of the SVG container and position of an element
figure

Use the diagram to connect the top-left origin to an element’s horizontal x position and downward y position. section 1.2.2

Use coordinates through each shape’s placement attributes:

  • Set a line’s endpoints with x1, y1, x2, and y2. section 1.2.2
  • Place a rectangle by its x and y position, then set width and height separately. section 1.2.2
  • Place a circle or ellipse from its center with cx and cy; use radius values to size it. section 1.2.2
  • Position text with x and y, then use text-anchor when its alignment should follow the middle or end rather than the start. section 1.2.2

Reserve chart margins with a translated group

A chart needs room for labels, legends, and axes. Rather than adding the left and top margin to every mark’s coordinates, create a group for the inner chart and translate it once. The group establishes a new coordinate system, so coordinates inside it begin at the chart’s reserved drawing area. const innerChart = svg .append("g") .attr("transform", `translate(${margin.left}, ${margin.top})`); section 4.1.1

This grouped approach separates the full SVG container from the plot area. After the translation is in place, axes and chart marks can use coordinates relative to the inner chart while margins remain available for supporting information. section 4.1.1

How D3.js in Action, Third Edition covers it

How deep it goes
Defines SVG axis directions and applies group translation, without broader Cartesian-coordinate formalism.
Examples
Uses lines, rectangles, circles, ellipses, paths, text, and a translated inner-chart group.
What it assumes
Introduces SVG positioning from basic geometric axes while assuming an SVG container and shape attributes.
What you can do after
Build SVG graphics and chart plot areas by assigning coordinates and translating grouped elements.
How it connects
Connects SVG shapes, viewBox, SVG groups, transforms, the margin convention, axes, and line charts.

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.