D3实现TREE树状图

[html]  view plain  copy
 print ?
  1. <html>    
  2.   <head>    
  3.         <meta charset="utf-8">    
  4.         <title>树状图</title>    
  5. <style>  
  6.   
  7. .node circle {  
  8.   fill: #fff;  
  9.   stroke: steelblue;  
  10.   stroke-width: 1.5px;  
  11. }  
  12.   
  13. .node {  
  14.   font: 12px sans-serif;  
  15. }  
  16.   
  17. .link {  
  18.   fill: none;  
  19.   stroke: #ccc;  
  20.   stroke-width: 1.5px;  
  21. }  
  22.   
  23. </style>  
  24.   </head>   
  25. <body>  
  26. <script src="http://d3js.org/d3.v3.min.js"></script>  
  27. <script>  
  28.   
  29. var width = 500,  
  30. height = 500;  
  31.   
  32. var tree = d3.layout.tree()  
  33.     .size([width, height-200])  
  34.     .separation(function(a, b) { return (a.parent == b.parent ? 1 : 2); });  
  35.   
  36. var diagonal = d3.svg.diagonal()  
  37.     .projection(function(d) { return [d.y, d.x]; });  
  38.   
  39. var svg = d3.select("body").append("svg")  
  40.     .attr("width", width)  
  41.     .attr("height", height)  
  42.     .append("g")  
  43.     .attr("transform", "translate(40,0)");  
  44.   
  45.   
  46.   
  47. d3.json("city_tree.json", function(error, root) {  
  48.   
  49.     var nodes = tree.nodes(root);  
  50.     var links = tree.links(nodes);  
  51.       
  52.     console.log(nodes);  
  53.     console.log(links);  
  54.       
  55.     var link = svg.selectAll(".link")  
  56.       .data(links)  
  57.       .enter()  
  58.       .append("path")  
  59.       .attr("class", "link")  
  60.       .attr("d", diagonal);  
  61.       
  62.     var node = svg.selectAll(".node")  
  63.       .data(nodes)  
  64.       .enter()  
  65.       .append("g")  
  66.       .attr("class", "node")  
  67.       .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })  
  68.       
  69.     node.append("circle")  
  70.       .attr("r", 4.5);  
  71.       
  72.     node.append("text")  
  73.       .attr("dx", function(d) { return d.children ? -8 : 8; })  
  74.       .attr("dy", 3)  
  75.       .style("text-anchor", function(d) { return d.children ? "end" : "start"; })  
  76.       .text(function(d) { return d.name; });  
  77.     });  
  78.   
  79. </script>  
  80.           
  81.     </body>    
  82. </html>    

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
d3.js树状图具有交互效果的展开折叠可以通过以下步骤来实现: 1. 定义树形布局,设置节点大小和节点之间的间隔: ``` const treeLayout = d3.tree().nodeSize([height, width]); ``` 2. 加载数据,生成树形结构: ``` const root = d3.hierarchy(data); const treeData = treeLayout(root); ``` 3. 定义节点和连线的生成器: ``` const nodeGenerator = d3 .linkHorizontal() .x((d) => d.y) .y((d) => d.x); const linkGenerator = d3 .linkHorizontal() .x((d) => d.y) .y((d) => d.x); ``` 4. 绘制节点: ``` const nodes = svg .selectAll("g.node") .data(treeData.descendants()) .enter() .append("g") .attr("class", "node") .attr("transform", (d) => `translate(${d.y},${d.x})`) .on("click", (d) => { if (d.children) { d._children = d.children; d.children = null; } else { d.children = d._children; d._children = null; } update(d); }); nodes.append("circle").attr("r", 10); nodes .append("text") .attr("dx", 12) .attr("dy", 4) .text((d) => d.data.name); ``` 5. 绘制连线: ``` const links = svg .selectAll("path.link") .data(treeData.links()) .enter() .append("path") .attr("class", "link") .attr("d", linkGenerator); ``` 6. 定义更新函数,用于更新节点和连线: ``` function update(source) { const nodes = svg.selectAll("g.node").data(treeData.descendants()); const links = svg.selectAll("path.link").data(treeData.links()); nodes .enter() .append("g") .attr("class", "node") .attr("transform", (d) => `translate(${source.y0},${source.x0})`) .merge(nodes) .transition() .duration(500) .attr("transform", (d) => `translate(${d.y},${d.x})`); nodes.exit().remove(); nodes .select("circle") .attr("r", 10) .attr("class", (d) => (d._children ? "collapsed" : "")); links .enter() .append("path") .attr("class", "link") .attr("d", () => { const o = { x: source.x0, y: source.y0 }; return linkGenerator({ source: o, target: o }); }) .merge(links) .transition() .duration(500) .attr("d", linkGenerator); links.exit().remove(); treeData.descendants().forEach((d) => { d.x0 = d.x; d.y0 = d.y; }); } ``` 7. 定义CSS样式,用于控制节点的展开和折叠: ``` .collapsed { fill: white; stroke: steelblue; stroke-width: 2px; } ``` 以上是实现具有交互效果的展开折叠的树状图的基本步骤,具体的实现还需要根据实际需求进行调整和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值