d3tree.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
  2. function drawTree(data, containerID, root_name, unique=true, _width=960, _height=500, depthMultiplier=180) {
  3. function getOrCreateNode(id, name, dict){
  4. var node = dict[id];
  5. if (!node){
  6. node = {}
  7. node.id = id;
  8. node.name = name;
  9. dict[id] = node;
  10. }
  11. return node;
  12. }
  13. function addNode(parentid, id, name, dict){
  14. var parent = dict[parentid];
  15. var node = dict[id];
  16. if (!node){
  17. node = getOrCreateNode(id,name,dict);
  18. node.parent = parent.id;
  19. if (!parent.children){
  20. parent.children = [];
  21. }
  22. parent.children.push(node);
  23. }
  24. return node;
  25. }
  26. function transform(data){
  27. var idToNode = [];
  28. var root = getOrCreateNode("-1",root_name,idToNode);
  29. root.parent = "null";
  30. var columns = data.head.vars;
  31. data.results.bindings.forEach(item => {
  32. var parent_id = "-1";
  33. for (let i = 0; i < columns.length/2; i++) {
  34. if (item[columns[2*i]]) {
  35. var childId = (!unique ? parent_id+"." : "") +item[columns[2*i]].value;
  36. addNode(parent_id, childId, item[columns[2*i+1]].value, idToNode);
  37. parent_id = childId;
  38. }
  39. }
  40. });
  41. return root;
  42. }
  43. function getVal(d){
  44. return d.value + (d.children ? d.data.value : 0);
  45. }
  46. var treeData = transform(data);
  47. // ************** Generate the tree diagram *****************
  48. var margin = {top: 20, right: 120, bottom: 20, left: 120},
  49. width = _width - margin.right - margin.left,
  50. height = _height - margin.top - margin.bottom;
  51. var i = 0,
  52. root;
  53. var root = d3.hierarchy(treeData)
  54. .sum(d => d.value)
  55. .sort((a, b) => b.value - a.value)
  56. var tree = d3.tree();
  57. tree.size([height, width]);
  58. var diagonal = d3.linkHorizontal().x(d => d.y).y(d => d.x)
  59. var svg = d3.select(containerID).append("svg")
  60. .attr("width", width + margin.right + margin.left)
  61. .attr("height", height + margin.top + margin.bottom)
  62. .append("g")
  63. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  64. const gLink = svg.append("g")
  65. .attr("fill", "none")
  66. .attr("stroke", "#555")
  67. .attr("stroke-opacity", 0.4)
  68. .attr("stroke-width", 1.5);
  69. root.x0 = height / 2;
  70. root.y0 = 0;
  71. function update(source) {
  72. const duration = d3.event && d3.event.altKey ? 2500 : 250;
  73. const nodes = root.descendants().reverse();
  74. const links = root.links();
  75. // Compute the new tree layout.
  76. tree(root);
  77. let left = root;
  78. let right = root;
  79. root.eachBefore(node => {
  80. if (node.x < left.x) left = node;
  81. if (node.x > right.x) right = node;
  82. });
  83. // Normalize for fixed-depth.
  84. nodes.forEach(function(d) { d.y = d.depth * depthMultiplier; });
  85. const height = right.x - left.x + margin.top + margin.bottom;
  86. const transition = svg.transition()
  87. .duration(duration);
  88. // Update the nodes…
  89. var node = svg.selectAll("g.node")
  90. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  91. // Enter any new nodes at the parent's previous position.
  92. const nodeEnter = node.enter().append("g")
  93. .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
  94. .attr("class", "node")
  95. .on("click", click);
  96. nodeEnter.append("circle")
  97. .attr("r", 1e-6)
  98. .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  99. nodeEnter.append("text")
  100. .attr("dy", "0.35em")
  101. .attr("x", function(d) { return d.children || d._children ? -13 : 13; })
  102. .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
  103. .text(function(d){
  104. return d.data.name;
  105. });
  106. // Transition nodes to their new position.
  107. const nodeUpdate = node.merge(nodeEnter).transition(transition)
  108. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
  109. nodeUpdate.select("circle")
  110. .attr("r", 10)
  111. .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  112. nodeUpdate.select("text")
  113. .style("fill-opacity", 1);
  114. // Transition exiting nodes to the parent's new position.
  115. const nodeExit = node.exit().transition(transition)
  116. .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
  117. .remove();
  118. nodeExit.select("circle")
  119. .attr("r", 1e-6);
  120. nodeExit.select("text")
  121. .style("fill-opacity", 1e-6);
  122. // Update the links…
  123. const link = gLink.selectAll("path")
  124. .data(links, d => d.target.id);
  125. // Enter any new links at the parent's previous position.
  126. const linkEnter = link.enter().append("path")
  127. .attr("class", "link")
  128. .attr("d", d => {
  129. const o = {x: source.x0, y: source.y0};
  130. return diagonal({source: o, target: o});
  131. });
  132. // Transition links to their new position.
  133. link.merge(linkEnter).transition(transition)
  134. .attr("d", diagonal);
  135. // Transition exiting nodes to the parent's new position.
  136. link.exit().transition(transition).remove()
  137. .attr("d", d => {
  138. const o = {x: source.x, y: source.y};
  139. return diagonal({source: o, target: o});
  140. });
  141. // Stash the old positions for transition.
  142. root.eachBefore(d => {
  143. d.x0 = d.x;
  144. d.y0 = d.y;
  145. });
  146. }
  147. update(root);
  148. // Toggle children on click.
  149. function click(event, d) {
  150. if (d.children) {
  151. d._children = d.children;
  152. d.children = null;
  153. } else {
  154. d.children = d._children;
  155. d._children = null;
  156. }
  157. update(d);
  158. }
  159. }
  160. export { drawTree };
  161. export default drawTree;