Advertisement
NTahmid

title_box_plot

Feb 21st, 2024 (edited)
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5.21 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8" />
  3.  
  4. <!-- Load d3.js -->
  5. <script src="https://d3js.org/d3.v4.js"></script>
  6.  
  7. <!-- Create a div where the graph will take place -->
  8. <div id="my_dataviz"></div>
  9.  
  10. <script>
  11.   // set the dimensions and margins of the graph
  12.   var margin = { top: 10, right: 30, bottom: 30, left: 40 },
  13.     width = 960 - margin.left - margin.right,
  14.     height = 600 - margin.top - margin.bottom;
  15.   var chartWidth = 700;
  16.   var boxWidth = 100;
  17.  
  18.   // append the svg object to the body of the page
  19.   var svg = d3
  20.     .select("#my_dataviz")
  21.     .append("svg")
  22.     .attr("width", width + margin.left + margin.right)
  23.     .attr("height", height + margin.top + margin.bottom)
  24.     .append("g")
  25.     .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  26.  
  27.   var months = ["Jan", "Apr", "Jul"];
  28.  
  29.   // Actual data for chickenpox cases - Data for January, April, and July with added data points and outliers
  30.   var actualData = [
  31.     [
  32.     956, 922, 1320, 1500, 1124, 970, 1120, 1154, 970, 1220, 1100, 980, 1050,
  33.     950, 1000, 1800, 1650,
  34.     ], // Data for January
  35.     [
  36.     1005, 1075, 1035, 1200, 1125, 980, 1080, 1150, 930, 1185, 1065, 955, 1000,
  37.     920, 980, 1500, 1420,
  38.     ], // Data for April
  39.     [
  40.     1120, 980, 1035, 1250, 1300, 1400, 1500, 1150, 950, 1000, 1045, 1280,
  41.     1200, 1250, 1380, 1420, 1500,
  42.     ], // Data for July
  43.   ];
  44.  
  45.   var rectangleColors = ["#8ebad9", "#ffbe86", "#95cf95"];
  46.   var lineColors = ["#1f77b4", "#ff7f0e", "#2ca02c"];
  47.  
  48.   //blue, tan, red colors
  49.   var legendColors = d3
  50.     .scaleOrdinal()
  51.     .domain(months)
  52.     .range(["#8ebad9", "#ffbe86", "#95cf95"]);
  53.  
  54.   //Title
  55.   var title = svg
  56.     .append("text")
  57.     .attr("x", chartWidth / 2)
  58.     .attr("y", 40 - margin.top / 2)
  59.     .attr("text-anchor", "middle")
  60.     .style("font-size", "16px")
  61.     .style("text-decoration", "underline")
  62.     .text("Monthly Chickenpox Cases in NYC 1931-1971");
  63.  
  64.   // Annotate the title
  65.   svg
  66.     .append("rect")
  67.     .attr("x", chartWidth / 2 - 200)
  68.     .attr("y", 10)
  69.     .attr("width", 400)
  70.     .attr("height", 40)
  71.     .attr("fill", "none")
  72.     .attr("stroke", "red");
  73.  
  74.   // Show the X scale
  75.   var x = d3
  76.     .scaleBand()
  77.     .range([0, chartWidth])
  78.     .domain(months)
  79.     .paddingInner(1)
  80.     .paddingOuter(0.5);
  81.   svg
  82.     .append("g")
  83.     .attr("transform", "translate(40," + height + ")")
  84.     .call(d3.axisBottom(x));
  85.  
  86.   // Show the Y scale
  87.   var y = d3.scaleLinear().domain([400, 2000]).range([height, 80]);
  88.   svg
  89.     .append("g")
  90.     .attr("transform", "translate(40," + 0 + ")")
  91.     .call(d3.axisLeft(y));
  92.  
  93.   // Y axis label:
  94.   svg
  95.     .append("text")
  96.     .attr("text-anchor", "middle")
  97.     .attr("transform", "rotate(-90)")
  98.     .attr("y", -margin.left + 20)
  99.     .attr("x", -height / 2 + 20)
  100.     .text("Number of Chickenpox Cases");
  101.  
  102.   // Loop through the data and draw the boxplots
  103.   var boxes = svg
  104.     .selectAll(".box")
  105.     .data(months)
  106.     .enter()
  107.     .append("g")
  108.     .attr("class", "box")
  109.     .attr("transform", function (d) {
  110.     return "translate(" + (x(d) + 40 - boxWidth / 2) + ",0)";
  111.     });
  112.  
  113.   boxes.each(function (month, i) {
  114.     var data = actualData[i].sort(d3.ascending);
  115.     var q1 = d3.quantile(data, 0.25);
  116.     var median = d3.quantile(data, 0.5);
  117.     var q3 = d3.quantile(data, 0.75);
  118.     var interQuantileRange = q3 - q1;
  119.     var min = q1 - 1.5 * interQuantileRange;
  120.     var max = q3 + 1.5 * interQuantileRange;
  121.  
  122.     // Show the main vertical line
  123.     d3.select(this)
  124.     .append("line")
  125.     .attr("x1", boxWidth / 2)
  126.     .attr("x2", boxWidth / 2)
  127.     .attr("y1", y(min))
  128.     .attr("y2", y(max))
  129.     .attr("stroke", lineColors[i]);
  130.  
  131.     // Show the box
  132.     d3.select(this)
  133.     .append("rect")
  134.     .attr("y", y(q3))
  135.     .attr("height", y(q1) - y(q3))
  136.     .attr("width", boxWidth)
  137.     .attr("stroke", lineColors[i])
  138.     .style("fill", rectangleColors[i]);
  139.  
  140.     // show median, min and max horizontal lines
  141.     d3.select(this)
  142.     .selectAll("toto")
  143.     .data([min, median, max])
  144.     .enter()
  145.     .append("line")
  146.     .attr("x1", 0)
  147.     .attr("x2", boxWidth)
  148.     .attr("y1", function (d) {
  149.         return y(d);
  150.     })
  151.     .attr("y2", function (d) {
  152.         return y(d);
  153.     })
  154.     .attr("stroke", lineColors[i]);
  155.  
  156.     var filteredData = data.filter(function (d) {
  157.     return d > max || d < min;
  158.     });
  159.  
  160.     // Annotate outliers with a crimson outline
  161.     d3.select(this)
  162.     .selectAll("circle")
  163.     .data(filteredData)
  164.     .enter()
  165.     .append("circle")
  166.     .attr("cx", function (d) {
  167.         return boxWidth / 2;
  168.     })
  169.     .attr("cy", function (d) {
  170.         return y(d);
  171.     })
  172.     .attr("r", 4)
  173.     .style("fill", "none") // Transparent fill
  174.     .attr("stroke", "crimson"); // Crimson outline for outliers
  175.  });
  176.  
  177.  // Legend
  178.  var size = 20;
  179.  svg
  180.     .selectAll(".mydots")
  181.     .data(months)
  182.     .enter()
  183.     .append("rect")
  184.     .attr("class", "mydots")
  185.     .attr("x", 800)
  186.     .attr("y", function (d, i) {
  187.     return 100 + i * (size + 5);
  188.     })
  189.     .attr("width", size)
  190.     .attr("height", size)
  191.     .style("fill", function (d) {
  192.     return legendColors(d);
  193.     });
  194.  
  195.  svg
  196.     .selectAll(".mylabels")
  197.     .data(months)
  198.     .enter()
  199.     .append("text")
  200.     .attr("class", "mylabels")
  201.     .attr("x", 800 + size * 1.2)
  202.     .attr("y", function (d, i) {
  203.     return 100 + i * (size + 5) + size / 2;
  204.     })
  205.     .style("fill", function (d) {
  206.     return legendColors(d);
  207.     })
  208.     .text(function (d) {
  209.     return d;
  210.     })
  211.     .attr("text-anchor", "left")
  212.     .style("alignment-baseline", "middle");
  213. </script>
  214.  
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement