treemap2.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. Shiny.addCustomMessageHandler("treemap2_values", function (message) {
  2. console.log(message);
  3. var data = message[0];
  4. var data1 = message[1];
  5. var nextScope = "regiao";
  6. //Parallel Coordinates code
  7. var traits = ["name", 'value', "Hora", "Dia", "Idade", "Sexo", "Causador"];
  8. // ,"CNAE"
  9. var parcoordstable = d3.select("#parallelcoordinates_table table");
  10. function reloadParallelCords(arrayData) {
  11. console.log(arrayData);
  12. dragging = {};
  13. var background;
  14. var m = [50, 50, 20, 100],
  15. w = window.innerWidth * 0.7 - m[1] - m[3],
  16. h = window.innerHeight * 0.5 - m[0] - m[2];
  17. var line = d3.line(),
  18. axis = d3.axisLeft(),
  19. foreground;
  20. var parcoords = d3.select("#parallelcoordinates_area").append("svg")
  21. .attr("width", w + m[1] + m[3])
  22. .attr("height", h + m[0] + m[2])
  23. .append("g")
  24. .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
  25. // Create a scale and brush for each trait
  26. var x = d3.scalePoint().domain(traits).range([0, w]),
  27. y = {};
  28. traits.forEach(function (col) {
  29. if (col == "Dia") {
  30. var data = ["seg", "ter", "qua", "qui", "sex", "sáb", "dom"];
  31. y[col] = d3.scalePoint()
  32. .domain(data)
  33. .range([h, 0]);
  34. } else if (col == "name") {
  35. var data = [];
  36. arrayData.forEach(function (row) {
  37. data.push(row[col]);
  38. })
  39. y[col] = d3.scalePoint()
  40. .domain(data)
  41. .range([h, 0]);
  42. } else if (col == "Causador" || col == "CNAE") {
  43. var data = [];
  44. arrayData.forEach(function (row) {
  45. if (!arrayData.includes(row[col])) {
  46. data.push(row[col]);
  47. }
  48. })
  49. y[col] = d3.scalePoint()
  50. .domain(data)
  51. .range([h, 0]);
  52. } else {
  53. arrayData.forEach(function (p) {
  54. p[col] = +p[col];
  55. });
  56. if (col == "Sexo") {
  57. y[col] = d3.scaleLinear()
  58. .domain(
  59. [0, 1]
  60. )
  61. .range([h, 0]);
  62. } else {
  63. y[col] = d3.scaleLinear()
  64. .domain(d3.extent(arrayData, function (p) {
  65. return p[col];
  66. }))
  67. .range([h, 0]);
  68. }
  69. }
  70. //Create Brush
  71. y[col].brush = d3.brushY()
  72. .extent([
  73. [-7, y[col].range()[1]],
  74. [7, y[col].range()[0]]
  75. ])
  76. .on("start", brushstart)
  77. .on("brush", brushView)
  78. .on("end", brush);
  79. });
  80. //Append Background
  81. background = parcoords.append("g")
  82. .attr("class", "background")
  83. .selectAll("path")
  84. .data(arrayData)
  85. .enter().append("path")
  86. .attr("d", path);
  87. //Add Foreground Lines
  88. foreground = parcoords.append("g")
  89. .attr("class", "foreground")
  90. .selectAll("path")
  91. .data(arrayData)
  92. .enter().append("path")
  93. .attr("d", path)
  94. .attr("class", function (d) {
  95. return d.name;
  96. });
  97. // Add a group element for each trait.
  98. var g = parcoords.selectAll(".trait")
  99. .data(traits)
  100. .enter()
  101. .append("svg:g")
  102. .attr("class", "trait")
  103. .attr("transform", function (d) {
  104. return "translate(" + x(d) + ")";
  105. })
  106. .call(d3.drag()
  107. .subject(function (d) {
  108. return {
  109. x: x(d)
  110. };
  111. })
  112. .on("start", function (d) {
  113. dragging[d] = x(d);
  114. background.attr("visibility", "hidden");
  115. })
  116. .on("drag", function (d) {
  117. //+10 e -10 para mudar com o primeiro e o ultimo
  118. dragging[d] = Math.min(w + 10, Math.max(-10, d3.event.x));
  119. foreground.attr("d", path);
  120. traits.sort(function (a, b) {
  121. return position(a) - position(b);
  122. });
  123. x.domain(traits);
  124. g.attr("transform", function (d) {
  125. return "translate(" + position(d) + ")";
  126. })
  127. })
  128. .on("end", function (d) {
  129. delete dragging[d];
  130. transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
  131. transition(foreground).attr("d", path);
  132. background
  133. .attr("d", path)
  134. .transition()
  135. .delay(500)
  136. .duration(0)
  137. .attr("visibility", null);
  138. })
  139. );
  140. //Gradiente do eixo Sexo
  141. var defs = parcoords.append("defs");
  142. var gradient = defs.append("linearGradient")
  143. .attr("id", "svgGradient")
  144. .attr("x1", "50%")
  145. .attr("x2", "100%")
  146. .attr("y1", "50%")
  147. .attr("y2", "100%");
  148. gradient.append("stop")
  149. .attr('class', 'start')
  150. .attr("offset", "0%")
  151. .attr("stop-color", "blue")
  152. .attr("stop-opacity", 1);
  153. gradient.append("stop")
  154. .attr('class', 'end')
  155. .attr("offset", "100%")
  156. .attr("stop-color", "red")
  157. .attr("stop-opacity", 1);
  158. //Eixos
  159. g.append("svg:g")
  160. .attr("class", "axis")
  161. .each(function (col) {
  162. if (col == "Sexo") {
  163. d3.select(this)
  164. .attr("class", "eixoSexo")
  165. .call(d3.axisLeft(y[col]))
  166. .selectAll("text").remove();
  167. d3.select(this)
  168. .call(d3.axisLeft(y[col])
  169. .ticks(3));
  170. d3.select(this)
  171. .append("svg:text").text("M")
  172. .attr("x", -9)
  173. .attr("y", 10);
  174. d3.select(this)
  175. .append("svg:text").text("F")
  176. .attr("x", -9)
  177. .attr("y", h + 2);
  178. } else if (col == "Hora") {
  179. d3.select(this).call(d3.axisLeft(y[col]).ticks(18));
  180. } else if (col == "Causador") {
  181. // ALT
  182. d3.select(this).call(d3.axisLeft(y[col])).selectAll("text")._groups[0].forEach(function (p) {
  183. p.innerHTML = p.innerHTML.split(" ")[0];
  184. })
  185. } else if (col == "Dia") {
  186. // ALT
  187. d3.select(this).call(d3.axisLeft(y[col])).selectAll("text")._groups[0].forEach(function (p) {
  188. p.innerHTML = p.innerHTML.split(" ")[0];
  189. })
  190. } else {
  191. d3.select(this).call(d3.axisLeft(y[col]));
  192. }
  193. })
  194. // Axis titles
  195. .append("svg:text")
  196. .attr("text-anchor", "middle")
  197. .attr("y", -15)
  198. .text(function (col) {
  199. if (col != "value" && col != "name") {
  200. return col;
  201. } else if (col == "value") {
  202. return "Acidentes"
  203. } else if (col == "name") {
  204. return "Escopo"
  205. }
  206. });
  207. // Add a brush for each axis.
  208. g.append("svg:g")
  209. .attr("class", "brush")
  210. .each(function (d) {
  211. d3.select(this).call(y[d].brush);
  212. })
  213. .selectAll("rect")
  214. .attr("x", -8)
  215. .attr("width", 16);
  216. function position(d) {
  217. var v = dragging[d];
  218. return v == null ? x(d) : v;
  219. }
  220. function transition(g) {
  221. return g.transition().duration(500);
  222. }
  223. // Returns the path for a given data point.
  224. function path(d) {
  225. return line(traits.map(function (p) {
  226. return [position(p), y[p](d[p])];
  227. }));
  228. }
  229. // Brush functions
  230. function brushstart() {
  231. d3.event.sourceEvent.stopPropagation();
  232. }
  233. function brushView() {
  234. var actives = [];
  235. //filter brushed extents
  236. parcoords.selectAll(".brush")
  237. .filter(function (d) {
  238. return d3.brushSelection(this);
  239. })
  240. .each(function (d) {
  241. actives.push({
  242. dimension: d,
  243. extent: d3.brushSelection(this)
  244. });
  245. dim = actives[0].dimension;
  246. });
  247. //set un-brushed foreground line disappear
  248. foreground.classed("fade", function (d, i) {
  249. return !actives.every(function (active) {
  250. var dim = active.dimension;
  251. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[
  252. 1];
  253. });
  254. });
  255. }
  256. function brush() {
  257. var actives = [];
  258. //filter brushed extents
  259. parcoords.selectAll(".brush")
  260. .filter(function (d) {
  261. return d3.brushSelection(this);
  262. })
  263. .each(function (d) {
  264. actives.push({
  265. dimension: d,
  266. extent: d3.brushSelection(this)
  267. });
  268. var selected = arrayData.filter(function (d) {
  269. return actives.every(function (active) {
  270. var dim = active.dimension;
  271. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
  272. });
  273. });
  274. hideTableColNames();
  275. d3.selectAll(".row_data").remove();
  276. showTableColNames();
  277. selected.forEach(function (d) {
  278. var row = parcoordstable.append("tr")
  279. .attr("class", "row_data")
  280. traits.forEach(
  281. function (string) {
  282. row.append("td")
  283. .text(d[string]);
  284. }
  285. )
  286. })
  287. });
  288. foreground.classed("fade", function (d, i) {
  289. return !actives.every(function (active) {
  290. var dim = active.dimension;
  291. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
  292. });
  293. });
  294. }
  295. function hideTableColNames() {
  296. parcoordstable.style("visibility", "hidden");
  297. }
  298. function showTableColNames() {
  299. parcoordstable.style("visibility", "visible");
  300. }
  301. }
  302. function toggleAxis(checkBox, trait, data) {
  303. if (checkBox.checked == true) {
  304. if (!traits.includes(trait))
  305. traits.push(trait);
  306. }
  307. if (checkBox.checked == false) {
  308. var i = traits.indexOf(trait);
  309. traits.splice(i, 1);
  310. }
  311. d3.select("#parallelcoordinates_area svg").remove();
  312. reloadParallelCords(data);
  313. }
  314. var checkboxes = d3.select("#parallelcoordinates_area")
  315. .append("div")
  316. .attr("id", "parallelcoordinates_switch");
  317. function reloadCheckBoxes(data) {
  318. traits.forEach(function (trait) {
  319. checkboxes.append("input")
  320. .attr("type", "checkbox")
  321. .attr("id", trait)
  322. .attr("value", trait)
  323. .attr("checked", true)
  324. .on("click", function () {
  325. toggleAxis(this, trait, data);
  326. });
  327. checkboxes.append("text")
  328. .text(trait);
  329. });
  330. }
  331. reloadCheckBoxes(data.children);
  332. reloadParallelCords(data.children);
  333. // ===========================================================================
  334. // Parallel Coords Cities Code
  335. // ============================================================================
  336. function reloadParallelCordsCities(arrayData) {
  337. dragging = {};
  338. var background;
  339. var m = [40, 50, 20, 150],
  340. w = window.innerWidth * 0.7 - m[1] - m[3],
  341. h = window.innerHeight * 0.5 - m[0] - m[2];
  342. var line = d3.line(),
  343. axis = d3.axisLeft(),
  344. foreground;
  345. var parcoords = d3.select("#parallelcoordinates_area_cities").append("svg")
  346. .attr("width", w + m[1] + m[3])
  347. .attr("height", h + m[0] + m[2])
  348. .append("g")
  349. .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
  350. // Create a scale and brush for each trait
  351. var x = d3.scalePoint().domain(traits).range([0, w]),
  352. y = {};
  353. traits.forEach(function (col) {
  354. if (col == "Dia") {
  355. var data = ["seg", "ter", "qua", "qui", "sex", "sáb", "dom"];
  356. y[col] = d3.scalePoint()
  357. .domain(data)
  358. .range([h, 0]);
  359. } else if (col == "name") {
  360. var data = [];
  361. arrayData.forEach(function (row) {
  362. data.push(row[col]);
  363. })
  364. y[col] = d3.scalePoint()
  365. .domain(data)
  366. .range([h, 0]);
  367. } else if (col == "Causador" || col == "CNAE") {
  368. var data = [];
  369. arrayData.forEach(function (row) {
  370. if (!arrayData.includes(row[col])) {
  371. data.push(row[col]);
  372. }
  373. })
  374. y[col] = d3.scalePoint()
  375. .domain(data)
  376. .range([h, 0]);
  377. } else {
  378. arrayData.forEach(function (p) {
  379. p[col] = +p[col];
  380. });
  381. if (col == "Sexo") {
  382. y[col] = d3.scaleLinear()
  383. .domain(
  384. [0, 1]
  385. )
  386. .range([h, 0]);
  387. } else {
  388. y[col] = d3.scaleLinear()
  389. .domain(d3.extent(arrayData, function (p) {
  390. return p[col];
  391. }))
  392. .range([h, 0]);
  393. }
  394. }
  395. //Create Brush
  396. y[col].brush = d3.brushY()
  397. .extent([
  398. [-7, y[col].range()[1]],
  399. [7, y[col].range()[0]]
  400. ])
  401. .on("start", brushstart)
  402. .on("brush", brushView)
  403. .on("end", brush);
  404. });
  405. //Append Background
  406. background = parcoords.append("g")
  407. .attr("class", "background")
  408. .selectAll("path")
  409. .data(arrayData)
  410. .enter().append("path")
  411. .attr("d", path);
  412. //Add Foreground Lines
  413. foreground = parcoords.append("g")
  414. .attr("class", "foreground")
  415. .selectAll("path")
  416. .data(arrayData)
  417. .enter().append("path")
  418. .attr("d", path)
  419. .style("stroke", function (d) {
  420. return colorMake(toColor.indexOf(d[nextScope]));
  421. });
  422. // Add a group element for each trait.
  423. var g = parcoords.selectAll(".trait")
  424. .data(traits)
  425. .enter()
  426. .append("svg:g")
  427. .attr("class", "trait")
  428. .attr("transform", function (d) {
  429. return "translate(" + x(d) + ")";
  430. })
  431. .call(d3.drag()
  432. .subject(function (d) {
  433. return {
  434. x: x(d)
  435. };
  436. })
  437. .on("start", function (d) {
  438. dragging[d] = x(d);
  439. background.attr("visibility", "hidden");
  440. })
  441. .on("drag", function (d) {
  442. //+10 e -10 para mudar com o primeiro e o ultimo
  443. dragging[d] = Math.min(w + 10, Math.max(-10, d3.event.x));
  444. foreground.attr("d", path);
  445. traits.sort(function (a, b) {
  446. return position(a) - position(b);
  447. });
  448. x.domain(traits);
  449. g.attr("transform", function (d) {
  450. return "translate(" + position(d) + ")";
  451. })
  452. })
  453. .on("end", function (d) {
  454. delete dragging[d];
  455. transition(d3.select(this)).attr("transform", "translate(" + x(d) + ")");
  456. transition(foreground).attr("d", path);
  457. background
  458. .attr("d", path)
  459. .transition()
  460. .delay(500)
  461. .duration(0)
  462. .attr("visibility", null);
  463. })
  464. );
  465. //Gradiente do eixo Sexo
  466. var defs = parcoords.append("defs");
  467. var gradient = defs.append("linearGradient")
  468. .attr("id", "svgGradient")
  469. .attr("x1", "50%")
  470. .attr("x2", "100%")
  471. .attr("y1", "50%")
  472. .attr("y2", "100%");
  473. gradient.append("stop")
  474. .attr('class', 'start')
  475. .attr("offset", "0%")
  476. .attr("stop-color", "blue")
  477. .attr("stop-opacity", 1);
  478. gradient.append("stop")
  479. .attr('class', 'end')
  480. .attr("offset", "100%")
  481. .attr("stop-color", "red")
  482. .attr("stop-opacity", 1);
  483. //Axis
  484. g.append("svg:g")
  485. .attr("class", "axis")
  486. .each(function (col) {
  487. if (col == "Sexo") {
  488. d3.select(this)
  489. .attr("class", "eixoSexo")
  490. .call(d3.axisLeft(y[col]))
  491. .selectAll("text").remove();
  492. d3.select(this)
  493. .call(d3.axisLeft(y[col])
  494. .ticks(3));
  495. d3.select(this)
  496. .append("svg:text").text("M")
  497. .attr("x", -9)
  498. .attr("y", 10);
  499. d3.select(this)
  500. .append("svg:text").text("F")
  501. .attr("x", -9)
  502. .attr("y", h + 2);
  503. } else if (col == "Hora") {
  504. d3.select(this).call(d3.axisLeft(y[col]).ticks(18));
  505. } else if (col == "Causador") {
  506. // ALT
  507. d3.select(this).call(d3.axisLeft(y[col])).selectAll("text")._groups[0].forEach(function (p) {
  508. p.innerHTML = p.innerHTML.split(" ")[0];
  509. })
  510. } else if (col == "Dia") {
  511. // ALT
  512. d3.select(this).call(d3.axisLeft(y[col])).selectAll("text")._groups[0].forEach(function (p) {
  513. p.innerHTML = p.innerHTML.split(" ")[0];
  514. })
  515. } else {
  516. d3.select(this).call(d3.axisLeft(y[col]));
  517. }
  518. })
  519. // Axis Titles
  520. .append("svg:text")
  521. .attr("text-anchor", "middle")
  522. .attr("y", -15)
  523. .text(function (col) {
  524. if (col != "value" && col != "name") {
  525. return col;
  526. } else if (col == "value") {
  527. return "Acidentes"
  528. } else if (col == "name") {
  529. return "Escopo Cities"
  530. }
  531. });
  532. // Add a brush for each axis.
  533. g.append("svg:g")
  534. .attr("class", "brush")
  535. .each(function (d) {
  536. d3.select(this).call(y[d].brush);
  537. })
  538. .selectAll("rect")
  539. .attr("x", -8)
  540. .attr("width", 16);
  541. function position(d) {
  542. var v = dragging[d];
  543. return v == null ? x(d) : v;
  544. }
  545. function transition(g) {
  546. return g.transition().duration(500);
  547. }
  548. // Returns the path for a given data point.
  549. function path(d) {
  550. return line(traits.map(function (p) {
  551. return [position(p), y[p](d[p])];
  552. }));
  553. }
  554. // BRUSH FUNCTIONS
  555. function brushstart() {
  556. d3.event.sourceEvent.stopPropagation();
  557. }
  558. function brushView() {
  559. var actives = [];
  560. //filter brushed extents
  561. parcoords.selectAll(".brush")
  562. .filter(function (d) {
  563. return d3.brushSelection(this);
  564. })
  565. .each(function (d) {
  566. actives.push({
  567. dimension: d,
  568. extent: d3.brushSelection(this)
  569. });
  570. dim = actives[0].dimension;
  571. });
  572. //set un-brushed foreground line disappear
  573. foreground.classed("fade", function (d, i) {
  574. return !actives.every(function (active) {
  575. var dim = active.dimension;
  576. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[
  577. 1];
  578. });
  579. });
  580. }
  581. function brush() {
  582. var actives = [];
  583. //filter brushed extents
  584. parcoords.selectAll(".brush")
  585. .filter(function (d) {
  586. return d3.brushSelection(this);
  587. })
  588. .each(function (d) {
  589. actives.push({
  590. dimension: d,
  591. extent: d3.brushSelection(this)
  592. });
  593. var selected = arrayData.filter(function (d) {
  594. return actives.every(function (active) {
  595. var dim = active.dimension;
  596. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
  597. });
  598. });
  599. hideTableColNames();
  600. d3.selectAll(".row_data").remove();
  601. showTableColNames();
  602. selected.forEach(function (d) {
  603. var row = parcoordstable.append("tr")
  604. .attr("class", "row_data")
  605. traits.forEach(
  606. function (string) {
  607. row.append("td")
  608. .text(d[string]);
  609. }
  610. )
  611. })
  612. });
  613. foreground.classed("fade", function (d, i) {
  614. return !actives.every(function (active) {
  615. var dim = active.dimension;
  616. return active.extent[0] <= y[dim](d[dim]) && y[dim](d[dim]) <= active.extent[1];
  617. });
  618. });
  619. }
  620. function hideTableColNames() {
  621. parcoordstable.style("visibility", "hidden");
  622. }
  623. function showTableColNames() {
  624. parcoordstable.style("visibility", "visible");
  625. }
  626. }
  627. function toggleAxisCities(checkBox, trait, data) {
  628. if (checkBox.checked == true) {
  629. if (!traits.includes(trait))
  630. traits.push(trait);
  631. }
  632. if (checkBox.checked == false) {
  633. var i = traits.indexOf(trait);
  634. traits.splice(i, 1);
  635. }
  636. d3.select("#parallelcoordinates_area_cities svg").remove();
  637. reloadParallelCordsCities(data);
  638. }
  639. var checkboxescities = d3.select("#parallelcoordinates_area_cities")
  640. .append("div")
  641. .attr("id", "parallelcoordinates_switch_cities");
  642. function reloadCheckBoxesCities(data) {
  643. traits.forEach(function (trait) {
  644. checkboxescities.append("input")
  645. .attr("type", "checkbox")
  646. .attr("id", trait)
  647. .attr("value", trait)
  648. .attr("checked", true)
  649. .on("click", function () {
  650. toggleAxisCities(this, trait, data);
  651. });
  652. checkboxescities.append("text")
  653. .text(trait);
  654. });
  655. }
  656. // TRANSPOSE OF DATA THAT COME FROM R READ OF CSV
  657. var values = ["municipio", "acidentes_municipio", "hora_municipio", "diaSemana_municipio", "idadeMedia_municipio", "sexoPorcentagem_municipio", "dsAgenteModa_municipio", "classeCnaeModa_municipio"];
  658. var values2 = ["regiao", "uf", "mesorregiao", "microrregiao"];
  659. function transposeData(d) {
  660. var len = d.pais.length;
  661. var datainsert = [];
  662. var dataTransposed = [];
  663. var i, j;
  664. for (i = 0; i < len; i++) {
  665. for (j = 0; j < values.length; j++) {
  666. datainsert[traits[j]] = d[values[j]][i];
  667. };
  668. values2.forEach(function (value2) {
  669. datainsert[value2] = d[value2][i];
  670. })
  671. dataTransposed.push(datainsert);
  672. datainsert = [];
  673. }
  674. return dataTransposed;
  675. }
  676. var colorMake = color = d3.scaleOrdinal().range(d3.schemeCategory10);
  677. var toColorDup = data1[nextScope];
  678. toColor = toColorDup.filter(function (elem, index, self) {
  679. return index === self.indexOf(elem);
  680. });
  681. var i0;
  682. var colors_subtitle = [];
  683. for (i0 = 0; i0 < toColor.length; i0++) {
  684. colors_subtitle.push({
  685. name: toColor[i0],
  686. color: colorMake(i0)
  687. });
  688. }
  689. dataTranspose = transposeData(data1);
  690. reloadCheckBoxesCities(dataTranspose);
  691. reloadParallelCordsCities(dataTranspose);
  692. var s1 = d3.select("#parallel_coordinates_subtitle").selectAll('div').data(colors_subtitle).enter().append("div").append("text");
  693. s1Attr = s1.text(function (d) {
  694. return d.name;
  695. })
  696. .style("color", function (d) {
  697. return d.color;
  698. })
  699. .style("font-weight", "bold");
  700. //Treemap code
  701. var margin = {
  702. top: 25,
  703. right: 0,
  704. bottom: 0,
  705. left: 0
  706. },
  707. width = window.innerWidth * 0.55,
  708. height = window.innerHeight * 0.5,
  709. formatNumber = d3.format(",d"),
  710. transitioning,
  711. hovering;
  712. var tooltip = d3.select("body")
  713. .append("div")
  714. .style("position", "absolute")
  715. .style("z-index", "10")
  716. .style("visibility", "hidden")
  717. .style("color", "white")
  718. .style("padding", "8px")
  719. .style("background-color", "rgba(0, 0, 0, 0.75)")
  720. .style("border-radius", "6px")
  721. .style("font", "12px sans-serif")
  722. .text("tooltip");
  723. var x = d3.scaleLinear()
  724. .domain([0, width])
  725. .range([0, width]);
  726. var y = d3.scaleLinear()
  727. .domain([0, height - margin.top - margin.bottom])
  728. .range([0, height - 3 * margin.top - margin.bottom]);
  729. var c = [];
  730. function q(d) {
  731. for (var i = 0; i < d.children.length; i++) {
  732. c[i] = d.children[i].value;
  733. //Do something
  734. }
  735. return c.sort(function (a, b) {
  736. return (a - b);
  737. });
  738. }
  739. q(data);
  740. var maxValue = c[c.length - 1];
  741. var minValue = maxValue / 9;
  742. var color = d3.scaleThreshold()
  743. .domain([minValue, minValue * 2, minValue * 3, minValue * 4, minValue * 5, minValue * 6, minValue * 7, minValue * 8])
  744. .range(d3.schemeBlues[9]);
  745. var format = d3.format(",d");
  746. var createTreemap;
  747. var treemap, grandparent;
  748. updateDrillDown();
  749. function updateDrillDown() {
  750. if (treemap) {
  751. treemap.selectAll("*").remove();
  752. } else {
  753. // var treemap = d3.layout.treemap()
  754. // .children(function(d, depth) { return depth ? null : d._children; })
  755. // .sort(function(a, b) { return a.value - b.value; })
  756. // .ratio(height / width * 0.5 * (1 + Math.sqrt(5)))
  757. // .round(false);
  758. treemap = d3.select("#treemap2_area").append("svg")
  759. .attr("width", width - margin.left - margin.right)
  760. .attr("height", height - margin.bottom - margin.top)
  761. .style("margin-left", -margin.left + "px")
  762. .style("margin.right", -margin.right + "px")
  763. .append("g")
  764. .attr("transform", "translate(" + margin.left + "," + margin.top + ")")
  765. .style("shape-rendering", "crispEdges");
  766. grandparent = treemap.append("g")
  767. .attr("class", "grandparent");
  768. grandparent.append("rect")
  769. .style("fill", "#ADEAEA")
  770. .style("padding", "1px")
  771. .attr("y", -margin.top)
  772. .attr("width", width)
  773. .attr("height", margin.top);
  774. grandparent.append("text")
  775. .attr("x", 6)
  776. .attr("y", 6 - margin.top)
  777. .attr("dy", ".75em");
  778. createTreemap = d3.treemap()
  779. .tile(d3.treemapBinary) //.ratio(height / width * 0.5 * (1 + Math.sqrt(5))/2))
  780. .size([width, height - 10])
  781. .round(false)
  782. .paddingInner(0);
  783. }
  784. var root = d3.hierarchy(data)
  785. .eachBefore(function (d) {
  786. d.id = (d.parent ? d.parent.id + "." : "") + d.data.name;
  787. })
  788. //.sum((d) => d.value)
  789. .sum(function (d) {
  790. //console.log(d.value);
  791. return d.value;
  792. })
  793. .sort(function (a, b) {
  794. //console.log('initial root sort a ' + a.value + ' b ' + b.value);
  795. return b.height - a.height || b.value - a.value;
  796. });
  797. initialize(root);
  798. accumulate(root);
  799. layout(root);
  800. createTreemap(root);
  801. display(root);
  802. };
  803. function initialize(root) {
  804. root.x = root.y = 0;
  805. root.x1 = width;
  806. root.y1 = height;
  807. root.depth = 0;
  808. }
  809. function accumulate(d) {
  810. return (d._children = d.children) ?
  811. d.value = d.children.reduce(function (p, v) {
  812. return p + accumulate(v);
  813. }, 0) : d.value;
  814. }
  815. function layout(d) {
  816. if (d._children) {
  817. // treemap.nodes({_children: d._children});
  818. // treemap(d);
  819. d._children.forEach(function (c) {
  820. /*c.x0 = d.x0 + c.x0 * (d.x1 - d.x0);
  821. c.y0 = d.y0 + c.y0 * (d.y1 - d.y0);
  822. c.x1 *= d.x1;
  823. c.y1 *= d.y1;*/
  824. c.x0 = d.x0 + c.x0 * d.x1;
  825. c.y0 = d.y0 + c.y0 * d.y1;
  826. c.x1 *= (d.x1 - d.x0);
  827. c.y1 *= (d.y1 - d.y0);
  828. c.parent = d;
  829. layout(c);
  830. });
  831. }
  832. }
  833. function display(d) {
  834. grandparent
  835. .datum(d.parent)
  836. .on("click", transition)
  837. .select("text")
  838. .text(name(d));
  839. var g1 = treemap.insert("g", ".grandparent")
  840. .datum(d)
  841. .attr("class", "depth");
  842. var g = g1.selectAll("g")
  843. .data(d._children)
  844. .enter().append("g").style("opacity", 1);
  845. //aqui
  846. g.filter(function (d) {
  847. return d._children;
  848. })
  849. .classed("children", true)
  850. .on("click", transition);
  851. var children = g.selectAll(".child")
  852. .data(function (d) {
  853. return d._children || [d];
  854. })
  855. .enter().append("g") //SHOW CHILDREN FROM TREEMAP VIEW
  856. .style("opacity", 0.7); // DEFINES THE OPACITY OF THE INTERNAL NODES
  857. children.append("rect")
  858. .attr("class", "child")
  859. .call(rect)
  860. .append("title")
  861. .text(function (d) {
  862. return d.data.name + " (" + formatNumber(d.data.value) + ")";
  863. });
  864. children.append("text")
  865. .attr("class", "ctext").style("font-size", "14px")
  866. .text(function (d) {
  867. return d.data.name;
  868. })
  869. .call(text2);
  870. g.append("rect")
  871. .attr("class", "parent")
  872. .call(rect);
  873. //MOUSEOVER: SHOW CHILDREN OF THE NODES
  874. g.selectAll("rect").classed("parent", true).on("mouseover", function (d) {
  875. d3.select(this).style("opacity", 0.3); //.
  876. tooltip.html(d.data.name + "<br/>" + formatNumber(d.data.value));
  877. tooltip.transition();
  878. return tooltip.style("visibility", "visible").style("opacity", 1);
  879. //children.select("g").style("opacity", 1);
  880. });
  881. //MOUSEOUT: HIDE CHILDREN OF THE NODES
  882. g.selectAll("rect").classed("parent", false).on("mouseout", function () {
  883. d3.select(this).style("opacity", 1);
  884. return tooltip.style("visibility", "hidden");
  885. });
  886. g.selectAll("rect").classed("parent", false).on("mousemove", function () {
  887. d3.select(this).style("opacity", 0.3);
  888. tooltip.style("opacity", 1);
  889. return tooltip.style("top", (d3.event.pageY - 10) + "px").style("left", (d3.event.pageX + 10) + "px");
  890. });
  891. var t = g.append("text")
  892. .attr("class", "ptext").style("font-weight", "bold")
  893. .attr("dy", ".75em");
  894. t.append("tspan")
  895. .attr("font-weight", "bold")
  896. .text(function (d) {
  897. return d.data.name;
  898. });
  899. t.append("tspan")
  900. .attr("dy", "1.0em")
  901. .text(function (d) {
  902. return formatNumber(d.data.value);
  903. });
  904. t.call(text);
  905. g.selectAll("rect")
  906. .style("fill", function (d) {
  907. return color(d.data.value);
  908. });
  909. function transition(d) {
  910. // Handle Parcoords data
  911. Shiny.onInputChange("scope", d.data.escopo);
  912. Shiny.onInputChange("name", d.data.name);
  913. Shiny.addCustomMessageHandler("treemap2_values_change", function (message) {
  914. if (d.data.children[0]) {
  915. nextScope = d.data.children[0].escopo;
  916. var toColorDup = message[nextScope];
  917. toColor = toColorDup.filter(function (elem, index, self) {
  918. return index === self.indexOf(elem);
  919. });
  920. console.log(toColor);
  921. var i0;
  922. colors_subtitle = [];
  923. for (i0 = 0; i0 < toColor.length; i0++) {
  924. colors_subtitle.push({
  925. name: toColor[i0],
  926. color: colorMake(i0)
  927. });
  928. }
  929. d3.select("#parallel_coordinates_subtitle").selectAll('div').remove();
  930. s1 = d3.select("#parallel_coordinates_subtitle").selectAll('div').data(colors_subtitle).enter().append("div").append("text");
  931. s1Attr = s1.text(function (d) {
  932. return d.name;
  933. })
  934. .style("color", function (d) {
  935. return d.color;
  936. })
  937. .style("font-weight", "bold");
  938. } else {
  939. nextScope = "municipio"
  940. }
  941. traits = ["name", 'value', "Hora", "Dia", "Idade", "Sexo", "Causador"];
  942. message = transposeData(message);
  943. d3.select("#parallelcoordinates_switch_cities").selectAll("*").remove();
  944. reloadCheckBoxesCities(message)
  945. d3.select("#parallelcoordinates_area_cities svg").remove();
  946. reloadParallelCordsCities(message);
  947. });
  948. d3.select("#parallelcoordinates_switch").selectAll("*").remove();
  949. reloadCheckBoxes(d.data.children);
  950. d3.select("#parallelcoordinates_area svg").remove();
  951. reloadParallelCords(d.data.children);
  952. if (transitioning || !d) return;
  953. transitioning = true;
  954. var g2 = display(d),
  955. t1 = g1.transition().duration(750),
  956. t2 = g2.transition().duration(750);
  957. // Update the domain only after entering new elements.
  958. //x.domain([d.x0, d.x0 + d.x1]);
  959. //y.domain([d.y0, d.y0 + d.y1]);
  960. x.domain([d.x0, d.x0 + (d.x1 - d.x0)]);
  961. y.domain([d.y0, d.y0 + (d.y1 - d.y0)]);
  962. // Enable anti-aliasing during the transition.
  963. treemap.style("shape-rendering", null);
  964. // Draw child nodes on top of parent nodes.
  965. //treemap.selectAll(".depth").sort(function(a, b) {
  966. //console.log('.depth sort a ' + a.depth + ' b ' + b.depth);
  967. //return a.depth - b.depth; });
  968. // Fade-in entering text.
  969. g2.selectAll("text").style("fill-opacity", 0);
  970. // Transition to the new view.
  971. t1.selectAll(".ptext").call(text).style("fill-opacity", 0).style("font-weight", "bold"); //.style("font-size", "12px");
  972. t2.selectAll(".ptext").call(text).style("fill-opacity", 1).style("font-weight", "bold"); //.style("font-size", "12px");
  973. t1.selectAll(".ctext").call(text2).style("fill-opacity", 0);
  974. t2.selectAll(".ctext").call(text2).style("fill-opacity", 1);
  975. t1.selectAll("rect").call(rect); //.style("fill", function(d) { return color(d.data.value); });
  976. t2.selectAll("rect").call(rect); //.style("fill", function(d) { return color(d.data.value); });
  977. // Remove the old node when the transition is finished.
  978. t1.remove().on("end", function () {
  979. treemap.style("shape-rendering", "crispEdges");
  980. transitioning = false;
  981. });
  982. }
  983. return g;
  984. }
  985. function make_title(d) {
  986. //console.log("making_title", d);
  987. return d ? d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  988. }
  989. function text(text) {
  990. text.selectAll("tspan")
  991. .attr("x", function (d) {
  992. return x(d.x0) + 5;
  993. });
  994. text.attr("x", function (d) {
  995. return x(d.x0) + 5;
  996. })
  997. .attr("y", function (d) {
  998. return y(d.y0) + 3;
  999. })
  1000. .style("opacity", function (d) {
  1001. var w = x(d.x1) - x(d.x0);
  1002. //console.log("text opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
  1003. return this.getComputedTextLength() <= w ? 1 : 0;
  1004. })
  1005. .style("font-size", "10px");
  1006. }
  1007. function text2(text) {
  1008. text
  1009. .attr("x", function (d) {
  1010. // console.log(d.data.name);
  1011. // console.log( this.getComputedTextLength() );
  1012. return x(d.x1) - this.getComputedTextLength() - 2;
  1013. })
  1014. .attr("y", function (d) {
  1015. return y(d.y1) - 2;
  1016. })
  1017. .style("opacity", function (d) {
  1018. var w = x(d.x1) - x(d.x0);
  1019. //console.log("text2 opacity setting textlength " + this.getComputedTextLength() + " d size " + w);
  1020. return this.getComputedTextLength() <= w ? 1 : 0;
  1021. })
  1022. .style("font-size", "14px");
  1023. }
  1024. function rect(rect) {
  1025. rect.attr("x", function (d) {
  1026. return x(d.x0);
  1027. })
  1028. .attr("y", function (d) {
  1029. return y(d.y0);
  1030. })
  1031. .attr("width", function (d) {
  1032. var w = x(d.x1) - x(d.x0) - 1;
  1033. //console.log('id ' + d.id +' rect width ' + w);
  1034. return w;
  1035. })
  1036. .attr("height", function (d) {
  1037. var h = y(d.y1) - y(d.y0);
  1038. //console.log('id ' + d.id +' rect height ' + h);
  1039. return h;
  1040. })
  1041. .style("stroke", "darkblue")
  1042. .style("stroke-width", 1);
  1043. }
  1044. function name(d) {
  1045. return d.parent ? name(d.parent) + " -> " + d.data.name + " (" + formatNumber(d.data.value) + ")" : d.data.name + " (" + formatNumber(d.data.value) + ")";
  1046. }
  1047. });