Skip to main content
100%

Line Transition 1: (Broken)

✓ Published0🌍 Public
深深度碎片
Last edited Jul 29, 2016
Created on Jul 29, 2016

This example demonstrates a broken path transition where a smoothly moving line fails to slide horizontally as intended. Using d3 version 4, a path element with class “line” is drawn via `d3.line` and updated with `d3.transition` when new data points push and shift an array of 40 random normal values. The default `d3.interpolateString` pairs numbers in the SVG path’s “d” attribute incorrectly, causing only the y-coordinates to change while x-coordinates stay fixed. The animation runs continuously via the `tick` function, redrawing with linear easing over 500ms.

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.

forked from <a href='http://bl.ocks.org/mbostock/'>mbostock</a>'s block: <a href='http://bl.ocks.org/mbostock/1643051'>Line Transition 1: (Broken)</a>

gpl-3.0 Licensed

Similar vizzes