如何将所有后代节点和链接设置为与级别 2 祖先相同的颜色?

我有一个 d3.js 树,其后代节点接收它们的 2 级祖先节点颜色。这是从第 2 级到第 3 级工作,但在第 4 级及以上停止工作。

http://img.mukewang.com/60c32f2c0001bf5b07120505.jpg

相关代码:


var colourScale = d3.scale.ordinal()

    .domain(["MD","Professional", "Leader", "Advocate", "Clinician", "Educator", "Scholar"])

    .range(["#6695c8", "#cd3838","#d48440", "#a8ba5f", "#63b7c0", "#c97eb2", "#ccc136"]);


 nodeUpdate.select("circle")

    .attr("r", 10)

    .attr("fill-opacity","0.7")

    .attr("stroke-opacity","1")

    .style("fill", function(d) {

      return d.depth === 2 ? colourScale(d.parent.name) : colourScale(d.name);

    })

    .style("stroke", function(d) {

      return d.depth === 2 ? colourScale(d.parent.name) : colourScale(d.name);

    });


// Enter any new links at the parent's previous position.

  link.enter().insert("path", "g")

    .attr("class", "link")

    .attr("stroke-width", function(d) {

      return 1;

    })

    .attr("d", function(d) {

      var o = {

        x: source.x0,

        y: source.y0

      };

      return diagonal({

        source: o,

        target: o

      });

    })

    .attr("opacity","0.3")

    .style("stroke", function(d) {

      return d.target.depth === 2 ? colourScale(d.target.parent.name) : colourScale(d.target.name);

    });

如何将父级的所有后代设置为相同的颜色(节点和链接)?


呼唤远方
浏览 115回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript