heatmap-table.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Based on:
  2. // http://bl.ocks.org/ganezasan/dfe585847d65d0742ca7d0d1913d50e1
  3. var width = window.innerWidth * 0.6,
  4. height = window.innerHeight * 0.4,
  5. stripes_width = window.innerWidth,
  6. stripes_height = window.innerHeight * 0.3;
  7. var gridSize = Math.floor(width / 93); // multiples of 31: number of days
  8. const drawHeatMaps = function(data, location) {
  9. d3.select("#heatmap_container").selectAll("svg").remove();
  10. var slice = [];
  11. for(var i = 0; i < 6; i++) {
  12. var temp = data[i].filter(function(d) {
  13. return d.uf == location;
  14. });
  15. slice.push(temp);
  16. }
  17. const colorScale = d3.scaleSequential()
  18. .domain([0, d3.max(slice, function(d) {
  19. return d3.max(d, function(d) {
  20. return d.value;
  21. });
  22. })])
  23. .interpolator(d3.interpolateBlues);
  24. for(var k = 0; k <= 5; k++) {
  25. heatmapChart(slice[k], location, k, colorScale);
  26. }
  27. heatmapStripes(slice, location, colorScale);
  28. };
  29. const heatmapChart = function(data, location, year, colorScale) {
  30. var hm_tooltip = d3.select("body")
  31. .append("div")
  32. .style("position", "absolute")
  33. .style("z-index", "10")
  34. .style("visibility", "hidden")
  35. .style("color", "white")
  36. .style("padding", "8px")
  37. .style("background-color", "rgba(0, 0, 0, 0.75)")
  38. .style("border-radius", "6px")
  39. .style("font", "12px sans-serif");
  40. var heatmapTable = d3.select("#heatmap_20"+(year+12)+"_area").append("svg")
  41. .attr("width", width / 6)
  42. .attr("height", height)
  43. .append("g");
  44. const cards = heatmapTable.selectAll(".htables")
  45. .data(data);
  46. cards.enter().append("rect")
  47. .attr("x", (d) => (d.month - 1) * gridSize)
  48. .attr("y", (d) => (d.day - 1) * gridSize)
  49. .attr("class", "month bordered")
  50. .attr("width", gridSize)
  51. .attr("height", gridSize)
  52. .style("stroke", "gray")
  53. .style("stroke-width", "0.5px")
  54. .merge(cards)
  55. .style("fill", (d) => colorScale(d.value))
  56. .on("mouseover", function(d) {
  57. d3.select(this)
  58. .style("stroke", "orange")
  59. .style("stroke-width", "1px");
  60. hm_tooltip.html("Dia: " + d.day + "<br/> "+ "Mês: " + d.month + "<br/> " + "Acidentes: " + d.value);
  61. return hm_tooltip.style("visibility", "visible").style("opacity", 1);
  62. }).on("mouseout", function(d) {
  63. d3.select(this)
  64. .style("stroke", "gray")
  65. .style("stroke-width", "0.5px");
  66. hm_tooltip.html("Dia: " + d.day + "<br/> "+ "Mês: " + d.month + "<br/> " + "Acidentes: " + d.value);
  67. return hm_tooltip.style("visibility", "hidden").style("opacity", 1);
  68. }).on("mousemove", function() {
  69. d3.select(this)
  70. .style("stroke", "orange")
  71. .style("stroke-width", "1px");
  72. return hm_tooltip.style("top",(d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
  73. });
  74. cards.exit().remove();
  75. };
  76. const heatmapStripes = function(data, location, colorScale) {
  77. console.log(data);
  78. var boxSizeX = stripes_width / 390,
  79. boxSizeY = stripes_height / 9;
  80. var hm_tooltip = d3.select("body")
  81. .append("div")
  82. .style("position", "absolute")
  83. .style("z-index", "10")
  84. .style("visibility", "hidden")
  85. .style("color", "white")
  86. .style("padding", "8px")
  87. .style("background-color", "rgba(0, 0, 0, 0.75)")
  88. .style("border-radius", "6px")
  89. .style("font", "12px sans-serif");
  90. var heatmapStripes = d3.select("#heatmap_stripes_area").append("svg")
  91. .attr("width", window.innerWidth)
  92. .attr("height", height)
  93. .append("g");
  94. for (var k = 0; k < 6; k++) {
  95. var cards = heatmapStripes.selectAll(".hstripes")
  96. .data(data[k]);
  97. cards.enter().append("rect")
  98. .attr("x", (d, i) => i * boxSizeX)
  99. .attr("y", k * boxSizeY)
  100. .attr("class", "month bordered")
  101. .attr("width", boxSizeX)
  102. .attr("height", boxSizeY)
  103. .style("stroke", "gray")
  104. .style("stroke-width", "0.2px")
  105. .merge(cards)
  106. .style("fill", (d) => colorScale(d.value))
  107. .on("mouseover", function(d) {
  108. d3.select(this)
  109. .style("stroke", "orange")
  110. .style("stroke-width", "1px");
  111. hm_tooltip.html("Dia: " + d.day + "<br/> "+ "Mês: " + d.month + "<br/> " + "Acidentes: " + d.value);
  112. return hm_tooltip.style("visibility", "visible").style("opacity", 1);
  113. }).on("mouseout", function(d) {
  114. d3.select(this)
  115. .style("stroke", "gray")
  116. .style("stroke-width", "0.2px");
  117. hm_tooltip.html("Dia: " + d.day + "<br/> "+ "Mês: " + d.month + "<br/> " + "Acidentes: " + d.value);
  118. return hm_tooltip.style("visibility", "hidden").style("opacity", 1);
  119. }).on("mousemove", function() {
  120. d3.select(this)
  121. .style("stroke", "orange")
  122. .style("stroke-width", "1px");
  123. return hm_tooltip.style("top",(d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
  124. });
  125. cards.exit().remove();
  126. }
  127. };