Gradient along links in D3 Sankey diagram

前端 未结 1 343
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 08:25

Here is jsfiddle of a Sankey diagram:

\"enter

I am trying to modi

相关标签:
1条回答
  • 2020-12-08 08:51

    @VividD: Just saw your comment, but I was about done anyway. Feel free to ignore this until you've figured it out on the own, but I wanted to make sure I knew how to do it, too. Plus, it's a really common question, so good to have for reference.

    How to get a gradient positioned along a line

    With the caveat for anyone reading this later, that it will only work because the paths are almost straight lines, so a linear gradient will look half-decent -- setting a path stroke to a gradient does not make the gradient curve with the path!

    1. In initialization, create a <defs> (definitions) element in the SVG and save the selection to a variable:

      var defs = svg.append("defs");
      
    2. Define a function that will create a unique id for your gradient from a link data object. It's also a good idea to give a name to the function for determining node colour:

      function getGradID(d){return "linkGrad-" + d.source.name + d.target.name;}
      function nodeColor(d) { return d.color = color(d.name.replace(/ .*/, ""));}
      
    3. Create a selection of <linearGradient> objects within <defs> and join it to your link data, then set the stop offsets and line coordinates according to the source and target data objects.

      For your example, it probably will look fine if you just make all the gradients horizontal. Since that's conveniently the default I thought all we would have to do is tell the gradient to fit to the size of the path it is painting:

      var grads = defs.selectAll("linearGradient")
                       .data(graph.links, getLinkID);
      
      grads.enter().append("linearGradient")
                   .attr("id", getGradID)
                   .attr("gradientUnits", "objectBoundingBox"); //stretch to fit
      
      grads.html("") //erase any existing <stop> elements on update
           .append("stop")
           .attr("offset", "0%")
           .attr("stop-color", function(d){
                 return nodeColor( (d.source.x <= d.target.x)? d.source: d.target) 
                });
      
      grads.append("stop")
           .attr("offset", "100%")
           .attr("stop-color", function(d){
                 return nodeColor( (d.source.x > d.target.x)? d.source: d.target) 
                });
      

      Unfortunately, when the path is a completely straight line, its bounding box doesn't exist (no matter how wide the stroke width), and the net result is the gradient doesn't get painted.

      So I had to switch to the more general pattern, in which the gradient is positioned and angled along the line between source and target:

      grads.enter().append("linearGradient")
          .attr("id", getGradID)
          .attr("gradientUnits", "userSpaceOnUse");
      
      grads.attr("x1", function(d){return d.source.x;})
          .attr("y1", function(d){return d.source.y;})
          .attr("x2", function(d){return d.target.x;})
          .attr("y2", function(d){return d.target.y;});
      
      /* and the stops set as before */
      
    4. Of course, now that the gradient is defined based on the coordinate system instead of based on the length of the path, you have to update those coordinates whenever a node moves, so I had to wrap those positioning statements in a function that I could call in the dragmove() function.

    5. Finally, when creating your link paths, set their fill to be a CSS url() function referencing the corresponding unique gradient id derived from the data (using the pre-defined utility function):

      link.style("stroke", function(d){
          return "url(#" + getGradID(d) + ")";
      })
      

    And Voila!
    Sankey diagram with custom gradients from the data

    0 讨论(0)
提交回复
热议问题