score:0

Accepted answer

Wrap everything in a function that just has the dom-id as a parameter, then call that function multiple times. Like so (HTML):

<div id="chart1" style="height: 300px;"></div>
<div id="chart2" style="height: 300px;"></div>

JS:

$(document).ready(function() {
  var gradient = function(domId) {
    [ YOUR CODE ]
  }

  gradient("chart1")
  gradient("chart2")
});

I've put it in a plunkr for you: https://plnkr.co/edit/AD2W1Sadxwp0nuvuNIIM?p=preview

FYI: you should specify what version of d3 you're using. v3 and v4 differ quite a bit and are not interchangeable.


Related Query

More Query from same tag