【问题标题】:Filling and Styling D3 Tree nodes based off of data [closed]基于数据填充和样式化 D3 树节点 [关闭]
【发布时间】:2016-10-05 07:05:44
【问题描述】:

坚持在

中找到可折叠树示例的样式

D3 代码:https://bl.ocks.org/mbostock/4339083

JSON:https://bl.ocks.org/mbostock/4339083 (但不是 size,而是使用 number 设置为某个单个数字整数)

到目前为止,我已经修改了 index.html:

nodeEnter.append("circle")
      .attr("r", 1e-6)

*.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });*

.style("stroke", function(d) { return d.status > 0 ? "green" : "red";})

我希望节点在 number 数据大于 1 的情况下填充红色,如果为零则填充绿色。

最重要的是,如果某种善良的灵魂也可以

  1. 接受第三个条件(即负数)
  2. 指导我如何根据子节点填充祖先节点的颜色

我已经彻底搜索了我所有的在线资源,因此非常感谢任何想法。

【问题讨论】:

  • the number datum equals something greater than 1 是什么意思...如果你添加你的 json 对象会很好,这样我们就可以看到你在说什么

标签: javascript html d3.js tree


【解决方案1】:

我不确定您真正需要什么,但我认为您向 json 对象添加了属性 status,并根据其值为相应元素赋予不同的圆圈颜色。

  1. 接受第三个条件(即负数)

在添加圈子时,您可以将每个圈子的样式设置如下:

如果项目的状态大于 1,则将 stroke 属性设置为红色,否则如果等于 0,则设置为绿色,否则(如果为负数)设置为橙色。

nodeEnter.append("circle")
         .attr("r", 1e-6)
         .style("stroke",
              function(d) { 
                 /*return color according to the current data's status value    */
                 /*it is > 1 than red else if < 0 than orange else(if 0)than red */   
                  return d.status >= 1 ? 
                         "red" 
                         :( d.status < 0 ? "orange" : "green")
              }) ;
  1. 指导我如何根据子节点填充祖先节点的颜色

我想你想将每个有孩子的圈子的fill 属性设置为与stroke 属性相同的颜色。

因此,在循环创建的圈子时,您必须检查当前项目是否有子项,如果是这样,请将fill 属性设置为与stroke 属性相同的颜色,如下所示:

nodeUpdate.select("circle")
          .attr("r", 4.5)
          .style("fill",
                 function(d) { 
                     /*check if children proprety exists for the current if yes 
                       get its stoke color and set fill proprety it this value */
                    return d._children ? d3.select(this).style('stroke') : "" ;
                 }) ;

完整代码:

var margin = {top: 20, right: 120, bottom: 20, left: 120},
    width = 960 - margin.right - margin.left,
    height = 800 - margin.top - margin.bottom;

var i = 0,
    duration = 750,
    root;

var tree = d3.layout.tree()
    .size([height, width]);

var diagonal = d3.svg.diagonal()
    .projection(function(d) { return [d.y, d.x]; });

var svg = d3.select("body").append("svg")
    .attr("width", width + margin.right + margin.left)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

   var flare ={
              "name": "positive with children",
              "status":3,
              "children": [
              {
                "name": "zero with children",
                "status":0,
                 "children": [
                   {
                    "name": "positive with children",
                    "status":1,
                    "children": [
                      {"name": "positive with no children", "size": 3938,"status":3},
                      {"name": "negative with no children", "size": 3812,"status":-1},
                      {"name": "positive with no children", "size": 6714,"status":1}
                    ]
                   },
                   {
                     "name": "negative with children",
                     "status":-2,
                     "children": [
                       {"name": "positive with no children", "size": 3534,"status":1},
                       {"name": "positive with no children", "size": 5731,"status":6}
      
                     ]
                   }
                  ]
                },
                {
                  "name":"postive with children",
                  "status":1,
                   "children": [
                   {"name": "negative with no children", "size": 599,"status":-3},
                   {"name": "zero with no children", "size": 386,"status":0},
                   {"name": "positive with no children", "size": 323,"status":3},
                   {"name": "zero with no children", "size": 307,"status":0}
                   ]
                },
                 {
                  "name":"negative with children",
                  "status":-3,
                   "children": [
                   {"name": "positive with no children", "size": 335,"status":1},
    {"name": "negative with no children", "size": 383,"status":-1},
    {"name": "zero with no children", "size": 874,"status":0},
    {
     "name": "positive with children",
     "status":1,
     "children": [
      {"name": "positive with no children", "size": 3165,"status":1},
      {"name": "zero with no children", "size": 2815,"status":0},
      {"name": "negative with no children", "size": 3366,"status":-2}
     ]
    }
                   ]
                }
            ]
      };
  root = flare;
  root.x0 = height / 2;
  root.y0 = 0;

  function collapse(d) {
    if (d.children) {
      d._children = d.children;
      d._children.forEach(collapse);
      d.children = null;
    }
  }

  root.children.forEach(collapse);
  update(root);

d3.select(self.frameElement).style("height", "800px");

function update(source) {

  // Compute the new tree layout.
  var nodes = tree.nodes(root).reverse(),
      links = tree.links(nodes);

  // Normalize for fixed-depth.
  nodes.forEach(function(d) { d.y = d.depth * 180; });

  // Update the nodes…
  var node = svg.selectAll("g.node")
      .data(nodes, function(d) { return d.id || (d.id = ++i); });

  // Enter any new nodes at the parent's previous position.
  var nodeEnter = node.enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
      .on("click", click);

       nodeEnter.append("circle")
              .attr("r", 1e-6)
              .style("stroke", function(d) { return d.status >= 1 ? "red" : ( d.status < 0 ? "orange" : "green")  }) ;
      //.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });

  nodeEnter.append("text")
      .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
      .attr("dy", ".35em")
      .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
      .text(function(d) { return d.name; })
      .style("fill-opacity", 1e-6);

  // Transition nodes to their new position.
  var nodeUpdate = node.transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });

  nodeUpdate.select("circle")
      .attr("r", 4.5)
      //.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
       .style("fill", function(d) { return d._children ? d3.select(this).style('stroke') : "";}) ;
  nodeUpdate.select("text")
      .style("fill-opacity", 1);

  // Transition exiting nodes to the parent's new position.
  var nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
      .remove();

  nodeExit.select("circle")
      .attr("r", 1e-6);

  nodeExit.select("text")
      .style("fill-opacity", 1e-6);

  // Update the links…
  var link = svg.selectAll("path.link")
      .data(links, function(d) { return d.target.id; });

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

  // 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();

  // Stash the old positions for transition.
  nodes.forEach(function(d) {
    d.x0 = d.x;
    d.y0 = d.y;
  });
}

// Toggle children on click.
function click(d) {
  if (d.children) {
    d._children = d.children;
    d.children = null;
  } else {
    d.children = d._children;
    d._children = null;
  }
  update(d);
}
.node {
  cursor: pointer;
}

.node circle {
  fill: #fff;
  stroke-width: 1.5px;
}

.node text {
  font: 10px sans-serif;
}

.link {
  fill: none;
  stroke: #ccc;
  stroke-width: 1.5px;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.0/d3.min.js"&gt;&lt;/script&gt;

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 2017-03-15
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多