Home Reference Source

scripts/experiment-properties/graphing/d3-custom-plots/distractor_scatter_plot.js

  1. import {plot_scatter_points} from "/scripts/experiment-properties/graphing/d3-base-plots/scatter_plot.js";
  2. export {create_distractor_scatter_plot};
  3.  
  4. var BUFFER = 60;
  5. var RANGE_ADJUSTMENT = 15;
  6.  
  7. /**
  8. * D3 code for setting up scatter plot chart area with distractor dataset (so two-populations per graph)
  9. *
  10. * @param {object} attributes
  11. */
  12. function create_distractor_scatter_plot(attributes) {
  13. let target_dataset = attributes["target"]["dataset"];
  14. console.log("target_dataset: " + JSON.stringify(target_dataset));
  15. let target_properties = attributes["target"]["graph_attributes"];
  16. console.log("target_properties: " + JSON.stringify(target_properties));
  17.  
  18. let distractor_dataset = attributes["distractor"]["dataset"];
  19. console.log("distractor_dataset: " + JSON.stringify(distractor_dataset));
  20. let distractor_properties = attributes["distractor"]["graph_attributes"];
  21. console.log("distractor_properties: " + JSON.stringify(distractor_properties));
  22.  
  23. // Size of the graph
  24. var window_height_multiplier = 0.65;
  25. var window_width_multiplier = 0.5;
  26.  
  27. if (target_properties.alternate_scaling) {
  28. BUFFER = 150;
  29. window_height_multiplier = 1.15;
  30. window_width_multiplier = .75;
  31. }
  32. let height = window.innerHeight * window_height_multiplier;
  33. let width = height * window_width_multiplier;
  34.  
  35. // Create scales:
  36. // ** D3 creates a function that takes in input between [0, 100] and
  37. // outputs between [0, width].
  38. // Basically, domain = input, range = ouput.
  39. let xscale = d3.scaleLinear()
  40. .domain([0, 1])
  41. .range([0, width - RANGE_ADJUSTMENT]);
  42.  
  43. let yscale = d3.scaleLinear()
  44. .domain([0, 1])
  45. .range([height/2, 0 + RANGE_ADJUSTMENT]);
  46.  
  47. if (target_properties.alternate_scaling) {
  48. // Numerosity experiments do not scale the distribution
  49. // coordinates down to the range of [0,1] so the
  50. // call to .domain() in the scaleLinear() method is needed
  51. // scale the domain down into the appropriate range
  52. var x_max = target_properties.row;
  53. var y_max = target_properties.col;
  54.  
  55. xscale = d3.scaleLinear()
  56. .domain([0, x_max])
  57. .range([0, width - RANGE_ADJUSTMENT]);
  58. yscale = d3.scaleLinear()
  59. .domain([0, y_max])
  60. .range([height/2, 0 + RANGE_ADJUSTMENT]);
  61. }
  62.  
  63. // Create axes:
  64. let x_axis = d3.axisBottom()
  65. .scale(xscale)
  66. .tickSize([0]);
  67.  
  68. let y_axis = d3.axisLeft()
  69. .scale(yscale)
  70. .tickSize([0]);
  71.  
  72. // Append SVG into graph div
  73. let chart = d3.select("#graph")
  74. .append("svg")
  75. .attr("width", width + BUFFER)
  76. .attr("height", height)
  77. .attr("style", `margin-right: ${width/2}; margin-top: 25vh; margin-left: ${BUFFER}`);
  78. // Creating transform SVG elements + append to SVG:
  79. let yAxisElements = chart.append("g")
  80. .attr("transform", "translate(50, 10)")
  81. .call(y_axis);
  82.  
  83. let xAxisTranslate = height/2 + 10;
  84.  
  85. let xAxisElements = chart.append("g")
  86. .attr("transform", "translate(50, " + xAxisTranslate +")")
  87. .call(x_axis)
  88.  
  89. // If dist point color is WHITE, only plot the targets
  90. if (distractor_properties["point_color"] === "WHITE") {
  91. plot_scatter_points(chart, xscale, yscale, target_dataset, target_properties);
  92. }
  93. else {
  94.  
  95. let larger_data;
  96. let smaller_data;
  97.  
  98. // Handle if the number of points between distractor & target are unequal
  99. if (attributes["target"]["dataset"].length > attributes["distractor"]["dataset"].length) {
  100. larger_data = attributes["target"];
  101. smaller_data = attributes["distractor"];
  102. } else {
  103. larger_data = attributes["distractor"];
  104. smaller_data = attributes["target"];
  105. }
  106.  
  107. // Alternate plotting of distractor and main dataset points - want equal chance of one
  108. // getting occluded over the other
  109. for (let j in larger_data["dataset"]) {
  110.  
  111. if ( j < smaller_data["dataset"].length){
  112. let small_point = smaller_data["dataset"][j];
  113. plot_scatter_points(chart, xscale, yscale, [small_point], smaller_data["graph_attributes"]);
  114. }
  115.  
  116. let large_point = larger_data["dataset"][j];
  117. plot_scatter_points(chart, xscale, yscale, [large_point], larger_data["graph_attributes"]);
  118. }
  119. }
  120.  
  121. // Set axis color
  122. chart.selectAll("path")
  123. .attr("stroke", target_properties["axis_color"]);
  124.  
  125. // Remove tick labels
  126. chart.selectAll("text").remove();
  127.  
  128. }