treemap.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. Shiny.addCustomMessageHandler("treemap_values", function (message) {
  2. var data = message[0];
  3. var data1 = message[1];
  4. var data2 = message[2];
  5. var scopes = ["pais", "regiao", "uf", "mesorregiao", "microrregiao", "municipio"];
  6. var nextScope = "regiao";
  7. //Parallel Coordinates code
  8. var traits = ["localidade", "cnae_secao", "ds_natureza_lesao", "cbo_grande_grupo",
  9. "agrupamento_parte_do_corpo", "ds_grupo_agcausadores",
  10. "ds_tipo_acidente", "ds_tipo_local_acidente",
  11. "ds_emitente_cat", "idade", "obito", "sexo", "turno"];
  12. // We have to specify which of the axis are categorical, instead of numerical
  13. var categoricalAxis = ["localidade", "ds_grupo_agcausadores", "cnae_secao",
  14. "ds_emitente_cat", "ds_natureza_lesao" ,"cbo_grande_grupo"
  15. ,"agrupamento_parte_do_corpo", "ds_tipo_acidente",
  16. "ds_tipo_local_acidente"];
  17. function toggleAxis(checkBox, trait, data, div_name) {
  18. if (checkBox.checked == true) {
  19. if (!traits.includes(trait))
  20. traits.push(trait);
  21. }
  22. if (checkBox.checked == false) {
  23. var i = traits.indexOf(trait);
  24. traits.splice(i, 1);
  25. }
  26. d3.select(div_name + " svg").remove();
  27. drawParallelCoords(data, div_name);
  28. }
  29. var checkboxes = d3.select("#parallelcoordinates_area")
  30. .append("div")
  31. .attr("id", "parallelcoordinates_switch");
  32. function reloadCheckBoxes(data, checkboxes, div_name) {
  33. traits.forEach(function (trait) {
  34. checkboxes.append("input")
  35. .attr("type", "checkbox")
  36. .attr("id", trait)
  37. .attr("value", trait)
  38. .attr("checked", true)
  39. .on("click", function () {
  40. toggleAxis(this, trait, data, div_name);
  41. });
  42. checkboxes.append("text")
  43. .text(trait);
  44. });
  45. }
  46. function drawParallelCoords(arrayData, div_name) {
  47. dragging = {};
  48. var background;
  49. var m = {top: 30, right: 15, bottom: 10, left:150},
  50. w = window.innerWidth *0.95 - m.left - m.right,
  51. h = window.innerHeight * 0.8 - m.top - m.bottom;
  52. var line = d3.line(),
  53. axis = d3.axisLeft(),
  54. foreground;
  55. var parcoords = d3.select(div_name).append("svg")
  56. .attr("width", w + m.left + m.right)
  57. .attr("height", h + m.top + m.bottom)
  58. .append("g")
  59. .attr("transform", "translate(" + m.left + "," + m.top + ")");
  60. // Create a scale and brush for each trait
  61. var x = d3.scalePoint().domain(traits).range([0, w]),
  62. y = {};
  63. traits.forEach(function (col) {
  64. var data = [];
  65. if (categoricalAxis.includes(col)) {
  66. arrayData.forEach(function (row) {
  67. if (!arrayData.includes(row[col])) {
  68. data.push(row[col]);
  69. }
  70. });
  71. y[col] = d3.scalePoint()
  72. .domain(data)
  73. .range([h, 0]);
  74. } else {
  75. arrayData.forEach(function (p) {
  76. p[col] = +p[col];
  77. });
  78. if (col == "sexo" || col == "obito" || col == "turno") {
  79. y[col] = d3.scaleLinear()
  80. .domain(
  81. [0, 1]
  82. )
  83. .range([h, 0]);
  84. } else if (col == "idade") { //TODO remove these magic numbers
  85. y[col] = d3.scaleLinear()
  86. .domain(
  87. [19, 67]
  88. )
  89. .range([h, 0]);
  90. } else {
  91. y[col] = d3.scaleLinear()
  92. .domain(d3.extent(arrayData, function (p) {
  93. return p[col];
  94. }))
  95. .range([h, 0]);
  96. }
  97. }
  98. //Create Brush
  99. y[col].brush = d3.brushY()
  100. .extent([
  101. [-7, y[col].range()[1]],
  102. [7, y[col].range()[0]]
  103. ])
  104. .on("start", brushstart)
  105. .on("brush", brushView)
  106. .on("end", brush);
  107. });
  108. //Append Background
  109. background = parcoords.append("g")
  110. .attr("class", "background")
  111. .selectAll("path")
  112. .data(arrayData)
  113. .enter().append("path")
  114. .attr("d", path);
  115. //Add Foreground Lines
  116. foreground = parcoords.append("g")
  117. .attr("class", "foreground")
  118. .selectAll("path")
  119. .data(arrayData)
  120. .enter().append("path")
  121. .attr("d", path);
  122. /*.style("stroke", function (d) {
  123. return colorMake(toColor.indexOf(d[nextScope]));
  124. });*/
  125. // Add a group element for each trait.
  126. var g = parcoords.selectAll(".trait")
  127. .data(traits)
  128. .enter()
  129. .append("svg:g")
  130. .attr("class", "trait")
  131. .attr("transform", function (d) {
  132. return "translate(" + x(d) + ")";
  133. })
  134. .call(d3.drag()
  135. .subject(function (d) {
  136. return {
  137. x: x(d)
  138. };
  139. })
  140. .on("start", function (d) {
  141. dragging[d] = x(d);
  142. background.attr("visibility", "hidden");
  143. })
  144. .on("drag", function (d) {
  145. //+10 e -10 para mudar com o primeiro e o ultimo
  146. dragging[d] = Math.min(w + 10, Math.max(-10, d3.event.x));
  147. foreground.attr("d", path);
  148. traits.sort(function (a, b) {
  149. return position(a) - position(b);
  150. });
  151. x.domain(traits);
  152. g.attr("transform", function (d) {
  153. return "translate(" + position(d) + ")";
  154. });
  155. })
  156. .on("end", function (d) {
  157. delete dragging[d];
  158. transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
  159. transition(foreground).attr("d", path);
  160. background
  161. .attr("d", path)
  162. .transition()
  163. .delay(500)
  164. .duration(0)
  165. .attr("visibility", null);
  166. })
  167. );
  168. //Gradiente do eixo Sexo
  169. var defs = parcoords.append("defs");
  170. var gradient = defs.append("linearGradient")
  171. .attr("id", "svgGradient")
  172. .attr("x1", "50%")
  173. .attr("x2", "100%")
  174. .attr("y1", "50%")
  175. .attr("y2", "100%");
  176. gradient.append("stop")
  177. .attr('class', 'start')
  178. .attr("offset", "0%")
  179. .attr("stop-color", "blue")
  180. .attr("stop-opacity", 1);
  181. gradient.append("stop")
  182. .attr('class', 'end')
  183. .attr("offset", "100%")
  184. .attr("stop-color", "red")
  185. .attr("stop-opacity", 1);
  186. //Axis
  187. g.append("svg:g")
  188. .attr("class", "axis")
  189. .each(function (col) {
  190. if (col == "sexo") {
  191. d3.select(this)
  192. .attr("class", "eixoSexo")
  193. .call(d3.axisLeft(y[col]))
  194. .selectAll("text").remove();
  195. d3.select(this)
  196. .call(d3.axisLeft(y[col])
  197. .ticks(3));
  198. d3.select(this)
  199. .append("svg:text").text("M")
  200. .attr("x", -9)
  201. .attr("y", 10);
  202. d3.select(this)
  203. .append("svg:text").text("F")
  204. .attr("x", -9)
  205. .attr("y", h + 2);
  206. // Text category reduction
  207. } else if (col == "cnae_secao" || col == "ds_natureza_lesao" ||
  208. col == "cbo_grande_grupo" || col == "agrupamento_parte_do_corpo" ||
  209. col == "ds_grupo_agcausadores") {
  210. // ALT
  211. d3.select(this).call(d3.axisLeft(y[col])).selectAll("text")._groups[0].forEach(function (p) {
  212. p.innerHTML = p.innerHTML.slice(0,20).toLowerCase();
  213. });
  214. } else {
  215. d3.select(this).call(d3.axisLeft(y[col]));
  216. }
  217. })
  218. // Axis Titles
  219. .append("svg:text")
  220. .attr("text-anchor", "middle")
  221. .attr("y", -15)
  222. .text(function (col) {
  223. return col;
  224. });
  225. // Add a brush for each axis.
  226. g.append("svg:g")
  227. .attr("class", "brush")
  228. .each(function (d) {
  229. d3.select(this).call(y[d].brush);
  230. })
  231. .selectAll("rect")
  232. .attr("x", -8)
  233. .attr("width", 16);
  234. function position(d) {
  235. var v = dragging[d];
  236. return v == null ? x(d) : v;
  237. }
  238. function transition(g) {
  239. return g.transition().duration(500);
  240. }
  241. // Returns the path for a given data point.
  242. function path(d) {
  243. return line(traits.map(function (p) {
  244. return [position(p), y[p](d[p])];
  245. }));
  246. }
  247. // BRUSH FUNCTIONS
  248. function brushstart() {
  249. d3.event.sourceEvent.stopPropagation();
  250. }
  251. function brushView() {
  252. var actives = [];
  253. //filter brushed extents
  254. parcoords.selectAll(".brush")
  255. .filter(function (d) {
  256. return d3.brushSelection(this);
  257. })
  258. .each(function (d) {
  259. actives.push({
  260. dimension: d,
  261. extent: d3.brushSelection(this)
  262. });
  263. dim = actives[0].dimension;
  264. });
  265. //set un-brushed foreground line disappear
  266. foreground.classed("fade", function (d, i) {
  267. return !actives.every(function (active) {
  268. var dim = active.dimension;
  269. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[
  270. 1];
  271. });
  272. });
  273. }
  274. function brush() {
  275. var actives = [];
  276. //filter brushed extents
  277. parcoords.selectAll(".brush")
  278. .filter(function (d) {
  279. return d3.brushSelection(this);
  280. })
  281. .each(function (d) {
  282. actives.push({
  283. dimension: d,
  284. extent: d3.brushSelection(this)
  285. });
  286. var selected = arrayData.filter(function (d) {
  287. return actives.every(function (active) {
  288. var dim = active.dimension;
  289. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
  290. });
  291. });
  292. });
  293. foreground.classed("fade", function (d, i) {
  294. return !actives.every(function (active) {
  295. var dim = active.dimension;
  296. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
  297. });
  298. });
  299. }
  300. }
  301. var checkboxescities = d3.select("#parallelcoordinates_area_cities")
  302. .append("div")
  303. .attr("id", "parallelcoordinates_switch_cities");
  304. // TRANSPOSE DATA THAT COMES FROM R
  305. // true means to use municipio
  306. function transposeData(d, flag) {
  307. var values = Object.keys(d).slice(1);
  308. index = values.findIndex(x => x ==="cnae_secao");
  309. var len = d.idade.length; //We really only want to know how many lines there is in the file
  310. var datainsert = [];
  311. var dataTransposed = [];
  312. var i, j;
  313. for (i = 0; i < len; i++) {
  314. if(flag) {
  315. datainsert["localidade"] = d[values[index-1]][i];
  316. } else {
  317. datainsert["localidade"] = d[nextScope][i];
  318. }
  319. values.forEach(function (v) {
  320. datainsert[v] = d[v][i];
  321. });
  322. dataTransposed.push(datainsert);
  323. datainsert = [];
  324. }
  325. return dataTransposed;
  326. }
  327. /*var colorMake = color = d3.scaleOrdinal().range(d3.schemeCategory10);
  328. var toColorDup = data1[nextScope];
  329. toColor = toColorDup.filter(function (elem, index, self) {
  330. return index === self.indexOf(elem);
  331. });*/
  332. var colors_subtitle = [];
  333. /*for (var i = 0; i < toColor.length; i++) {
  334. colors_subtitle.push({
  335. name: toColor[i],
  336. color: colorMake(i)
  337. });
  338. }*/
  339. //Bottom graph
  340. dataTranspose = transposeData(data1, false);
  341. reloadCheckBoxes(dataTranspose, checkboxescities, "#parallelcoordinates_area_cities");
  342. drawParallelCoords(dataTranspose, "#parallelcoordinates_area_cities");
  343. //Top graph
  344. dataTranspose2 = transposeData(data2, false);
  345. reloadCheckBoxes(dataTranspose2, checkboxes, "#parallelcoordinates_area");
  346. drawParallelCoords(dataTranspose2, "#parallelcoordinates_area");
  347. var s1 = d3.select("#parallel_coordinates_subtitle").selectAll('div')
  348. .data(colors_subtitle)
  349. .enter()
  350. .append("div")
  351. .append("text");
  352. s1Attr = s1.text(function (d) {
  353. return d.name;
  354. })
  355. .style("color", function (d) {
  356. return d.color;
  357. })
  358. .style("font-weight", "bold");
  359. //Treemap code
  360. var margin = {
  361. top: 25,
  362. right: 0,
  363. bottom: 0,
  364. left: 0
  365. },
  366. width = window.innerWidth * 0.95,
  367. height = window.innerHeight * 0.6,
  368. formatNumber = d3.format(",d"),
  369. transitioning,
  370. hovering;
  371. var tooltip = d3.select("body")
  372. .append("div")
  373. .style("position", "absolute")
  374. .style("z-index", "10")
  375. .style("visibility", "hidden")
  376. .style("color", "white")
  377. .style("padding", "8px")
  378. .style("background-color", "rgba(0, 0, 0, 0.75)")
  379. .style("border-radius", "6px")
  380. .style("font", "12px sans-serif")
  381. .text("tooltip");
  382. var x = d3.scaleLinear()
  383. .domain([0, width])
  384. .range([0, width]);
  385. var y = d3.scaleLinear()
  386. .domain([0, height - margin.top - margin.bottom])
  387. .range([0, height - 3 * margin.top - margin.bottom]);
  388. var c = [];
  389. function q(d) {
  390. for (var i = 0; i < d.children.length; i++) {
  391. c[i] = d.children[i].value;
  392. }
  393. return c.sort(function (a, b) {
  394. return (a - b);
  395. });
  396. }
  397. q(data);
  398. var maxValue = c[c.length - 1];
  399. var minValue = maxValue / 9;
  400. var color = d3.scaleThreshold()
  401. .domain([minValue, minValue * 2, minValue * 3, minValue * 4, minValue * 5, minValue * 6, minValue * 7, minValue * 8])
  402. .range(d3.schemeBlues[9]);
  403. var format = d3.format(",d");
  404. var createTreemap;
  405. var treemap, grandparent;
  406. updateDrillDown();
  407. function updateDrillDown() {
  408. if (treemap) {
  409. treemap.selectAll("*").remove();
  410. } else {
  411. treemap = d3.select("#treemap2_area").append("svg")
  412. .attr("width", width + margin.left + margin.right)
  413. .attr("height", height + margin.bottom + margin.top)
  414. //.style("margin-left", -margin.left + "px")
  415. //.style("margin.right", -margin.right + "px")
  416. .append("g")
  417. .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
  418. .style("shape-rendering", "crispEdges");
  419. grandparent = treemap.append("g")
  420. .attr("class", "grandparent");
  421. grandparent.append("rect")
  422. .style("fill", "#ADEAEA")
  423. .style("padding", "1px")
  424. .attr("y", -margin.top)
  425. .attr("width", width)
  426. .attr("height", margin.top);
  427. grandparent.append("text")
  428. .attr("x", 6)
  429. .attr("y", 6 - margin.top)
  430. .attr("dy", ".75em");
  431. createTreemap = d3.treemap()
  432. .tile(d3.treemapBinary)
  433. .size([width, height - 10])
  434. .round(false)
  435. .paddingInner(0);
  436. }
  437. var root = d3.hierarchy(data)
  438. .eachBefore(function (d) {
  439. d.id = (d.parent ? d.parent.id + "." : "") + d.data.name;
  440. })
  441. .sum(function (d) {
  442. return d.value;
  443. })
  444. .sort(function (a, b) {
  445. return b.height - a.height || b.value - a.value;
  446. });
  447. initialize(root);
  448. accumulate(root);
  449. layout(root);
  450. createTreemap(root);
  451. display(root);
  452. };
  453. function initialize(root) {
  454. root.x = root.y = 0;
  455. root.x1 = width;
  456. root.y1 = height;
  457. root.depth = 0;
  458. }
  459. function accumulate(d) {
  460. return (d._children = d.children) ?
  461. d.value = d.children.reduce(function (p, v) {
  462. return p + accumulate(v);
  463. }, 0) : d.value;
  464. }
  465. function layout(d) {
  466. if (d._children) {
  467. d._children.forEach(function (c) {
  468. c.x0 = d.x0 + c.x0 * d.x1;
  469. c.y0 = d.y0 + c.y0 * d.y1;
  470. c.x1 *= (d.x1 - d.x0);
  471. c.y1 *= (d.y1 - d.y0);
  472. c.parent = d;
  473. layout(c);
  474. });
  475. }
  476. }
  477. function display(d) {
  478. grandparent
  479. .datum(d.parent)
  480. .on("click", transition)
  481. .select("text")
  482. .text(name(d));
  483. var g1 = treemap.insert("g", ".grandparent")
  484. .datum(d)
  485. .attr("class", "depth");
  486. var g = g1.selectAll("g")
  487. .data(d._children)
  488. .enter().append("g").style("opacity", 1);
  489. g.filter(function (d) {
  490. return d._children;
  491. })
  492. .classed("children", true)
  493. .on("click", transition);
  494. var children = g.selectAll(".child")
  495. .data(function (d) {
  496. return d._children || [d];
  497. })
  498. .enter().append("g") //SHOW CHILDREN FROM TREEMAP VIEW
  499. .style("opacity", 0.7); // DEFINES THE OPACITY OF THE INTERNAL NODES
  500. children.append("rect")
  501. .attr("class", "child")
  502. .call(rect)
  503. .append("title")
  504. .text(function (d) {
  505. return d.data.name + " (" + formatNumber(d.data.value) + ")";
  506. });
  507. children.append("text")
  508. .attr("class", "ctext").style("font-size", "14px")
  509. .text(function (d) {
  510. return d.data.name;
  511. })
  512. .call(text2);
  513. g.append("rect")
  514. .attr("class", "parent")
  515. .call(rect);
  516. //MOUSEOVER: SHOW CHILDREN OF THE NODES
  517. g.selectAll("rect").classed("parent", true).on("mouseover", function (d) {
  518. d3.select(this).style("opacity", 0.3); //.
  519. tooltip.html(d.data.name + "<br/>" + formatNumber(d.data.value));
  520. tooltip.transition();
  521. return tooltip.style("visibility", "visible").style("opacity", 1);
  522. //children.select("g").style("opacity", 1);
  523. });
  524. //MOUSEOUT: HIDE CHILDREN OF THE NODES
  525. g.selectAll("rect").classed("parent", false).on("mouseout", function () {
  526. d3.select(this).style("opacity", 1);
  527. return tooltip.style("visibility", "hidden");
  528. });
  529. g.selectAll("rect").classed("parent", false).on("mousemove", function () {
  530. d3.select(this).style("opacity", 0.3);
  531. tooltip.style("opacity", 1);
  532. return tooltip.style("top", (d3.event.pageY - 10) + "px").style("left", (d3.event.pageX + 10) + "px");
  533. });
  534. var t = g.append("text")
  535. .attr("class", "ptext").style("font-weight", "bold")
  536. .attr("dy", ".75em");
  537. t.append("tspan")
  538. .attr("font-weight", "bold")
  539. .text(function (d) {
  540. return d.data.name;
  541. });
  542. t.append("tspan")
  543. .attr("dy", "1.0em")
  544. .text(function (d) {
  545. return formatNumber(d.data.value);
  546. });
  547. t.call(text);
  548. g.selectAll("rect")
  549. .style("fill", function (d) {
  550. return color(d.data.value);
  551. });
  552. function transition(d) {
  553. // Handle Parcoords data
  554. Shiny.onInputChange("scope", scopes[d.depth]);
  555. Shiny.onInputChange("name", d.data.name);
  556. Shiny.addCustomMessageHandler("treemap2_values_change", function (message) {
  557. dataset1 = message[0];
  558. dataset2 = message[1];
  559. if (d.data.children[0]) {
  560. nextScope = scopes[d.depth +1];
  561. /*var toColorDup = dataset1[nextScope];
  562. toColor = toColorDup.filter(function (elem, index, self) {
  563. return index === self.indexOf(elem);
  564. });*/
  565. colors_subtitle = [];
  566. /*for (var i = 0; i < toColor.length; i++) {
  567. colors_subtitle.push({
  568. name: toColor[i],
  569. color: colorMake(i)
  570. });
  571. }*/
  572. d3.select("#parallel_coordinates_subtitle").selectAll('div').remove();
  573. s1 = d3.select("#parallel_coordinates_subtitle").selectAll('div')
  574. .data(colors_subtitle)
  575. .enter()
  576. .append("div")
  577. .append("text");
  578. s1Attr = s1.text(function (d) {
  579. return d.name;
  580. })
  581. .style("color", function (d) {
  582. return d.color;
  583. })
  584. .style("font-weight", "bold");
  585. } else {
  586. nextScope = "municipio";
  587. }
  588. dataset1 = transposeData(dataset1, false);
  589. //dataset2 = transposeData(dataset2);
  590. d3.select("#parallelcoordinates_switch_cities").selectAll("*").remove();
  591. reloadCheckBoxes(dataset1, checkboxescities, "#parallelcoordinates_area_cities");
  592. d3.select("#parallelcoordinates_area_cities svg").remove();
  593. drawParallelCoords(dataset1, "#parallelcoordinates_area_cities");
  594. dataset2 = transposeData(dataset2, false);
  595. d3.select("#parallelcoordinates_switch").selectAll("*").remove();
  596. reloadCheckBoxes(dataset2, checkboxes, "#parallelcoordinates_area");
  597. d3.select("#parallelcoordinates_area svg").remove();
  598. drawParallelCoords(dataset2, "#parallelcoordinates_area");
  599. });
  600. if (transitioning || !d) return;
  601. if (transitioning || !d) return;
  602. transitioning = true;
  603. var g2 = display(d),
  604. t1 = g1.transition().duration(750),
  605. t2 = g2.transition().duration(750);
  606. // Update the domain only after entering new elements.
  607. x.domain([d.x0, d.x0 + (d.x1 - d.x0)]);
  608. y.domain([d.y0, d.y0 + (d.y1 - d.y0)]);
  609. // Enable anti-aliasing during the transition.
  610. treemap.style("shape-rendering", null);
  611. // Draw child nodes on top of parent nodes.
  612. //treemap.selectAll(".depth").sort(function(a, b) {
  613. //console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
  614. //return a.depth - b.depth; });
  615. // Fade-in entering text.
  616. g2.selectAll("text").style("fill-opacity", 0);
  617. // Transition to the new view.
  618. t1.selectAll(".ptext").call(text).style("fill-opacity", 0).style("font-weight", "bold"); //.style("font-size", "12px");
  619. t2.selectAll(".ptext").call(text).style("fill-opacity", 1).style("font-weight", "bold"); //.style("font-size", "12px");
  620. t1.selectAll(".ctext").call(text2).style("fill-opacity", 0);
  621. t2.selectAll(".ctext").call(text2).style("fill-opacity", 1);
  622. t1.selectAll("rect").call(rect);
  623. t2.selectAll("rect").call(rect);
  624. // Remove the old node when the transition is finished.
  625. t1.remove().on("end", function () {
  626. treemap.style("shape-rendering", "crispEdges");
  627. transitioning = false;
  628. });
  629. }
  630. return g;
  631. }
  632. function make_title(d) {
  633. return d ? d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  634. }
  635. function text(text) {
  636. text.selectAll("tspan")
  637. .attr("x", function (d) {
  638. return x(d.x0) + 5;
  639. });
  640. text.attr("x", function (d) {
  641. return x(d.x0) + 5;
  642. })
  643. .attr("y", function (d) {
  644. return y(d.y0) + 3;
  645. })
  646. .style("opacity", function (d) {
  647. var w = x(d.x1) - x(d.x0);
  648. return this.getComputedTextLength() <= w ? 1 : 0;
  649. })
  650. .style("font-size", "10px");
  651. }
  652. function text2(text) {
  653. text
  654. .attr("x", function (d) {
  655. return x(d.x1) - this.getComputedTextLength() - 2;
  656. })
  657. .attr("y", function (d) {
  658. return y(d.y1) - 2;
  659. })
  660. .style("opacity", function (d) {
  661. var w = x(d.x1) - x(d.x0);
  662. return this.getComputedTextLength() <= w ? 1 : 0;
  663. })
  664. .style("font-size", "14px");
  665. }
  666. function rect(rect) {
  667. rect.attr("x", function (d) {
  668. return x(d.x0);
  669. })
  670. .attr("y", function (d) {
  671. return y(d.y0);
  672. })
  673. .attr("width", function (d) {
  674. var w = x(d.x1) - x(d.x0) - 1;
  675. return w;
  676. })
  677. .attr("height", function (d) {
  678. var h = y(d.y1) - y(d.y0);
  679. return h;
  680. })
  681. .style("stroke", "darkblue")
  682. .style("stroke-width", 1);
  683. }
  684. function name(d) {
  685. return d.parent ? name(d.parent) + " -> " + d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  686. }
  687. });