Skip to content

D3.js - Paths API

SVG Paths are fundamental for drawing custom shapes like rectangles, circles, ellipses, polylines, polygons, straight lines, and complex curves. An SVG <path> element uses a special mini-language in its d attribute to define the shape’s outline. This outline can then be stroked, filled, used as a clipping path, or any combination of these. This chapter explores D3’s Path API, which helps in programmatically generating these path definitions.

D3 offers two main ways to work with paths:

  1. The d3.path() serializer: A low-level tool to build path strings step-by-step, similar to the Canvas API.
  2. Path generators (e.g., d3.line(), d3.arc()): Higher-level functions within the d3-shape module that generate path strings from data. These are commonly used for charts.

To use these features, you can include the main D3 library or specific modules.

<!-- Option 1: Include the full D3 library -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<!-- Option 2: For d3.path() specifically (if not using full D3) -->
<script src="https://d3js.org/d3-path.v3.min.js"></script>
<!-- Option 3: For path generators like d3.line() (if not using full D3) -->
<script src="https://d3js.org/d3-shape.v3.min.js"></script>
<script>
// Example usage after including the library
// const path = d3.path();
// const line = d3.line();
</script>

d3.path() creates an object that can be used to build an SVG path string. Here are its primary methods:

  • d3.path() − Creates a new path serializer instance.
  • path.moveTo(x, y) − Moves the current point to the specified (x, y) coordinates without drawing a line. (Corresponds to SVG ‘M’ command).
  • path.closePath() − Closes the current subpath by drawing a straight line from the current point to the initial point of the subpath. (Corresponds to SVG ‘Z’ command).
  • path.lineTo(x, y) − Draws a straight line from the current point to the specified (x, y) coordinates. (Corresponds to SVG ‘L’ command).
  • path.quadraticCurveTo(cpx, cpy, x, y) − Draws a quadratic Bézier curve from the current point to (x, y) with (cpx, cpy) as the control point. (Corresponds to SVG ‘Q’ command).
  • path.bezierCurveTo(cpx1, cpy1, cpx2, cpy2, x, y) − Draws a cubic Bézier curve from the current point to (x, y) with (cpx1, cpy1) and (cpx2, cpy2) as control points. (Corresponds to SVG ‘C’ command).
  • path.arcTo(x1, y1, x2, y2, radius) − Draws a circular arc with the given radius, tangential to the line segment from the current point to (x1, y1) and the line segment from (x1, y1) to (x2, y2). (Corresponds to SVG ‘A’ command, though behavior is more like Canvas arcTo).
  • path.arc(x, y, radius, startAngle, endAngle[, anticlockwise]) − Draws a circular arc centered at (x, y) with the specified radius, from startAngle to endAngle. Angles are in radians. The optional anticlockwise boolean (default false) determines direction. (Corresponds to SVG ‘A’ command, constructed differently).
  • path.rect(x, y, width, height) − Creates a new rectangular subpath. It moves to (x, y), then draws lines to (x + width, y), (x + width, y + height), (x, y + height), and closes the path. (Uses SVG ‘M’ and ‘L’ commands).
  • path.toString() − Returns the string representation of the constructed path, suitable for use in an SVG d attribute.

While d3.path() is for manual path construction, d3.line() (from d3-shape) is a path generator ideal for creating line charts from data. Let’s create a simple line.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>D3.js Line Path Example</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<style>
.line-path {
fill: none;
stroke: green;
stroke-width: 2px;
}
</style>
</head>
<body>
<svg width="600" height="200">
<path class="line-path" transform="translate(50, 50)"></path>
</svg>
<script>
const data = [
{x: 0, y: 20},
{x: 50, y: 30},
{x: 100, y: 50},
{x: 200, y: 60},
{x: 300, y: 90}
];
// Create a line generator
const lineGenerator = d3.line()
.x(d => d.x) // Accessor for x coordinate
.y(d => d.y); // Accessor for y coordinate
// Generate the path string
const pathString = lineGenerator(data);
// Select the path element and set its 'd' attribute
d3.select('.line-path')
.attr('d', pathString);
</script>
</body>
</html>

When you open this HTML file in a browser, you will see a green line drawn based on the provided data points. The d3.line() generator simplifies creating complex path strings for common chart types.

Paths are the backbone of most D3.js visualizations:

  • Line charts, area charts, and streamgraphs use path generators.
  • Custom symbols in scatter plots can be defined using paths.
  • Geographic maps (GeoJSON features) are rendered as paths.
  • Complex diagrams like Sankey diagrams or chord diagrams rely heavily on paths.

For more detailed information on SVG paths, you can refer to the MDN SVG Path Tutorial. For D3 path generators, explore the d3-shape documentation.