treemap.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. Shiny.addCustomMessageHandler("treemap_values", function(message) {
  2. var data = message;
  3. console.log(data);
  4. var margin = {top: 24, right: 0, bottom: 0, left: 0},
  5. width = 1200, //640
  6. height = 500,
  7. formatNumber = d3.format(",d"),
  8. transitioning;
  9. var x = d3.scaleLinear()
  10. .domain([0, width])
  11. .range([0, width]);
  12. var y = d3.scaleLinear()
  13. .domain([0, height - margin.top - margin.bottom])
  14. .range([0, height - 3*margin.top - margin.bottom]);
  15. var color = d3.scaleOrdinal()
  16. .range(d3.schemeSet3
  17. .map(function(c) { c = d3.rgb(c); c.opacity = 0.7; return c; }));
  18. //var color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)); //schemeCategory20* is no longer supported
  19. var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); };
  20. var format = d3.format(",d");
  21. var treemap;
  22. var svg, grandparent;
  23. updateDrillDown();
  24. function updateDrillDown() {
  25. if (svg) {
  26. svg.selectAll("*").remove();
  27. } else {
  28. // var treemap = d3.layout.treemap()
  29. // .children(function(d, depth) { return depth ? null : d._children; })
  30. // .sort(function(a, b) { return a.value - b.value; })
  31. // .ratio(height / width * 0.5 * (1 + Math.sqrt(5)))
  32. // .round(false);
  33. svg = d3.select("#treemap_area").append("svg")
  34. .attr("width", width - margin.left - margin.right)
  35. .attr("height", height - margin.bottom - margin.top)
  36. .style("margin-left", -margin.left + "px")
  37. .style("margin.right", -margin.right + "px")
  38. .append("g")
  39. .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
  40. .style("shape-rendering", "crispEdges");
  41. grandparent = svg.append("g")
  42. .attr("class", "grandparent");
  43. grandparent.append("rect")
  44. .attr("y", -margin.top)
  45. .attr("width", width)
  46. .attr("height", margin.top);
  47. grandparent.append("text")
  48. .attr("x", 6)
  49. .attr("y", 6 - margin.top)
  50. .attr("dy", ".75em");
  51. treemap = d3.treemap()
  52. .tile(d3.treemapResquarify.ratio(height / width * 0.5 * (1 + Math.sqrt(5))))
  53. .size([width, height - 10])
  54. .round(true)
  55. .paddingInner(0.5);
  56. }
  57. var root = d3.hierarchy(data)
  58. .eachBefore(function(d) { d.id = (d.parent ? d.parent.id + "." : "") + d.data.name; })
  59. //.sum((d) => d.value)
  60. .sum(function(d) {
  61. //console.log(d.value);
  62. return d.value;
  63. })
  64. .sort(function(a, b) {
  65. //console.log('initial root sort a ' + a.value + ' b ' + b.value);
  66. return b.height - a.height || b.value - a.value;
  67. });
  68. initialize(root);
  69. accumulate(root);
  70. layout(root);
  71. treemap(root);
  72. display(root);
  73. };
  74. function initialize(root) {
  75. root.x = root.y = 0;
  76. root.x1 = width;
  77. root.y1 = height;
  78. root.depth = 0;
  79. }
  80. // Aggregate the values for internal nodes. This is normally done by the
  81. // treemap layout, but not here because of our custom implementation.
  82. // We also take a snapshot of the original children (_children) to avoid
  83. // the children being overwritten when when layout is computed.
  84. function accumulate(d) {
  85. //console.log('accumulate called ' + d.data.name);
  86. return (d._children = d.children)
  87. ? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0) : d.value;
  88. }
  89. // Compute the treemap layout recursively such that each group of siblings
  90. // uses the same size (1×1) rather than the dimensions of the parent cell.
  91. // This optimizes the layout for the current zoom state. Note that a wrapper
  92. // object is created for the parent node for each group of siblings so that
  93. // the parent’s dimensions are not discarded as we recurse. Since each group
  94. // of sibling was laid out in 1×1, we must rescale to fit using absolute
  95. // coordinates. This lets us use a viewport to zoom.
  96. function layout(d) {
  97. if (d._children) {
  98. // treemap.nodes({_children: d._children});
  99. // treemap(d);
  100. d._children.forEach(function(c) {
  101. /*c.x0 = d.x0 + c.x0 * (d.x1 - d.x0);
  102. c.y0 = d.y0 + c.y0 * (d.y1 - d.y0);
  103. c.x1 *= d.x1;
  104. c.y1 *= d.y1;*/
  105. c.x0 = d.x0 + c.x0 * d.x1;
  106. c.y0 = d.y0 + c.y0 * d.y1;
  107. c.x1 *= (d.x1 - d.x0);
  108. c.y1 *= (d.y1 - d.y0);
  109. c.parent = d;
  110. layout(c);
  111. });
  112. }
  113. }
  114. function display(d) {
  115. grandparent
  116. .datum(d.parent)
  117. .on("click", transition)
  118. .select("text")
  119. .text(name(d));
  120. var g1 = svg.insert("g", ".grandparent")
  121. .datum(d)
  122. .attr("class", "depth");
  123. var g = g1.selectAll("g")
  124. .data(d._children)
  125. .enter().append("g");
  126. g.filter(function(d) { return d._children; })
  127. .classed("children", true)
  128. .on("click", transition);
  129. var children = g.selectAll(".child")
  130. .data(function(d) { return d._children || [d]; })
  131. .enter().append("g");
  132. children.append("rect")
  133. .attr("class", "child")
  134. .call(rect)
  135. .append("title")
  136. .text(function(d) { return d.data.name + " (" + formatNumber(d.data.value) + ")"; });
  137. children.append("text")
  138. .attr("class", "ctext")
  139. .text(function(d) { return d.data.name; })
  140. .call(text2);
  141. g.append("rect")
  142. .attr("class", "parent")
  143. .call(rect);
  144. var t = g.append("text")
  145. .attr("class", "ptext").style("font-weight", "bold")
  146. .attr("dy", ".75em");
  147. t.append("tspan")
  148. .attr("font-weigh", "bold")
  149. .text(function(d) { return d.data.name; });
  150. t.append("tspan")
  151. .attr("dy", "1.0em")
  152. .text(function(d) { return formatNumber(d.data.value); });
  153. t.call(text);
  154. g.selectAll("rect")
  155. .style("fill", function(d) { return color(d.data.name); });
  156. function transition(d) {
  157. if (transitioning || !d) return;
  158. transitioning = true;
  159. var g2 = display(d),
  160. t1 = g1.transition().duration(750),
  161. t2 = g2.transition().duration(750);
  162. // Update the domain only after entering new elements.
  163. //x.domain([d.x0, d.x0 + d.x1]);
  164. //y.domain([d.y0, d.y0 + d.y1]);
  165. x.domain([d.x0, d.x0 + (d.x1 - d.x0)]);
  166. y.domain([d.y0, d.y0 + (d.y1 - d.y0)]);
  167. // Enable anti-aliasing during the transition.
  168. svg.style("shape-rendering", null);
  169. // Draw child nodes on top of parent nodes.
  170. svg.selectAll(".depth").sort(function(a, b) {
  171. //console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
  172. return a.depth - b.depth; });
  173. // Fade-in entering text.
  174. g2.selectAll("text").style("fill-opacity", 0);
  175. // Transition to the new view.
  176. t1.selectAll(".ptext").call(text).style("fill-opacity", 0).style("font-weight", "bold");
  177. t2.selectAll(".ptext").call(text).style("fill-opacity", 1).style("font-weight", "bold");
  178. t1.selectAll(".ctext").call(text2).style("fill-opacity", 0);
  179. t2.selectAll(".ctext").call(text2).style("fill-opacity", 1);
  180. t1.selectAll("rect").call(rect);
  181. t2.selectAll("rect").call(rect);
  182. // Remove the old node when the transition is finished.
  183. t1.remove().on("end", function() {
  184. svg.style("shape-rendering", "crispEdges");
  185. transitioning = false;
  186. });
  187. }
  188. return g;
  189. }
  190. function text(text) {
  191. text.selectAll("tspan")
  192. .attr("x", function(d) { return x(d.x0) + 6; });
  193. text.attr("x", function(d) { return x(d.x0) + 6; })
  194. .attr("y", function(d) { return y(d.y0) + 3;})
  195. .style("opacity", function(d) {
  196. var w = x(d.x1) - x(d.x0);
  197. //console.log("text opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
  198. return this.getComputedTextLength() < w ? 1 : 0; });
  199. }
  200. function text2(text) {
  201. text.attr("x", function(d) {
  202. return x(d.x1) - this.getComputedTextLength() - 3;
  203. })
  204. .attr("y", function(d) { return y(d.y1) - 3; })
  205. .style("opacity", function(d) {
  206. var w = x(d.x1) - x(d.x0);
  207. //console.log("text2 opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
  208. return this.getComputedTextLength() < w ? 1 : 0;
  209. });
  210. }
  211. function rect(rect) {
  212. rect.attr("x", function(d) { return x(d.x0); })
  213. .attr("y", function(d) { return y(d.y0); })
  214. .attr("width", function(d) {
  215. var w = x(d.x1) - x(d.x0);
  216. //console.log('id ' + d.id +' rect width ' + w);
  217. return w;
  218. })
  219. .attr("height", function(d) {
  220. var h = y(d.y1) - y(d.y0);
  221. //console.log('id ' + d.id +' rect height ' + h);
  222. return h;
  223. });
  224. }
  225. function name(d) {
  226. return d.parent ? name(d.parent) + " / " + d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  227. }
  228. });