沿着Sankey图中的链接的渐变 [英] Gradient along links in D3 Sankey diagram

查看:354
本文介绍了沿着Sankey图中的链接的渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

,其中绘制了这个渐变填充循环:



>



但是,我根本无法将该解决方案集成到我的应用中,对于给定的任务看起来太复杂了。



原始Sankey图在节点被拖动时移动,并且即使在那些短暂状态下也必须显示梯度。一个小问题也是链接和节点的透明度,以及绘图的顺序。

解决方案

@VividD:刚刚看到你的评论,但我还是做了。随意忽略这一点,直到你自己想出来,但我想确保知道如何做到了。



如何获取沿着一条线定位的渐变




随后阅读这篇文章的任何人都知道,它只会工作,因为路径几乎是直线,所以线性渐变会看起来半点 - 将渐变设置为渐变不会使渐变曲线与路径一致





  1. 在初始化时,在SVG中创建一个< defs> 对变量的选择:

      var defs = svg.append(defs); 


  2. 定义一个函数,渐变从链接数据对象。这也是一个好主意,给函数确定节点颜色的名称:

      function getGradID(d){returnlinkGrad - + d.source.name + d.target.name;} 
    function nodeColor(d){return d.color = color(d.name.replace(/。* /,)


  3. 创建< defs>中的/TR/SVG/pservers.html#LinearGradientsrel =noreferrer> < linearGradient> 并将其加入您的链接数据,然后根据源数据对象和目标数据对象设置停止位移和线坐标。



    ,它可能会看起来很好,如果你只是使所有的梯度水平。因为这是方便的默认,我认为我们要做的是告诉渐变适合它正在绘制的路径的大小:

      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>元素更新
    .append(stop)
    .attr(offset,0%)
    .attr(stop-color,function(d){
    return nodeColor((d.source.x <= d.target.x)?dsource:d.target)
    });

    grads.append(stop)
    .attr(offset,100%)
    .attr(stop-color,function
    return nodeColor((d.source.x> d.target.x)?dsource:d.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;});

    / *和以前设置的止损* /


  4. 当然,现在渐变是基于坐标系而不是基于路径的长度来定义的,你必须在节点移动时更新这些坐标,所以我不得不将这些定位语句包装在一个我可以调用的函数中在 dragmove()函数中。


  5. 最后,创建链接路径时,是引用从数据导出的相应唯一渐变ID(使用预定义的效用函数)的CSS url()

      link.style(stroke,function(d){
    returnurl(#+ getGradID(d)+);
    })


http://jsfiddle.net/CeAZQ/4/rel =noreferrer> Voila!


Here is jsfiddle of a Sankey diagram:

I am trying to modify colors of the links so that the color of each link is actually gradient from its source node color to its target node color. (it is assumed that opacity will remain 0.2 or 0.5 depending whether a mouse hovers or not over the link; so links will remain a little "paler" than nodes)

I took a look at this nice and instructive example, which draws this gradient filled loop:

However, I simply couldn't integrate that solution to mine, it looks too complex for the given task.

Also, note that links in original Sankey diagram move while node is being dragged, and must display gradient even in those transitory states. A slight problem is also transparency of links and nodes, and order of drawing. I would appreciate ideas, hints.

解决方案

@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图中的链接的渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆