如何更改D3.js中路径的颜色?

时间:2014-05-13 13:38:56

标签: javascript d3.js

我在D3中的树形图中创建了一些路径,我想改变线条(路径)的颜色,但我无法使代码工作:

 // Enter any new links at the parent's previous position.
      link.enter().insert("svg:path", "g")
          .attr("class", "link")
          .attr("d", function(d) {
            var o = {x: source.x0, y: source.y0};
            return diagonal({source: o, target: o});
          })
        .transition()
          .duration(duration)
          .attr("d", diagonal);

      // Transition links to their new position.
      link.transition()
          .duration(duration)
          .attr("d", diagonal);

      // Transition exiting nodes to the parent's new position.
      link.exit().transition()
          .duration(duration)
          .attr("d", function(d) {
            var o = {x: source.x, y: source.y};
            return diagonal({source: o, target: o});
          })
          .remove();

我尝试使用以下内容进行更改,并且我已经查找了其他一些解决方案,但我无法理解在更新选择中需要选择哪些内容来更改它。

  link.selectAll("path")
      .attr("stroke", "#000000");

感谢您的帮助。

2 个答案:

答案 0 :(得分:4)

我刚开始使用d3,但我认为你需要使用link.style(“stroke”,#xxxxxx)。

答案 1 :(得分:0)

svg可以通过使用fill属性来改变颜色(即使是svg的子节点,路径属性也是如此)

background-image:url('dead.beef');
background-size: 100% 100%;
background-origin:border-box;

工作代码供您参考:
https://jsfiddle.net/pjrc0yy3/1/

相关问题