D3 /如果孩子状态为红色或问题,则更改父母的颜色

时间:2016-02-24 10:16:43

标签: javascript jquery d3.js

这是jsfiddle的链接。这不会显示在我的代码上,但我创建的内容在我的最终工作正常,但无法在jsfiddle上复制。如果孩子身份是红色或有问题,我想要的是什么。父母的颜色应该是红色,直到我们打开它给有问题的孩子

如果"检查9"有问题而不是"测试"应该是红色的文本和节点红色点击它应该恢复正常,下一个是检查1应该是红色,如果打开比检查2等等,直到我到达问题孩子,检查9为演示

var margin = {top: 20, right: 120, bottom: 20, left: 120},
        width = window.innerWidth - margin.right - margin.left - 20,
        height = window.innerHeight - margin.top - margin.bottom - 120;
    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)
        .call(d3.behavior.zoom().scaleExtent([0.5, 2]).on("zoom", function () {
         svg.attr("transform", "translate(" + d3.event.translate + ")" + " scale(" + d3.event.scale + ")")}))
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    var flare = {
 "name": "Test",
 "children": [
  {
   "name": "head 1",
   "children": [
    {
     "name": "head 2",
     "children": [
      {
          "name": "head 3",
          "children": [
          { 
                "name": "head 4",
                "children": [
              { 
                    "name": "head 5",
                    "children": [
                  { 
                        "name": "head 6",
                        "children": [
                      { 
                            "name": "head 7",
                            "children": [
                          { 
                                "name": "head 8",
                                "children": [
                              { 
                                "name": "head 9",
                                "children": [
                                 {"name": "gi"},
                                 {"name": "hi"}
                             ]
                              }
                             ]
                          }
                         ]
                      }
                     ]
                  }
                 ]
              }
             ]    
          },
              { 
                "name": "head1",
                "children": [
              { 
                    "name": "head 2",
                    "children": [
                  { 
                        "name": "head 3",
                        "children": [
                      { 
                            "name": "head 4",
                            "children": [
                          { 
                                "name": "head 5",
                                "children": [
                              { 
                                "name": "head 6",
                                "children": [
                                 {"name": "hi"},
                                 {"name": "hi"}
                             ]
                              }
                             ]
                          }
                         ]
                      }
                     ]
                  }
                 ]
              }
             ]    
          },
              { 
                "name": "head 1",
                "children": [
              { 
                    "name": "head 2",
                    "children": [
                  { 
                        "name": "head 3",
                        "children": [
                      { 
                            "name": "head 4",
                            "children": [
                          { 
                                "name": "head 5",
                                "children": [
                              { 
                                "name": "head 6",
                                "children": [
                                 {"name": "hi"},
                                 {"name": "hi"}
                             ]
                              },
                                    {   
                                "name": "R",
                                "children": [
                                 {"name": "hi"},
                                 {"name": "hi"}
                             ]
                              },
                                    {   
                                "name": "rr",
                                "children": [
                                 {"name": "hi"},
                                 {"name": "hi"}
                             ]
                              }
                             ]
                          }
                         ]
                      }
                     ]
                  }
                 ]
              }
             ]    
          }
         ]
      }
     ]    
      }
     ]
    },
       {
   "name": "check 1",
   "children": [
    {
     "name": "check 2",
            "status":"red",
     "children": [
      {
          "name": "check 3",
          "children": [
          { 
                "name": "check 4",
                "children": [
              { 
                    "name": "check 5",
                    "children": [
                  { 
                        "name": "check 6",
                        "children": [
                      { 
                            "name": "check 7",
                            "children": [
                          { 
                                "name": "check 8",
                                "children": [
                              { 
                                "name": "check 9",
                "status":"red",
                                "children": [
                                    {"name": "problem","url":"www.google.com"},
                                    {"name": "hi","url":"www.google.com"}
                             ]
                              }
                             ]
                          }
                         ]
                      }
                     ]
                  }
                 ]
              }
             ]    
          }
         ]
      }
     ]    
      }
     ]
    }
    ]
    };
      //if (error) throw error;

      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('contextmenu',d3.contextMenu(content))
          .on("click", click);

      nodeEnter.append("circle")
          .attr("r", 1e-6)
          .style("fill", function(d) { return d._children ? (d.status ? d.status : "#f77a03") : "#fff"; });//changed

      nodeEnter.append("text")
          .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
          .attr("dy", function(d) { return d.children || d._children ? -10 : 10; })
          .style('fill',function(d) { return d.status ? d.status : "white"; })//added
          .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 ? (d.status ? d.status : "#f77a03") : "#fff"; });//changed

      nodeUpdate.select("text")
          .style('fill',function(d) { return d.status ? d.status : "black"; })//added
          .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;
      });
    }
    //expand and collapse
    function expand(d){   
        var children = (d.children)?d.children:d._children;
        if (d._children) {        
            d.children = d._children;
            d._children = null;       
        }
        if(children)
          children.forEach(expand);
    }

    function expandAll(){
        expand(root); 
        update(root);
    }

    function collapseAll(){
        root.children.forEach(collapse);
        collapse(root);
        update(root);
    }
    // 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);
    }
    //on right click
    var content = [
                {
                    title: 'Item #1',
                    action: function(elm, d, i) {
                        window.open(d.url, '_blank');
                    }
                },
                {
                    title: 'Item #2',
                    action: function(elm, d, i) {
                        window.open(d.url, '_blank');
                    }
                }
            ]

不希望节点更改它的位置

假设这是json的一部分,节点"检查9"有状态"红色"。但是当我们加载页面时,我们无法看到它直到我们展开全部或达到那一点,所以我想要的是在页面加载时使根节点的颜色变红所以我会知道那里有一些问题(某个节点状态为红色)。然后,当我点击根节点或最可见的子节点时,在这种情况下"检查2"在页面加载时会显示红色,当我点击它时,由于它没有自己的状态,它将更改为默认颜色和"检查3"将变为红色等等,直到达到"检查9"当状态为红色时,它将保持红色。只有在状态为红色而不是任何其他情况下才会执行此操作。如果仍然不清楚我真的需要加入句子形成类:(

"name": "check 1",
   "children": [
    {
     "name": "check 2",
     "children": [
      {
          "name": "check 3",
          "children": [
          {
                "name": "check 9",
                "status":"red",
                "children": [
                 {"name": "problem","url":"www.google.com"},
                 {"name": "hi","url":"www.google.com"}
                             ]
                              }
                             ]
                          }
                         ]
                      }
                     ]

1 个答案:

答案 0 :(得分:2)

由于你没有得到孩子的身份,我没有正确地测试过这个,但是这样的事情会起作用:

nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function(d) {
    console.log('children')
    console.log(d.children)
    if (d.children && d.children.length > 0) {
      var isRed = false;
      for (i = 0; i < d.children.length; i++) {
        if (d.children[i].status == 'red' || d.children[i].status == 'problem') {
          isRed = true; //set to true if any are 'red' or 'problem'
          return 'red'
        }
      }
      if (isRed == false) { //check if any are 'red' or 'problem'
        return 'green' //if not, colour green
      }

    }
  } else {
    return 'white'; //no children
  }
  //return d._children ? (d.status ? d.status : "#f77a03") : "#fff";
}); //changed

基本上在该节点的“填充”中,如果它有子节点,如果其中任何一个状态为“红色”或“问题”返回红色,则通过每个子节点,否则变为绿色,如果没有子节点颜色白:)

您的代码在小提琴中,以便您自己可以看到它:https://jsfiddle.net/reko91/r3vtvs74/4/