Advertisement
NTahmid

complex_trend_scatter_v2

Feb 22nd, 2024
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 8.21 KB | None | 0 0
  1. ```<!doctype html>
  2. <meta charset="utf-8" />
  3. <html>
  4.   <head>
  5.     <script src="https://d3js.org/d3.v6.min.js" charset="utf-8"></script>
  6.     <script src="https://rawgit.com/susielu/d3-annotation/master/d3-annotation.min.js"></script>
  7.   </head>
  8.  
  9.   <style>
  10.     * {
  11.       font-family: sans-serif;
  12.     }
  13.  
  14.     #tooltip {
  15.       visibility: hidden;
  16.       position: absolute;
  17.       opacity: 0.8;
  18.       padding: 10px;
  19.       vertical-align: middle;
  20.       border-radius: 5px;
  21.       background-color: #ecf0f1;
  22.       font-size: 14px;
  23.     }
  24.  
  25.     .textbox {
  26.       font-size: 14px;
  27.     }
  28.  
  29.     #legend {
  30.       opacity: 0.2;
  31.       fill: #2c3e50;
  32.     }
  33.  
  34.     #title {
  35.       text-anchor: middle;
  36.       font-size: 22px;
  37.     }
  38.  
  39.     .label {
  40.       text-anchor: middle;
  41.     }
  42.  
  43.     #svg {
  44.       background-color: white;
  45.     }
  46.  
  47.     .annotation-note-title {
  48.       fill: #c86984;
  49.     }
  50.  
  51.     .annotation-note-label {
  52.       fill: #c86984;
  53.     }
  54.  
  55.     .annotation-note-bg {
  56.       fill: #c86984;
  57.     }
  58.  
  59.     .annotation-note-line {
  60.       stroke: #c86984;
  61.     }
  62.  
  63.     .annotation-note-connector {
  64.       stroke: #c86984;
  65.     }
  66.  
  67.     .annotation-note-outline {
  68.       stroke: #c86984;
  69.     }
  70.   </style>
  71.  
  72.   <body>
  73.     <div id="container" align="center"></div>
  74.  
  75.     <script type="text/javascript">
  76.       // Url to the input data
  77.       var url =
  78.         "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json";
  79.  
  80.       // Colors to differentiate riders with and without doping allegations
  81.       var colors = ["#27ae60", "#8e44ad"];
  82.  
  83.       // The attributes of the riders corresponding to the above colors
  84.       var legendKeys = ["No Doping Allegations", "Doping Allegations"];
  85.  
  86.       // Create an invisible div for the tooltip
  87.       const tooltip = d3
  88.         .select("body")
  89.         .append("div")
  90.         .attr("id", "tooltip")
  91.         .style("visibility", "hidden");
  92.  
  93.       // 1. Load the data from external source
  94.       d3.json(url).then(function (data) {
  95.         // 2. Append svg-object for the bar chart to a div in your webpage
  96.         // (here we use a div with id=container)
  97.         var width = 700;
  98.         var height = 500;
  99.         var margin = { left: 90, top: 80, bottom: 50, right: 20 };
  100.         var axisOffset = 10; // How for the axes are moved away from each other
  101.  
  102.         const svg = d3
  103.           .select("#container")
  104.           .append("svg")
  105.           .attr("id", "svg")
  106.           .attr("width", width)
  107.           .attr("height", height);
  108.  
  109.         // 3. Define scales to translate domains of the data to the range of the svg
  110.         var xMin = d3.min(data, (d) => d["Year"]);
  111.         var xMax = d3.max(data, (d) => d["Year"]);
  112.  
  113.         var parseTime = d3.timeParse("%M:%S");
  114.         var yMin = d3.min(data, (d) => parseTime(d["Time"]));
  115.         var yMax = d3.max(data, (d) => parseTime(d["Time"]));
  116.  
  117.         var xScale = d3
  118.           .scaleLinear()
  119.           .domain([xMin, xMax])
  120.           .range([margin.left + axisOffset, width - margin.right]);
  121.  
  122.         var yScale = d3
  123.           .scaleTime()
  124.           .domain([yMax, yMin])
  125.           .range([height - margin.bottom - axisOffset, margin.top]);
  126.  
  127.         // 4. Draw and transform/translate horizontal and vertical axes
  128.         var xAxis = d3.axisBottom().scale(xScale).tickFormat(d3.format("d"));
  129.         var yAxis = d3
  130.           .axisLeft()
  131.           .scale(yScale)
  132.           .tickFormat(d3.timeFormat("%M:%S"));
  133.  
  134.         svg
  135.           .append("g")
  136.           .attr("transform", "translate(0, " + (height - margin.bottom) + ")")
  137.           .attr("id", "x-axis")
  138.           .call(xAxis);
  139.  
  140.         svg
  141.           .append("g")
  142.           .attr("transform", "translate(" + margin.left + ", 0)")
  143.           .attr("id", "y-axis")
  144.           .call(yAxis);
  145.  
  146.         // 5. Draw individual scatter points and define mouse events for the tooltip
  147.         svg
  148.           .selectAll("scatterPoints")
  149.           .data(data)
  150.           .enter()
  151.           .append("circle")
  152.           .attr("cx", (d) => xScale(d["Year"]))
  153.           .attr("cy", (d) => yScale(parseTime(d["Time"])))
  154.           .attr("r", 5)
  155.           .attr("fill", (d) => (d["Doping"] == "" ? colors[0] : colors[1]))
  156.           .attr("class", "dot")
  157.           .attr("data-xvalue", (d) => d["Year"])
  158.           .attr("data-yvalue", (d) => parseTime(d["Time"]))
  159.           .on("mouseover", function (d) {
  160.             info = d["originalTarget"]["__data__"];
  161.             tooltip
  162.               .style("visibility", "visible")
  163.               .style("left", event.pageX + 10 + "px")
  164.               .style("top", event.pageY - 80 + "px")
  165.               .attr("data-year", info["Year"])
  166.               .html(
  167.                 info["Name"] +
  168.                   " (" +
  169.                   info["Year"] +
  170.                   ") <br> Time: " +
  171.                   info["Time"] +
  172.                   "<br><br>" +
  173.                   info["Doping"]
  174.               );
  175.           })
  176.           .on("mousemove", function () {
  177.             tooltip.style("left", event.pageX + 10 + "px");
  178.           })
  179.           .on("mouseout", function () {
  180.             tooltip.style("visibility", "hidden");
  181.           });
  182.  
  183.         // 6. Finalize chart by adding title, axes labels and legend
  184.         svg
  185.           .append("text")
  186.           .attr("x", margin.left + (width - margin.left - margin.right) / 2)
  187.           .attr("y", height - margin.bottom / 5)
  188.           .attr("class", "label")
  189.           .text("Year");
  190.  
  191.         svg
  192.           .append("text")
  193.           .attr("y", margin.left / 4)
  194.           .attr("x", -height / 2)
  195.           .attr("transform", "rotate(-90)")
  196.           .attr("class", "label")
  197.           .text("Time to finish");
  198.  
  199.         svg
  200.           .append("text")
  201.           .attr("x", margin.left + (width - margin.left - margin.right) / 2)
  202.           .attr("y", margin.top / 2.6)
  203.           .attr("id", "title")
  204.           .text("Doping in professional bike racing");
  205.  
  206.         svg
  207.           .append("text")
  208.           .attr("x", margin.left + (width - margin.left - margin.right) / 2)
  209.           .attr("y", margin.top / 1.4)
  210.           .text("35 fastest times to finish Alpe d'Huez")
  211.           .style("font-size", "16px")
  212.           .style("text-anchor", "middle");
  213.  
  214.         svg
  215.           .selectAll("legendSymbols")
  216.           .data(legendKeys)
  217.           .enter()
  218.           .append("circle")
  219.           .attr("cx", width - margin.right - 200)
  220.           .attr("cy", (d, i) => 150 + i * 25)
  221.           .attr("r", 5)
  222.           .attr("fill", (d, i) => colors[i]);
  223.  
  224.         svg
  225.           .selectAll("legendTexts")
  226.           .data(legendKeys)
  227.           .enter()
  228.           .append("text")
  229.           .text((d) => d)
  230.           .attr("x", width - margin.right - 200 + 15)
  231.           .attr("y", (d, i) => 150 + i * 25 + 5)
  232.           .attr("class", "textbox");
  233.  
  234.         const legend = svg
  235.           .append("rect")
  236.           .attr("x", width - margin.right - 200 - 15)
  237.           .attr("y", 150 - 5 - 10)
  238.           .attr("rx", 5)
  239.           .attr("ry", 5)
  240.           .attr("width", 195)
  241.           .attr("height", 55)
  242.           .attr("id", "legend");
  243.  
  244.         // Annotations
  245.         const annotations = [
  246.           {
  247.             note: {
  248.               title: "Trend",
  249.               label: "Cycling times generally decreasing over the years",
  250.             },
  251.             x: xScale(2000),
  252.             y: yScale(parseTime("38:00")),
  253.             dx: -50,
  254.             dy: -50,
  255.           },
  256.           {
  257.             note: {
  258.               title: "Outliers",
  259.               label: "Some cyclists have significantly faster times",
  260.             },
  261.             x: xScale(1995),
  262.             y: yScale(parseTime("36:50")),
  263.             dx: 50,
  264.             dy: 50,
  265.           },
  266.           {
  267.             note: {
  268.               title: "Doping Allegations",
  269.               label: "Cyclists with doping allegations are marked in purple",
  270.             },
  271.             x: xScale(2005),
  272.             y: yScale(parseTime("38:00")),
  273.             dx: 50,
  274.             dy: -50,
  275.           },
  276.         ];
  277.  
  278.         const makeAnnotations = d3
  279.           .annotation()
  280.           .annotations(annotations)
  281.           .type(d3.annotationLabel);
  282.  
  283.         svg.append("g").call(makeAnnotations);
  284.       });
  285.     </script>
  286.   </body>
  287. </html>
  288. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement