我想要每个节点d3的特定路径颜色

时间:2018-05-05 08:42:27

标签: html d3.js

问题是我想在树中添加不同节点颜色的每个节点,我尝试了多个解决方案,但没有结果。下面是我的数据,如何更改我的数据以将其添加到具有不同颜色的树。 这是我的Tree

var treeData = {"name" : "HTTP-GET", "children" : [
 {"name" : "  wah.comsats.edu.pk/ ", "children" : [
 {"name" : "  wah.comsats.edu.pk/AboutCIIT/Introduction.aspx","children" :[ 
 {"name" : "  wah.comsats.edu.pk/AboutCIIT/Introduction.aspx " },
 {"name" : "  wah.comsats.edu.pk/AboutCIIT/Introduction.aspx " },
 {"name" : " Embedded Stream ", "children" : [
 {"name" : "  wah.comsats.edu.pk/favicon.ico " },
 {"name" : "  wah.comsats.edu.pk/favicon.ico " }]}]},
 {"name" : "  wah.comsats.edu.pk/AboutCIIT/Introduction.aspx " }, 
 {"name" : "  wah.comsats.edu.pk/AboutCIIT/Introduction.aspx " }, 
 {"name" : " Embbeded Stream ", "children" : [
 {"name" : "  wah.comsats.edu.pk/favicon.ico " }, 
 {"name" : "  wah.comsats.edu.pk/favicon.ico " }]}]}, 
 {"name" : "  cuonline.ciitwah.edu.pk/ "} 
 ]};

以下是我可视化树的代码。我试图改变功能,但它停止了树的可视化。

    <!DOCTYPE html>
<meta charset="UTF-8">
<style>
.node circle { 
 fill: #fff;
stroke: steelblue;
stroke-width: 1px;
}
.node text {
 font: 12px sans-serif;
text-color:white;
}
.link {
 fill: none;
 stroke: #ccc;
stroke-width: 3px;
}
body{
background-image: url("gray.png");
}
</style>
<body>
<script type="text/javascript" src="lib/d3.min.js"></script>
<script>
var treeData = {"name" : "HTTP-GET", "children" : [
{"name" : " http://wah.comsats.edu.pk/ ", "children" : [
{"name" : " http://fonts.googleapis.com/css?family=Oswald " }, 
{"name" : " http://wah.comsats.edu.pk/img/acad.jpg " }, 
{"name" : " http://wah.comsats.edu.pk/js/responsiveCarousel.min.js " }, 
{"name" : " http://wah.comsats.edu.pk/slides/wahm.jpg " }, 
{"name" : " http://wah.comsats.edu.pk/slides/BITA17.jpg " }, 
{"name" : " http://wah.comsats.edu.pk/slides/alumni.jpg " }, 
{"name" : " http://wah.comsats.edu.pk/img/ann.jpg " }, 
{"name" : " http://wah.comsats.edu.pk/img/ann.jpg " }]}, 
{"name" : " https://www.google.com.pk/search?q=nsut&oq=nsut&aqs=chrome..69i57j69i60l4j69i59.613j0j7&sourceid=chrome&ie=UTF-8 "}, 
{"name" : " https://www.google.com.pk/url?sa=t&source=web&rct=j&url=http://www.nsut.com/&ved=0ahUKEwiq-q3goqbaAhXSxqQKHfLhBDIQFggmMAA "}, 
{"name" : " https://www.google.com.pk/search?q=nust&oq=nust&aqs=chrome..69i57j0l5.814j0j7&sourceid=chrome&ie=UTF-8 "}, 
{"name" : " https://pbs.twimg.com/profile_images/734623083781345280/Kb-33Gf3_normal.jpg "}, 
{"name" : " https://pbs.twimg.com/media/DaGZdo6V4AAJ6PW?format=jpg&name=medium "}, 
{"name" : " https://www.google.com.pk/search?q=patent&oq=patent&aqs=chrome..69i57j0l5.6101j0j7&sourceid=chrome&ie=UTF-8 "}, 
{"name" : " https://syndication.twitter.com/i/jot "}, 
{"name" : " https://platform.twitter.com/jot.html "}, 
]};



  var margin = {top: 20, right: 90, bottom: 30, left: 90},
 width = 1960 - margin.left - margin.right,
 height = 500 - margin.top - margin.bottom; 
  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 i = 0,
 duration = 750,
 root;
  var treemap = d3.tree().size([height, width]);
  root = d3.hierarchy(treeData, function(d) { return d.children; });
root.x0 = height / 2;
root.y0 = 0;
  root.children.forEach(collapse);

update(root);
function collapse(d) {
  if(d.children) {
    d._children = d.children
    d._children.forEach(collapse)
    d.children = null
  }
}
function update(source) {
  var treeData = treemap(root);
  var nodes = treeData.descendants(),
links = treeData.descendants().slice(1);
  nodes.forEach(function(d){ d.y = d.depth * 180});
  var node = svg.selectAll('g.node')
.data(nodes, function(d) {return d.id || (d.id = ++i); });
  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('class', 'node')
.attr('r', 1e-6)
.style("fill", function(d,i) {
return d._childern ? "lightsteelblue" : "#fff";
});
  nodeEnter.append('text')   
   .attr("dy", ".35em")   
  .attr("x", function(d) {   
      return d.children || d._children ? -13 : 13;
   })      
.attr("text-anchor", function(d) {      
    return d.children || d._children ? "end" : "start";      
})      
.text(function(d) { return d.data.name; });
  var nodeUpdate = nodeEnter.merge(node);
  nodeUpdate.transition()    
.duration(duration)    
.attr("transform", function(d) {         
return "translate(" + d.y + "," + d.x + ")";     
});
  nodeUpdate.select('circle.node')    
.attr('r', 10)    
.style("fill", function(d) {        
return d._children ? "lightsteelblue" : "#fff";    
})    
.attr('cursor', 'pointer');
  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);
  var link = svg.selectAll('path.link')      
.data(links, function(d) { return d.id; });
  var linkEnter = link.enter().insert('path', "g")      
.attr("class", "link")      
.attr('d', function(d){        
var o = {x: source.x0, y: source.y0}        
return diagonal(o, o)      
});
  var linkUpdate = linkEnter.merge(link);
  linkUpdate.transition()      
.duration(duration)      
.attr('d', function(d){ return diagonal(d, d.parent) });
  var linkExit = link.exit().transition()      
.duration(duration)      
.attr('d', function(d) {        
var o = {x: source.x, y: source.y}        
return diagonal(o, o)      
})      
.remove();
  nodes.forEach(function(d){    
d.x0 = d.x;    
d.y0 = d.y;  
});
  function diagonal(s, d) {

    path = `M ${s.y} ${s.x}
            C ${(s.y + d.y) / 2} ${s.x},
              ${(s.y + d.y) / 2} ${d.x},
              ${d.y} ${d.x}`

    return path
  }
  function click(d) {
    if (d.children) {
        d._children = d.children;
        d.children = null;
      } else {
        d.children = d._children;
        d._children = null;
      }
    update(d);
     }
    }
   </script>
   </body>

1 个答案:

答案 0 :(得分:0)

好的,这样做,

但在检查每个案例时要小心在字符串的两端使用空格。困了我一会儿,

WHERE