Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 44 Β· Shapes and Paths

SVG Paths, d3.path, Links and Radial Shapes

Read and write the SVG path mini-language (M, L, C, Q, A, Z), build paths with d3.path, draw connecting curves with d3.linkHorizontal and linkVertical.

Every shape generator in this chapter (line, area, arc, symbol) ends the same way: it returns a string for the d attribute of a <path>. Understanding that string makes everything easier to debug, and it unlocks custom shapes that no generator covers. This lesson explains the path mini-language, shows D3's path helper, and finishes with the generators for links (curves between two points) and radial charts.

The path mini-language

A path's d attribute is a sequence of commands. Each is a letter followed by numbers. Uppercase letters use absolute coordinates; lowercase letters are relative to the current point.

Command Meaning Numbers
M x y Move to (start a new subpath) x, y
L x y Line to x, y
H x / V y Horizontal / vertical line to x or y
C x1 y1 x2 y2 x y Cubic BΓ©zier with two control points 6
S x2 y2 x y Smooth cubic (reflects the previous control point) 4
Q x1 y1 x y Quadratic BΓ©zier with one control point 4
A rx ry rot large sweep x y Elliptical arc 7
Z Close the path with a straight line to its start none

So M10,80 L50,20 L90,60 Z draws a triangle. Commas and spaces are interchangeable. d3.line() simply emits M followed by L commands, and curves emit C.

Hand-written path commands

The method path.getTotalLength() measures a path and path.getPointAtLength(n) returns the point at distance n. They power "draw the line" animations (stroke-dasharray tricks) and labels that follow a curve.

Building paths in JavaScript: d3.path

Concatenating strings is error-prone. d3.path() provides the same API as the canvas drawing context (moveTo, lineTo, bezierCurveTo, quadraticCurveTo, arc, rect, closePath) and records the commands as a path string with toString().

JavaScript
const p = d3.path();
p.moveTo(20, 20);
p.lineTo(120, 20);
p.arc(120, 70, 50, -Math.PI / 2, Math.PI / 2);  // a half circle on the right
p.lineTo(20, 120);
p.closePath();
p.toString();   // a string like "M20,20L120,20A50,50,0,0,1,120,120L20,120Z"

This is helpful for shapes with many segments (rounded rectangles, speech bubbles, custom arrows), and it keeps the same code usable on canvas, because a generator can also draw to a canvas context: line.context(ctx).

d3.path: a speech-bubble shape

When you draw trees, node-link diagrams or flow charts, you connect a source point to a target point with a smooth curve. d3.linkHorizontal() makes a cubic BΓ©zier that leaves the source horizontally and arrives at the target horizontally (left-to-right layouts). d3.linkVertical() does the same top-to-bottom, and d3.linkRadial() works with angles and radii for radial trees.

JavaScript
const link = d3.linkHorizontal()
  .source(d => d.from)     // default: d.source
  .target(d => d.to)       // default: d.target
  .x(p => p[0])            // default: p[0]
  .y(p => p[1]);           // default: p[1]

link({ source: [20, 20], target: [200, 120] });   // "M20,20C110,20,110,120,200,120"

By default a link datum is {source: [x, y], target: [x, y]}. Hierarchy layouts in the next chapter produce nodes with x and y, and you set .x(d => d.y).y(d => d.x) to orient the tree.

Horizontal and vertical links

Radial lines and areas

d3.lineRadial() and d3.areaRadial() are line and area generators in polar coordinates. Instead of x and y you provide angle (radians, clockwise from the top) and radius. They are ideal for radar charts, circular time series (12 months around a clock) and wind roses.

JavaScript
const radial = d3.lineRadial()
  .angle(d => angle(d.month))   // a band or linear scale onto [0, 2 * Math.PI]
  .radius(d => r(d.value))
  .curve(d3.curveLinearClosed); // closes the loop

Draw it inside a g translated to the center, exactly like a pie. Use d3.scaleRadial for the radius if you want area, rather than length, to be proportional to the value.

A radial line chart (radar style)

Radial generators measure the angle from 12 o'clock, like d3.arc, whereas the trigonometric Math.cos and Math.sin measure from 3 o'clock. That is why the code subtracts Math.PI / 2 when it places the spoke lines and labels by hand.

A quick checklist for shape generators

Generator Input Output Typical elements
d3.line array of points open path one path per series
d3.area array with y0, y1 closed path one path per series or layer
d3.arc object with angles ring segment one path per slice
d3.symbol type and size small shape at origin one path per mark
d3.link* source and target curve one path per edge
d3.path method calls string custom shapes

You now have the full toolbox of marks. The next chapter shows how D3 computes positions for hierarchical and network data, and feeds the same generators.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. What is the difference between 'L 50 20' and 'l 50 20' in a path?

    Uppercase commands take absolute coordinates; lowercase commands are relative to the current position.
  2. 2. What does d3.path() give you?

    d3.path offers moveTo, lineTo, bezierCurveTo and so on, and toString() returns the d attribute text.
  3. 3. What does d3.linkHorizontal() produce for { source: [x0, y0], target: [x1, y1] }?

    Horizontal links use control points at the horizontal midpoint, giving an S-shaped curve for left-to-right layouts.
  4. 4. Which pair describes a point in d3.lineRadial?

    Radial generators use angle and radius, with zero angle at 12 o'clock.

Summary

  • A path's d string is a mini-language: M, L, H, V, C, Q, A, Z; lowercase letters are relative.
  • d3.path() builds paths with canvas-style calls; getTotalLength and getPointAtLength measure them.
  • d3.linkHorizontal, linkVertical and linkRadial draw connecting curves for trees and networks.
  • d3.lineRadial and areaRadial plot angle and radius for radar and circular charts.

Next: Hierarchies and Tree Layouts