| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>Collapsible Tree Example</title>
- <style>
- .node circle {
- fill: #fff;
- stroke: steelblue;
- stroke-width: 3px;
- }
- .node text { font: 12px sans-serif; }
- .link {
- fill: none;
- stroke: #ccc;
- stroke-width: 2px;
- }
- </style>
- </head>
- <body>
- <!-- load the d3.js library -->
- <script src="http://d3js.org/d3.v3.min.js"></script>
- <script>
- var data = [
- { "name" : "csdn.blog.world.python", "parent":"csdn.blog.world" },
- { "name" : "csdn.blog.world", "parent":"null" },
- { "name" : "csdn.blog.world.python_a", "parent":"csdn.blog.world.python" },
- { "name" : "csdn.blog.world.python_b", "parent":"csdn.blog.world.python" },
- { "name" : "csdn.blog.world.python_c", "parent":"csdn.blog.world.python" },
- { "name" : "csdn.blog.world.golang", "parent":"csdn.blog.world" },
- { "name" : "csdn.blog.world.c", "parent":"csdn.blog.world" },
- { "name" : "csdn.blog.world.java", "parent":"csdn.blog.world" }
- ];
- // *********** Convert flat data into a nice tree ***************
- // create a name: node map
- var dataMap = data.reduce(function(map, node) {
- map[node.name] = node;
- return map;
- }, {});
- // create the tree array
- var treeData = [];
- data.forEach(function(node) {
- // add to parent
- var parent = dataMap[node.parent];
- if (parent) {
- // create child array if it doesn't exist
- (parent.children || (parent.children = []))
- // add node to child array
- .push(node);
- } else {
- // parent is null or missing
- treeData.push(node);
- }
- });
- // ************** Generate the tree diagram *****************
- var margin = {top: 20, right: 120, bottom: 20, left: 120},
- width = 960 - margin.right - margin.left,
- height = 500 - margin.top - margin.bottom;
- var i = 0;
- console.log(width,height);
- width=2000;
- height=1600;
- 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 + ")");
- root = treeData[0];
- update(root);
- 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; });
- // Declare the nodes…
- var node = svg.selectAll("g.node")
- .data(nodes, function(d) { return d.id || (d.id = ++i); });
- // Enter the nodes.
- var nodeEnter = node.enter().append("g")
- .attr("class", "node")
- .attr("transform", function(d) {
- return "translate(" + d.y + "," + d.x + ")"; });
- nodeEnter.append("circle")
- .attr("r", 10)
- .style("fill", "#fff");
- nodeEnter.append("text")
- .attr("x", function(d) {
- return d.children || d._children ? -13 : 13; })
- .attr("dy", ".35em")
- .attr("text-anchor", function(d) {
- return d.children || d._children ? "end" : "start"; })
- .text(function(d) { return d.name; })
- .style("fill-opacity", 1);
- // Declare the links…
- var linkEnter = svg.selectAll("path.link").append("g")
- .data(links, function(d) { return d.target.id; });
- linkEnter.enter().insert("path", "g")//在指定元素之前插入一个元素
- .attr("class", "link")
- .attr("d", diagonal)
- // 首先为每条节点连线添加标识id
- .attr("id", function(d, i){
- return "mypath" + i;
- });
- //为连线添加文字
- linkEnter.enter().append('text')
- .attr('x', 90)
- .attr('y', 20)
- .style('fill', 'green')
- .style('font-size', '10px')
- .style('font-weight', 'bold')
- .append('textPath')
- .attr({//引用路径
- 'xlink:href': function(d, i){
- return "#mypath" + i;
- }
- })
- .text(function (d, i) {
- return i + '_Service.Pay()'
- });
- }
- </script>
- </body>
- </html>
|