dendrogram.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* based on https://bl.ocks.org/d3noob/43a860bc0024792f8803bba8ca0d5ecd */
  2. Shiny.addCustomMessageHandler("dendrogram_values", function(message) {
  3. var dataset = message;
  4. var bardata = message.n;
  5. // Set the dimensions and margins of the diagram
  6. var margin = {top: 20, right: 90, bottom: 30, left: 90},
  7. width = (window.innerWidth * 0.9) - margin.left - margin.right,
  8. height = (window.innerHeight * 0.9) - margin.top - margin.bottom;
  9. // scales for barchart
  10. var xScale = d3.scaleBand()
  11. .domain(d3.range(bardata.length))
  12. .range([0, width * 0.4])
  13. .paddingInner(0.5);
  14. var yScale = d3.scaleLinear()
  15. .domain([0, d3.max(bardata)])
  16. .range([0, 15]);
  17. // append the svg object to the area reserved for the dendrogram
  18. // appends a 'group' element to 'svg'
  19. // moves the 'group' element to the top left margin
  20. var dendrogram_plot = d3.select("#dendrogram_area").append("svg")
  21. .attr("width", width + margin.right + margin.left)
  22. .attr("height", height + margin.top + margin.bottom)
  23. .append("g")
  24. .attr("transform", "translate("
  25. + margin.left + "," + margin.top + ")");
  26. var i = 0,
  27. duration = 750,
  28. root;
  29. // declares a tree layout and assigns the size
  30. var dendrogram = d3.cluster().size([height, width * 0.4]);
  31. // Assigns parent, children, height, depth
  32. root = d3.hierarchy(dataset, function(d) { return d.children; });
  33. root.x0 = height / 2;
  34. root.y0 = 0;
  35. // Collapse after the second level
  36. root.children.forEach(collapse);
  37. update(root);
  38. //By default show bars only on last level
  39. hideBars(root);
  40. // Collapse the node and all it's children
  41. function collapse(d) {
  42. if(d.children) {
  43. d._children = d.children;
  44. d._children.forEach(collapse);
  45. d.children = null;
  46. }
  47. }
  48. function update(source) {
  49. // Assigns the x and y position for the nodes
  50. var dataset = dendrogram(root);
  51. // Compute the new tree layout.
  52. var nodes = dataset.descendants(),
  53. links = dataset.descendants().slice(1);
  54. // ****************** Nodes section ***************************
  55. // Update the nodes...
  56. var node = dendrogram_plot.selectAll('g.node')
  57. .data(nodes, function(d) {return d.id || (d.id = ++i); });
  58. // Enter any new modes at the parent's previous position.
  59. var nodeEnter = node.enter().append('g')
  60. .attr('class', 'node')
  61. .attr('id', function(d) {
  62. return "node-" + (d.id -1);
  63. })
  64. .attr("transform", function(d) {
  65. return "translate(" + source.y0 + "," + source.x0 + ")";
  66. })
  67. .on('click', click);
  68. // Add Circle for the nodes
  69. nodeEnter.append('circle')
  70. .attr('class', 'node')
  71. .attr('r', 1e-6)
  72. .style("fill", function(d) {
  73. return d._children ? "lightsteelblue" : "#fff";
  74. });
  75. // Add labels for the nodes
  76. nodeEnter.append('text')
  77. .attr("dy", ".35em")
  78. .attr("x", function(d) {
  79. //return d.children || d._children ? -13 : 13;
  80. return -13;
  81. })
  82. .attr("text-anchor", function(d) {
  83. //return d.children || d._children ? "end" : "start";
  84. return "end";
  85. })
  86. .text(function(d) { return d.data.name; });
  87. // Add barcharts to nodes
  88. nodeEnter.selectAll("rect")
  89. .data(bardata)
  90. .enter()
  91. .append("rect")
  92. .classed("bars", true)
  93. .attr("x", function(d, i) {
  94. return 13 + xScale(i);
  95. })
  96. .attr("y", function(d) {
  97. return 13 -yScale(d);
  98. })
  99. .attr("width", xScale.bandwidth())
  100. .attr("height", function(d) {
  101. return yScale(d);
  102. })
  103. .attr("fill", function(d) {
  104. return "rgb(0, 0, " + Math.round(d * 10) + ")";
  105. });
  106. // UPDATE
  107. var nodeUpdate = nodeEnter.merge(node);
  108. // Transition to the proper position for the node
  109. nodeUpdate.transition()
  110. .duration(duration)
  111. .attr("transform", function(d) {
  112. return "translate(" + d.y + "," + d.x + ")";
  113. });
  114. // Update the node attributes and style
  115. nodeUpdate.select('circle.node')
  116. .attr('r', 10)
  117. .style("fill", function(d) {
  118. return d._children ? "lightsteelblue" : "#fff";
  119. })
  120. .attr('cursor', 'pointer');
  121. // Remove any exiting nodes
  122. var nodeExit = node.exit().transition()
  123. .duration(duration)
  124. .attr("transform", function(d) {
  125. return "translate(" + source.y + "," + source.x + ")";
  126. })
  127. .remove();
  128. // On exit reduce the node circles size to 0
  129. nodeExit.select('circle')
  130. .attr('r', 1e-6);
  131. // On exit reduce the opacity of text labels
  132. nodeExit.select('text')
  133. .style('fill-opacity', 1e-6);
  134. // ****************** links section ***************************
  135. // Update the links...
  136. var link = dendrogram_plot.selectAll('path.link')
  137. .data(links, function(d) { return d.id; });
  138. // Enter any new links at the parent's previous position.
  139. var linkEnter = link.enter().insert('path', "g")
  140. .attr("class", "link")
  141. .attr('d', function(d){
  142. var o = {x: source.x0, y: source.y0};
  143. return diagonal(o, o);
  144. });
  145. // UPDATE
  146. var linkUpdate = linkEnter.merge(link);
  147. // Transition back to the parent element position
  148. linkUpdate.transition()
  149. .duration(duration)
  150. .attr('d', function(d){ return diagonal(d, d.parent); });
  151. // Remove any exiting links
  152. var linkExit = link.exit().transition()
  153. .duration(duration)
  154. .attr('d', function(d) {
  155. var o = {x: source.x, y: source.y};
  156. return diagonal(o, o);
  157. })
  158. .remove();
  159. // Store the old positions for transition.
  160. nodes.forEach(function(d){
  161. d.x0 = d.x;
  162. d.y0 = d.y;
  163. });
  164. // Creates a curved (diagonal) path from parent to the child nodes
  165. function diagonal(s, d) {
  166. path = `M ${s.y} ${s.x}
  167. C ${(s.y + d.y) / 2} ${s.x},
  168. ${(s.y + d.y) / 2} ${d.x},
  169. ${d.y} ${d.x}`;
  170. return path;
  171. }
  172. // Toggle children on click.
  173. function click(d) {
  174. //has children, so close them
  175. if (d.children) {
  176. d._children = d.children;
  177. d.children = null;
  178. update(d);
  179. //toggleBars(d);
  180. } else {
  181. //dont have children, so open them
  182. d.children = d._children;
  183. d._children = null;
  184. update(d);
  185. //toggleBars(d);
  186. }
  187. //update(d);
  188. toggleBars(d);
  189. }
  190. }
  191. function showBars(d) {
  192. dendrogram_plot.selectAll("#node-" + (d.id -1) + " .bars")
  193. .attr("fill", function(d) {
  194. return "rgb(0, 0, " + Math.round(d * 10) + ")";
  195. });
  196. }
  197. function hideBars(d) {
  198. dendrogram_plot.selectAll("#node-" + (d.id -1) + " .bars")
  199. .attr("fill", "none");
  200. }
  201. function toggleBars(d) {
  202. // show bars only on last level
  203. if (d.children) {
  204. console.log("selecting #node-" + (d.id -1) + " .bars");
  205. dendrogram_plot.selectAll("#node-" + (d.id -1) + " .bars")
  206. .attr("fill", "none");
  207. d.children.forEach(toggleBars);
  208. } else {
  209. console.log("selecting #node-" + (d.id -1) + " .bars");
  210. dendrogram_plot.selectAll("#node-" + (d.id -1) + " .bars")
  211. .attr("fill", function(d) {
  212. return "rgb(0, 0, " + Math.round(d * 10) + ")";
  213. });
  214. }
  215. }
  216. });