treemap2.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. Shiny.addCustomMessageHandler("treemap2_values", function(message) {
  2. var data = message[0]; // Get the single object inside the message
  3. var margin = {top: 25, right: 0, bottom: 0, left: 0},
  4. width = window.innerWidth * 0.9,
  5. height = window.innerHeight * 0.6,
  6. formatNumber = d3.format(",d"),
  7. transitioning,
  8. hovering;
  9. var tooltip = d3.select("body")
  10. .append("div")
  11. .style("position", "absolute")
  12. .style("z-index", "10")
  13. .style("visibility", "hidden")
  14. .style("color", "white")
  15. .style("padding", "8px")
  16. .style("background-color", "rgba(0, 0, 0, 0.75)")
  17. .style("border-radius", "6px")
  18. .style("font", "12px sans-serif")
  19. .text("tooltip");
  20. var x = d3.scaleLinear()
  21. .domain([0, width])
  22. .range([0, width]);
  23. var y = d3.scaleLinear()
  24. .domain([0, height - margin.top - margin.bottom])
  25. .range([0, height - 3*margin.top - margin.bottom]);
  26. var c = [];
  27. function q (d){
  28. for (var i = 0; i < d.children.length; i++) {
  29. c[i] = d.children[i].value;
  30. //Do something
  31. }
  32. return c.sort(function(a,b){return (a - b);});
  33. }
  34. q(data);
  35. var maxValue = c[c.length - 1];
  36. var minValue = maxValue / 9;
  37. var color = d3.scaleThreshold()
  38. .domain([minValue, minValue*2, minValue*3, minValue*4, minValue*5, minValue*6, minValue*7, minValue*8])
  39. .range(d3.schemeBlues[9]);
  40. // var color = d3.scaleQuantile()
  41. // .domain(/*GET VALUE BY LEVEL*/
  42. // .range(d3.schemeBlues[9]);
  43. //var color = d3.scaleOrdinal()
  44. // .range(d3.schemeSet3);
  45. //.map(function(c) { c = d3.rgb(c); c.opacity = 1; return c; });
  46. //var color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)); //schemeCategory20* is no longer supported
  47. //var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); };
  48. var format = d3.format(",d");
  49. var createTreemap; //Modificado de treemap
  50. var treemap, grandparent; //Modificar de svg para treemap
  51. updateDrillDown();
  52. function updateDrillDown() {
  53. if (treemap) { //Modificar de svg para treemap
  54. treemap.selectAll("*").remove(); //Modificar de svg para treemap
  55. } else {
  56. // var treemap = d3.layout.treemap()
  57. // .children(function(d, depth) { return depth ? null : d._children; })
  58. // .sort(function(a, b) { return a.value - b.value; })
  59. // .ratio(height / width * 0.5 * (1 + Math.sqrt(5)))
  60. // .round(false);
  61. treemap = d3.select("#treemap2_area").append("svg")
  62. .attr("width", width - margin.left - margin.right)
  63. .attr("height", height - margin.bottom - margin.top)
  64. .style("margin-left", -margin.left + "px")
  65. .style("margin.right", -margin.right + "px")
  66. .append("g")
  67. .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
  68. .style("shape-rendering", "crispEdges");
  69. grandparent = treemap.append("g") //Modificar de svg para treemap
  70. .attr("class", "grandparent");
  71. grandparent.append("rect")
  72. .style("fill", "#ADEAEA")
  73. //.style("stroke", "#ADEAEA")
  74. .style("padding", "1px")
  75. .attr("y", -margin.top)
  76. .attr("width", width)
  77. .attr("height", margin.top);
  78. grandparent.append("text")
  79. .attr("x", 6)
  80. .attr("y", 6 - margin.top)
  81. .attr("dy", ".75em");
  82. createTreemap = d3.treemap() //Modificado de treemap
  83. .tile(d3.treemapBinary) //.ratio(height / width * 0.5 * (1 + Math.sqrt(5))/2))
  84. .size([width, height - 10])
  85. .round(false)
  86. .paddingInner(0);
  87. }
  88. var root = d3.hierarchy(data)
  89. .eachBefore(function(d) { d.id = (d.parent ? d.parent.id + "." : "") + d.data.name; })
  90. //.sum((d) => d.value)
  91. .sum(function(d) {
  92. //console.log(d.value);
  93. return d.value;
  94. })
  95. .sort(function(a, b) {
  96. //console.log('initial root sort a ' + a.value + ' b ' + b.value);
  97. return b.height - a.height || b.value - a.value;
  98. });
  99. initialize(root);
  100. accumulate(root);
  101. layout(root);
  102. createTreemap(root); //Modificado de treemap
  103. display(root);
  104. };
  105. function initialize(root) {
  106. root.x = root.y = 0;
  107. root.x1 = width;
  108. root.y1 = height;
  109. root.depth = 0;
  110. }
  111. // Aggregate the values for internal nodes. This is normally done by the
  112. // treemap layout, but not here because of our custom implementation.
  113. // We also take a snapshot of the original children (_children) to avoid
  114. // the children being overwritten when when layout is computed.
  115. function accumulate(d) {
  116. //console.log('accumulate called ' + d.data.name);
  117. return (d._children = d.children)
  118. ? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0) : d.value;
  119. }
  120. // Compute the treemap layout recursively such that each group of siblings
  121. // uses the same size (1×1) rather than the dimensions of the parent cell.
  122. // This optimizes the layout for the current zoom state. Note that a wrapper
  123. // object is created for the parent node for each group of siblings so that
  124. // the parent’s dimensions are not discarded as we recurse. Since each group
  125. // of sibling was laid out in 1×1, we must rescale to fit using absolute
  126. // coordinates. This lets us use a viewport to zoom.
  127. function layout(d) {
  128. if (d._children) {
  129. // treemap.nodes({_children: d._children});
  130. // treemap(d);
  131. d._children.forEach(function(c) {
  132. /*c.x0 = d.x0 + c.x0 * (d.x1 - d.x0);
  133. c.y0 = d.y0 + c.y0 * (d.y1 - d.y0);
  134. c.x1 *= d.x1;
  135. c.y1 *= d.y1;*/
  136. c.x0 = d.x0 + c.x0 * d.x1;
  137. c.y0 = d.y0 + c.y0 * d.y1;
  138. c.x1 *= (d.x1 - d.x0);
  139. c.y1 *= (d.y1 - d.y0);
  140. c.parent = d;
  141. layout(c);
  142. });
  143. }
  144. }
  145. function display(d) {
  146. grandparent
  147. .datum(d.parent)
  148. .on("click", transition)
  149. .select("text")
  150. .text(name(d));
  151. var g1 = treemap.insert("g", ".grandparent") //Modificar de svg para treemap
  152. .datum(d)
  153. .attr("class", "depth");
  154. var g = g1.selectAll("g")
  155. .data(d._children)
  156. .enter().append("g").style("opacity", 1);
  157. g.filter(function(d) { return d._children; })
  158. .classed("children", true)
  159. .on("click", transition);
  160. var children = g.selectAll(".child")
  161. .data(function(d) { return d._children || [d]; })
  162. .enter().append("g")//SHOW CHILDREN FROM TREEMAP VIEW
  163. .style("opacity", 0.7);// DEFINES THE OPACITY OF THE INTERNAL NODES
  164. children.append("rect")
  165. .attr("class", "child")
  166. .call(rect)
  167. .append("title")
  168. .text(function(d) { return d.data.name + " (" + formatNumber(d.data.value) + ")"; });
  169. children.append("text")
  170. .attr("class", "ctext").style("font-size", "12px")
  171. .text(function(d) { return d.data.name; })
  172. .call(text2);
  173. g.append("rect")
  174. .attr("class", "parent")
  175. .call(rect);
  176. //MOUSEOVER: SHOW CHILDREN OF THE NODES
  177. g.selectAll("rect").classed("parent",true).on("mouseover", function(d) {
  178. d3.select(this).style("opacity", 0.3);//.
  179. tooltip.html(d.data.name + "<br/>" + formatNumber(d.data.value));
  180. tooltip.transition();
  181. return tooltip.style("visibility", "visible").style("opacity", 1);
  182. //children.select("g").style("opacity", 1);
  183. });
  184. //MOUSEOUT: HIDE CHILDREN OF THE NODES
  185. g.selectAll("rect").classed("parent",false).on("mouseout", function() {
  186. d3.select(this).style("opacity", 1);
  187. return tooltip.style("visibility", "hidden");
  188. });
  189. g.selectAll("rect").classed("parent",false).on("mousemove", function() {
  190. d3.select(this).style("opacity", 0.3);
  191. tooltip.style("opacity", 1);
  192. return tooltip.style("top",(d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
  193. });
  194. var t = g.append("text")
  195. .attr("class", "ptext").style("font-weight", "bold")
  196. .attr("dy", ".75em");
  197. t.append("tspan")
  198. .attr("font-weight", "bold")
  199. .text(function(d) { return d.data.name; });
  200. t.append("tspan")
  201. .attr("dy", "1.0em")
  202. .text(function(d) { return formatNumber(d.data.value); });
  203. t.call(text);
  204. g.selectAll("rect")
  205. .style("fill", function(d) { return color(d.data.value); });
  206. function transition(d) {
  207. if (transitioning || !d) return;
  208. transitioning = true;
  209. var g2 = display(d),
  210. t1 = g1.transition().duration(750),
  211. t2 = g2.transition().duration(750);
  212. // Update the domain only after entering new elements.
  213. //x.domain([d.x0, d.x0 + d.x1]);
  214. //y.domain([d.y0, d.y0 + d.y1]);
  215. x.domain([d.x0, d.x0 + (d.x1 - d.x0)]);
  216. y.domain([d.y0, d.y0 + (d.y1 - d.y0)]);
  217. // Enable anti-aliasing during the transition.
  218. treemap.style("shape-rendering", null); //Modificar de svg para treemap
  219. // Draw child nodes on top of parent nodes.
  220. //treemap.selectAll(".depth").sort(function(a, b) { //Modificar de svg para treemap
  221. //console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
  222. //return a.depth - b.depth; });
  223. // Fade-in entering text.
  224. g2.selectAll("text").style("fill-opacity", 0);
  225. // Transition to the new view.
  226. t1.selectAll(".ptext").call(text).style("fill-opacity", 0).style("font-weight", "bold");//.style("font-size", "12px");
  227. t2.selectAll(".ptext").call(text).style("fill-opacity", 1).style("font-weight", "bold");//.style("font-size", "12px");
  228. t1.selectAll(".ctext").call(text2).style("fill-opacity", 0);
  229. t2.selectAll(".ctext").call(text2).style("fill-opacity", 1);
  230. t1.selectAll("rect").call(rect);//.style("fill", function(d) { return color(d.data.value); });
  231. t2.selectAll("rect").call(rect);//.style("fill", function(d) { return color(d.data.value); });
  232. // Remove the old node when the transition is finished.
  233. t1.remove().on("end", function() {
  234. treemap.style("shape-rendering", "crispEdges"); //Modificar de svg para treemap
  235. transitioning = false;
  236. });
  237. }
  238. return g;
  239. }
  240. function make_title(d){
  241. //console.log("making_title", d);
  242. return d ? d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  243. }
  244. function text(text) {
  245. text.selectAll("tspan")
  246. .attr("x", function(d) { return x(d.x0) + 5;});
  247. text.attr("x", function(d) { return x(d.x0) + 5;})
  248. .attr("y", function(d) { return y(d.y0) + 3;})
  249. .style("opacity", function(d) {
  250. var w = x(d.x1) - x(d.x0);
  251. //console.log("text opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
  252. return this.getComputedTextLength() <= w ? 1 : 0; })
  253. .style("font-size", "10px");
  254. }
  255. function text2(text) {
  256. text
  257. .attr("x", function(d) {
  258. return x(d.x1) - this.getComputedTextLength() - 2;
  259. })
  260. .attr("y", function(d) { return y(d.y1) - 2; })
  261. .style("opacity", function(d) {
  262. var w = x(d.x1) - x(d.x0);
  263. //console.log("text2 opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
  264. return this.getComputedTextLength() <= w ? 1 : 0;
  265. })
  266. .style("font-size", "10px");
  267. }
  268. function rect(rect) {
  269. rect.attr("x", function(d) { return x(d.x0); })
  270. .attr("y", function(d) { return y(d.y0); })
  271. .attr("width", function(d) {
  272. var w = x(d.x1) - x(d.x0) - 1;
  273. //console.log('id ' + d.id +' rect width ' + w);
  274. return w;
  275. })
  276. .attr("height", function(d) {
  277. var h = y(d.y1) - y(d.y0);
  278. //console.log('id ' + d.id +' rect height ' + h);
  279. return h;
  280. })
  281. .style("stroke", "darkblue")
  282. .style("stroke-width", 1);
  283. }
  284. function name(d) {
  285. return d.parent ? name(d.parent) + " -> " + d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  286. }
  287. });