Accepted answer

One option you might consider is to use a gradient. Something like:

<svg xmlns="" width="100%"
  viewBox="0 0 100 100" preserveAspectRatio="none"> 

  <linearGradient id="g">
    <stop offset="0" stop-color="#008"/>
    <stop offset=".2" stop-color="#008"/>
    <stop offset=".2001" stop-color="#960"/>
    <stop offset=".5" stop-color="#960"/>
    <stop offset=".5001" stop-color="#800"/>
    <stop offset=".8" stop-color="#800"/>
    <stop offset=".8001" stop-color="#080"/>
    <stop offset="1" stop-color="#080"/>

  <path d="M 3 48 30 50 50 78 97 22" stroke-width="4" stroke="url(#g)" fill="none"/>

Play with it here

The svg should be simple enough to create using D3, but there are some gotcha's, for example I have seen problems on some browsers where the gradient id was not unique to the whole page.

Also note there there are ways to control how the gradient maps to your path that you can learn about here

Related Query

More Query from same tag