Skip to main content
100%

Line Transition (Broken)

✓ Published0🌍 Public
Mmbostock
Last edited Jul 6, 2016
Created on Jan 19, 2012

A continuous line chart animates as new random data points, drawn from a normal distribution, are appended and old ones removed. The animation uses a standard transition on the `d` attribute, demonstrating the default string interpolation that pairs numbers sequentially. This causes the line to move vertically instead of sliding horizontally, because the paired x-coordinates remain constant across the two paths. The code uses D3 v4, including `d3.randomNormal`, `d3.scaleLinear`, and a `d3.line` generator to construct the path.

AI-generated description

This example is the first of three in the Path Transitions tutorial.

When you interpolate a path element’s “d” attribute using transition.attr, the default interpolator is typically d3.interpolateString. This interpolator is not specialized for SVG paths, and instead simply looks for numbers embedded in strings. Numbers in the start and end strings are paired and then interpolated. So, the default behavior when interpolating two paths pairs numbers like this:

M x0, y0 L x1, y1 L x2, y2 L x3, y3
   ↓   ↓    ↓   ↓    ↓   ↓    ↓   ↓
M x0, y1 L x1, y2 L x2, y3 L x3, y4

The first point ⟨x0,y0⟩ is interpolated to ⟨x0,y1⟩. Since the x-values are the same for each pair of points, only the y-values change, and the path doesn’t slide left as intended.

For the correct behavior, see the next example.

gpl-3.0 Licensed

Similar vizzes