All examples By author By category About

heungseok

Gradient Along Stroke by value

This example shows how to create a gradient that follows a stroke by z-value. There are two main techniques for creating a gradient on a line or path in D3; by using gradient units which defined as userSpaceOnUse (d3 gradient encoding example), or by sampling path uniformly (d3 gradient along stroke example). Somtimes, however, you may have to use z-value to represent the color gradient for each line poisition value.

To start, get the SVG path element and use getPointAtLength. The precesion for sampling is calculated to match the length of the data, and pass the value according to the index value to the initiated color function to create the gradient.

The code was inspired from d3 gradient along stroke example of Mike Bostock