D3图表 - 向路径添加笔划

时间:2018-04-16 11:33:48

标签: javascript d3.js

我有这段代码:

path = group.append("svg:path")
  .attr("d", arc)
  .style("fill", function(d) {
    if (d.children)
      return color(d.name)
    else
      return "grey"
  })
  .attr("display", function(d) {
    return d.depth ? null : "none";
  })
  .each(stash);

但是我想不仅要在样式中添加“填充”,还要添加一个笔画,特别是这段代码:

 .style("fill", function(d) {
    if (d.children)
      return color(d.name)
    else
      return "grey"
  }) 

有人可以告诉我如何为此添加笔画吗?我无法在CSS中执行此操作,因为它需要特定于此特定路径,并且它没有id或类。

1 个答案:

答案 0 :(得分:1)

您可以将多个styleattr个电话组合在一起(就像您的示例中的filldisplay一样)这样做可以达到您的目的寻找?

path = group.append("svg:path")
  .attr("d", arc)
  .style("fill", function(d) {
    if (d.children)
      return color(d.name);
    else
      return "grey";
  })
  .attr("stroke", function(d) {
    if (d.children)
      return "red";
  })
  .attr("stroke-width", function(d) {
     if (d.children)
       return 2;
     else 
       return 0;
  })
  .attr("display", function(d) {
    return d.depth ? null : "none";
  })
  .each(stash);

似乎根据d.children的结果应用类会更清晰。