scatterplot.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. Shiny.addCustomMessageHandler("scatterplot_values", function(message) {
  2. var dataset = message;
  3. var parseTime = d3.timeParse("%H%M");
  4. var formatTime = d3.timeFormat("%H:%M");
  5. for (i = 0; i < dataset.length; i++) {
  6. dataset[i][0] = parseTime(dataset[i][0]);
  7. }
  8. var margin = {top: 20, right: 50, bottom: 50, left: 85},
  9. width = 600,
  10. height = 500,
  11. plot_width = width - margin.right - margin.left,
  12. plot_height = height - margin.top - margin.bottom;
  13. var scatterplot = d3.select("#scatterplot_area")
  14. .append("svg")
  15. .attr("width", width)
  16. .attr("height", height);
  17. var brush = d3.polybrush()
  18. .x(d3.scaleLinear().range([margin.left / 2, width]))
  19. .y(d3.scaleLinear().range([margin.top / 2, height]))
  20. .on("start", function() {
  21. scatterplot.selectAll(".selected").classed("selected", false);
  22. })
  23. .on("brush", highlightBrushedPoints)
  24. .on("end", displayTable);
  25. scatterplot.append("g")
  26. .attr("class", "brush")
  27. .call(brush);
  28. function clearTableRows() {
  29. hideTableColNames();
  30. d3.selectAll(".row_data").remove();
  31. }
  32. function hideTableColNames() {
  33. d3.select("table").style("visibility", "hidden");
  34. }
  35. function showTableColNames() {
  36. d3.select("table").style("visibility", "visible");
  37. }
  38. function highlightBrushedPoints() {
  39. scatterplot.selectAll(".point").classed("selected", function(d) {
  40. //get the associated circle
  41. var id = d3.select(this).attr("id");
  42. var i = id.substr(id.indexOf("-") + 1, id.length);
  43. if (brush.isWithinExtent(xScale(d[0]), yScale(d[1]))) {
  44. return true;
  45. } else {
  46. return false;
  47. }
  48. });
  49. }
  50. function displayTable() {
  51. var d_brushed = d3.selectAll(".point.selected").data();
  52. console.log(d_brushed);
  53. // populate table if one or more elements is brushed
  54. if (d_brushed.length > 0) {
  55. clearTableRows();
  56. d_brushed.forEach(d_row => populateTableRow(d_row));
  57. } else {
  58. clearTableRows();
  59. }
  60. }
  61. function populateTableRow(d_row) {
  62. showTableColNames();
  63. var d_row_filter = [d_row[2], // Agente Causador
  64. formatTime(d_row[0]), // Hora
  65. d_row[1]]; //Idade
  66. d3.select("table")
  67. .append("tr")
  68. .attr("class", "row_data")
  69. .selectAll("td")
  70. .data(d_row_filter)
  71. .enter()
  72. .append("td")
  73. .attr("align", (d, i) => i == 0 ? "left" : "right")
  74. .text(d => d);
  75. }
  76. var xScale = d3.scaleTime()
  77. .domain(d3.extent(dataset, function(d) { return (d[0]); }))
  78. .range([margin.left, plot_width]);
  79. var yScale = d3.scaleLinear()
  80. .domain(d3.extent(dataset, function(d) { return d[1]; }))
  81. .range([plot_height, margin.top]);
  82. var axis_x = d3.axisBottom(xScale)
  83. .tickFormat(formatTime);
  84. var axis_y = d3.axisLeft(yScale);
  85. var points = scatterplot.append("g").attr("id", "points");
  86. // Criar círculos
  87. points.selectAll("circle")
  88. .data(dataset)
  89. .enter()
  90. .append("circle")
  91. .attr("class", "point")
  92. .attr("id", function(d, i) {
  93. return "point-" + i;
  94. })
  95. .attr("r", 3)
  96. .attr("cx", function(d) {
  97. return xScale(d[0]);
  98. })
  99. .attr("cy", function(d) {
  100. return yScale(d[1]);
  101. });
  102. scatterplot.append("g")
  103. .attr("id", "axis_x")
  104. .attr("transform", "translate(0," + (plot_height + margin.bottom / 2) + ")")
  105. .call(axis_x);
  106. scatterplot.append("g")
  107. .attr("id", "axis_y")
  108. .attr("transform", "translate(" + (margin.left / 2) + ", 0)")
  109. .call(axis_y);
  110. d3.select("#axis_x")
  111. .append("text")
  112. .attr("transform", "translate(420, -5)")
  113. .text("Hora");
  114. d3.select("#axis_y")
  115. .append("text")
  116. .attr("transform", "rotate(-90) translate(-20, 15)")
  117. .text("Idade");
  118. });