parset.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. Shiny.addCustomMessageHandler("parset_values", function (message) {
  2. data = message[0];
  3. data1 = transposeData(message[1]);
  4. var scopes = ["pais", "regiao", "uf", "mesorregiao", "microrregiao", "municipio"];
  5. var margin = {top: 0, right: 50, bottom: 50, left: 0},
  6. width = window.innerWidth - margin.left - margin.right,
  7. height = window.innerHeight - margin.top - margin.bottom;
  8. var chart = d3.parsets()
  9. .width(width)
  10. .height(height)
  11. .dimensions(Object.keys(message[1]));
  12. var parset = d3.select("#parset_area").append("svg")
  13. .attr("width", width + margin.left + margin.right)
  14. .attr("height", height + margin.top + margin.bottom);
  15. parset.datum(data1).call(chart);
  16. function transposeData(d) {
  17. var values = Object.keys(d);
  18. var len = d.CNAE.length;
  19. var datainsert = new Object();
  20. var dataTransposed = [];
  21. for (var i = 0; i < len; i++) {
  22. values.forEach(function (v) {
  23. datainsert[v] = d[v][i];
  24. });
  25. dataTransposed.push(datainsert);
  26. datainsert = new Object();
  27. }
  28. return dataTransposed;
  29. }
  30. //Treemap code
  31. var tmargin = {
  32. top: 25,
  33. right: 0,
  34. bottom: 0,
  35. left: 0
  36. },
  37. twidth = window.innerWidth * 0.95,
  38. theight = window.innerHeight * 0.6,
  39. formatNumber = d3.format(",d"),
  40. transitioning,
  41. hovering;
  42. var tm_tooltip = d3.select("body")
  43. .append("div")
  44. .style("position", "absolute")
  45. .style("z-index", "10")
  46. .style("visibility", "hidden")
  47. .style("color", "white")
  48. .style("padding", "8px")
  49. .style("background-color", "rgba(0, 0, 0, 0.75)")
  50. .style("border-radius", "6px")
  51. .style("font", "12px sans-serif")
  52. .text("tooltip");
  53. var x = d3.scaleLinear()
  54. .domain([0, twidth])
  55. .range([0, twidth]);
  56. var y = d3.scaleLinear()
  57. .domain([0, theight - tmargin.top - tmargin.bottom])
  58. .range([0, theight - 3 * tmargin.top - tmargin.bottom]);
  59. var c = [];
  60. function q(d) {
  61. for (var i = 0; i < d.children.length; i++) {
  62. c[i] = d.children[i].value;
  63. }
  64. return c.sort(function (a, b) {
  65. return (a - b);
  66. });
  67. }
  68. q(data);
  69. var maxValue = c[c.length - 1];
  70. var minValue = maxValue / 9;
  71. var color = d3.scaleThreshold()
  72. .domain([minValue, minValue * 2, minValue * 3, minValue * 4, minValue * 5, minValue * 6, minValue * 7, minValue * 8])
  73. .range(d3.schemeBlues[9]);
  74. var format = d3.format(",d");
  75. var createTreemap;
  76. var treemap, grandparent;
  77. updateDrillDown();
  78. function updateDrillDown() {
  79. if (treemap) {
  80. treemap.selectAll("*").remove();
  81. } else {
  82. treemap = d3.select("#treemap_area").append("svg")
  83. .attr("width", twidth + tmargin.left + tmargin.right)
  84. .attr("height", theight + tmargin.bottom + tmargin.top)
  85. //.style("tmargin-left", -tmargin.left + "px")
  86. //.style("tmargin.right", -tmargin.right + "px")
  87. .append("g")
  88. .attr("transform", "translate(" + tmargin.left + "," + tmargin.top + ")")
  89. .style("shape-rendering", "crispEdges");
  90. grandparent = treemap.append("g")
  91. .attr("class", "grandparent");
  92. grandparent.append("rect")
  93. .style("fill", "#ADEAEA")
  94. .style("padding", "1px")
  95. .attr("y", -tmargin.top)
  96. .attr("width", twidth)
  97. .attr("height", tmargin.top);
  98. grandparent.append("text")
  99. .attr("x", 6)
  100. .attr("y", 6 - tmargin.top)
  101. .attr("dy", ".75em");
  102. createTreemap = d3.treemap()
  103. .tile(d3.treemapBinary)
  104. .size([twidth, theight - 10])
  105. .round(false)
  106. .paddingInner(0);
  107. }
  108. var root = d3.hierarchy(data)
  109. .eachBefore(function (d) {
  110. d.id = (d.parent ? d.parent.id + "." : "") + d.data.name;
  111. })
  112. .sum(function (d) {
  113. return d.value;
  114. })
  115. .sort(function (a, b) {
  116. return b.height - a.height || b.value - a.value;
  117. });
  118. initialize(root);
  119. accumulate(root);
  120. layout(root);
  121. createTreemap(root);
  122. display(root);
  123. };
  124. function initialize(root) {
  125. root.x = root.y = 0;
  126. root.x1 = twidth;
  127. root.y1 = theight;
  128. root.depth = 0;
  129. }
  130. function accumulate(d) {
  131. return (d._children = d.children) ?
  132. d.value = d.children.reduce(function (p, v) {
  133. return p + accumulate(v);
  134. }, 0) : d.value;
  135. }
  136. function layout(d) {
  137. if (d._children) {
  138. d._children.forEach(function (c) {
  139. c.x0 = d.x0 + c.x0 * d.x1;
  140. c.y0 = d.y0 + c.y0 * d.y1;
  141. c.x1 *= (d.x1 - d.x0);
  142. c.y1 *= (d.y1 - d.y0);
  143. c.parent = d;
  144. layout(c);
  145. });
  146. }
  147. }
  148. function display(d) {
  149. grandparent
  150. .datum(d.parent)
  151. .on("click", transition)
  152. .select("text")
  153. .text(name(d));
  154. var g1 = treemap.insert("g", ".grandparent")
  155. .datum(d)
  156. .attr("class", "depth");
  157. var g = g1.selectAll("g")
  158. .data(d._children)
  159. .enter().append("g").style("opacity", 1);
  160. g.filter(function (d) {
  161. return d._children;
  162. })
  163. .classed("children", true)
  164. .on("click", transition);
  165. var children = g.selectAll(".child")
  166. .data(function (d) {
  167. return d._children || [d];
  168. })
  169. .enter().append("g") //SHOW CHILDREN FROM TREEMAP VIEW
  170. .style("opacity", 0.7); // DEFINES THE OPACITY OF THE INTERNAL NODES
  171. children.append("rect")
  172. .attr("class", "child")
  173. .call(rect)
  174. .append("title")
  175. .text(function (d) {
  176. return d.data.name + " (" + formatNumber(d.data.value) + ")";
  177. });
  178. children.append("text")
  179. .attr("class", "ctext").style("font-size", "14px")
  180. .text(function (d) {
  181. return d.data.name;
  182. })
  183. .call(text2);
  184. g.append("rect")
  185. .attr("class", "parent")
  186. .call(rect);
  187. //MOUSEOVER: SHOW CHILDREN OF THE NODES
  188. g.selectAll("rect").classed("parent", true).on("mouseover", function (d) {
  189. d3.select(this).style("opacity", 0.3); //.
  190. tm_tooltip.html(d.data.name + "<br/>" + formatNumber(d.data.value));
  191. tm_tooltip.transition();
  192. return tm_tooltip.style("visibility", "visible").style("opacity", 1);
  193. //children.select("g").style("opacity", 1);
  194. });
  195. //MOUSEOUT: HIDE CHILDREN OF THE NODES
  196. g.selectAll("rect").classed("parent", false).on("mouseout", function () {
  197. d3.select(this).style("opacity", 1);
  198. return tm_tooltip.style("visibility", "hidden");
  199. });
  200. g.selectAll("rect").classed("parent", false).on("mousemove", function () {
  201. d3.select(this).style("opacity", 0.3);
  202. tm_tooltip.style("opacity", 1);
  203. return tm_tooltip.style("top", (d3.event.pageY - 10) + "px")
  204. .style("left", (d3.event.pageX + 10) + "px");
  205. });
  206. var t = g.append("text")
  207. .attr("class", "ptext").style("font-weight", "bold")
  208. .attr("dy", ".75em");
  209. t.append("tspan")
  210. .attr("font-weight", "bold")
  211. .text(function (d) {
  212. return d.data.name;
  213. });
  214. t.append("tspan")
  215. .attr("dy", "1.0em")
  216. .text(function (d) {
  217. return formatNumber(d.data.value);
  218. });
  219. t.call(text);
  220. g.selectAll("rect")
  221. .style("fill", function (d) {
  222. return color(d.data.value);
  223. });
  224. function transition(d) {
  225. //handle parallel sets data
  226. Shiny.onInputChange("scope", scopes[d.depth]);
  227. Shiny.onInputChange("name", d.data.name);
  228. Shiny.addCustomMessageHandler("treemap_values_change", function (message) {
  229. chart = d3.parsets()
  230. .width(width)
  231. .height(height)
  232. .dimensions(Object.keys(message));
  233. var data = transposeData(message);
  234. d3.select("#parset_area svg").remove();
  235. parset = d3.select("#parset_area").append("svg")
  236. .attr("width", width + margin.left + margin.right)
  237. .attr("height", height + margin.top + margin.bottom);
  238. parset.datum(data).call(chart);
  239. });
  240. if (transitioning || !d) return;
  241. if (transitioning || !d) return;
  242. transitioning = true;
  243. var g2 = display(d),
  244. t1 = g1.transition().duration(750),
  245. t2 = g2.transition().duration(750);
  246. // Update the domain only after entering new elements.
  247. x.domain([d.x0, d.x0 + (d.x1 - d.x0)]);
  248. y.domain([d.y0, d.y0 + (d.y1 - d.y0)]);
  249. // Enable anti-aliasing during the transition.
  250. treemap.style("shape-rendering", null);
  251. // Draw child nodes on top of parent nodes.
  252. //treemap.selectAll(".depth").sort(function(a, b) {
  253. //console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
  254. //return a.depth - b.depth; });
  255. // Fade-in entering text.
  256. g2.selectAll("text").style("fill-opacity", 0);
  257. // Transition to the new view.
  258. t1.selectAll(".ptext")
  259. .call(text)
  260. .style("fill-opacity", 0)
  261. .style("font-weight", "bold"); //.style("font-size", "12px");
  262. t2.selectAll(".ptext")
  263. .call(text)
  264. .style("fill-opacity", 1)
  265. .style("font-weight", "bold"); //.style("font-size", "12px");
  266. t1.selectAll(".ctext")
  267. .call(text2)
  268. .style("fill-opacity", 0);
  269. t2.selectAll(".ctext")
  270. .call(text2)
  271. .style("fill-opacity", 1);
  272. t1.selectAll("rect")
  273. .call(rect);
  274. t2.selectAll("rect")
  275. .call(rect);
  276. // Remove the old node when the transition is finished.
  277. t1.remove().on("end", function () {
  278. treemap.style("shape-rendering", "crispEdges");
  279. transitioning = false;
  280. });
  281. }
  282. return g;
  283. }
  284. function make_title(d) {
  285. return d ? d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  286. }
  287. function text(text) {
  288. text.selectAll("tspan")
  289. .attr("x", function (d) {
  290. return x(d.x0) + 5;
  291. });
  292. text.attr("x", function (d) {
  293. return x(d.x0) + 5;
  294. })
  295. .attr("y", function (d) {
  296. return y(d.y0) + 3;
  297. })
  298. .style("opacity", function (d) {
  299. var w = x(d.x1) - x(d.x0);
  300. return this.getComputedTextLength() <= w ? 1 : 0;
  301. })
  302. .style("font-size", "10px");
  303. }
  304. function text2(text) {
  305. text
  306. .attr("x", function (d) {
  307. return x(d.x1) - this.getComputedTextLength() - 2;
  308. })
  309. .attr("y", function (d) {
  310. return y(d.y1) - 2;
  311. })
  312. .style("opacity", function (d) {
  313. var w = x(d.x1) - x(d.x0);
  314. return this.getComputedTextLength() <= w ? 1 : 0;
  315. })
  316. .style("font-size", "14px");
  317. }
  318. function rect(rect) {
  319. rect.attr("x", function (d) {
  320. return x(d.x0);
  321. })
  322. .attr("y", function (d) {
  323. return y(d.y0);
  324. })
  325. .attr("width", function (d) {
  326. var w = x(d.x1) - x(d.x0) - 1;
  327. return w;
  328. })
  329. .attr("height", function (d) {
  330. var h = y(d.y1) - y(d.y0);
  331. return h;
  332. })
  333. .style("stroke", "darkblue")
  334. .style("stroke-width", 1);
  335. }
  336. function name(d) {
  337. return d.parent ? name(d.parent) + " -> " + d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  338. }
  339. });