cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div id="scatterplot-stats">
  <h1>Visualize Data with a Scatterplot Graph</h1>
  <h3>FreeCodeCamp</h3>
  <p><a href="https://youtu.be/E2737t3jh_c" target="_blank">Video Tutorial</a></p>
</div>

<!-- Footer-->
		<div class="row text-center">
			<div class="col-xs-12">
				<div class="tableFooter">
				  <!-- for footer -->
      <div class="col-xs-6">
      <h5><a href="http://www.eleftheriabatsou.com" target="_blank">Eleftheria</a> | <a href="https://codepen.io/EleftheriaBatsou" target="_blank">Projects</a> | <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank"> Coding videos </a></h5>
      </div>
      <!-- social media -->
      <div class="col-xs-6 text-primary">
        <a href="https://twitter.com/BatsouElef" target="_blank">
     <img class="footerSvg"src="https://image.flaticon.com/icons/svg/60/60580.svg" 
      style="text-align:center" alt="twitter"> 
    </a>
         <a href="https://github.com/EleftheriaBatsou" target="_blank">
     <img class="footerSvg" src="https://image.flaticon.com/icons/svg/25/25657.svg" 
      style="text-align:center" alt="github"> 
    </a>
        <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank">
     <img class="footerSvg" src="https://image.flaticon.com/icons/svg/25/25312.svg" 
       style="text-align:center" alt="youtube"> 
    </a>
      </div>
    <!-- end footer -->
				</div>
			</div>
		</div>
    <!-- end of Footer -->
            
          
!
            
              body {
 background-color:lightgray;;
}
h1, h3, p{
  text-align:center;
}

#scatterplot-stats {
  text-align: center;
}

.axis {
  fill: none;
  stroke: #3A3A3A;
}

.axis text {
  fill: #000;
  stroke: none;
}

circle {
  stroke: #3A3A3A;
}

.cyclist circle:hover {
  cursor: pointer;
}

.tooltip {
  position: absolute;
  padding: 5px;
  box-sizing: border-box;
  background-color: #000;
  color: #fff;
}

/*
 * Footer
 */
a:link    {color:orangered;  text-decoration:none}
a:visited {color:orangered; text-decoration:none}
a:hover   {color:red; text-decoration:underline}

.tableFooter{
  margin-top: 15px;
}
.footerSvg{
  width: 32px;
}
            
          
!
            
              (function() {
  var url = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/cyclist-data.json";
  
  //our JavaScript section starts and the first thing that happens is that we set the size of the area that we’re going to use for the chart and the margins;
  var margin = { top: 60, left: 60, bottom: 60, right: 90 } 
  var height = 480, width = 780;
  //format the time with minutes and seconds
  var ft = d3.time.format("%M:%S");
  
  //The next section declares the functions linear and scale
  var y = d3.scale.linear().range([0, height]);
  var x = d3.time.scale().range([0, width]);
  
  //The declarations for our two axes are relatively simple
  //show data (nums) under the x-axis line
  var xAxis = d3.svg.axis().scale(x).orient("bottom")
      .ticks(d3.time.seconds, 30)
      .tickFormat(ft);
  //show data (nums) left of the y-axis line
  var yAxis = d3.svg.axis().scale(y).orient("left");
  

/* 
* The next block of code selects the id scatterplot-stats on the web page 
* and appends an svg object to it of the size 
* that we have set up with our width, height and margin’s.
*/
  var svg = d3.select("#scatterplot-stats").append("svg")
      .attr("height", height + margin.top + margin.bottom)
      .attr("width", width + margin.left + margin.right);
  
  svg.append("rect")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .attr("x", 0)
        .attr("y", 0)
        .attr("fill", "white")
        .attr("fill-opacity", 0.8);
  // It also adds a g element that provides a reference point for adding our axes.  
  svg = svg.append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  
  //declair the tooltip
  var tooltip = d3.select("#scatterplot-stats").append("div")
      .attr("class", "tooltip");
  
 
  /*
  * If doping allegiance make the circle red else make it orange
  */
  function doping(arg) {
    return arg !== "" ? "red" : "orange";
  }
  
  /* 
  * this function is like mouse over. 
  * If we place the mouse over a circle the tooltip is going to show up.
  */
  function showToolTip(d, i) {
    tooltip.style({
      "height": "125px",
      "width": "200px",
      "opacity": 0.9
    });
    var circle = d3.event.target; 
    var tippadding = 5, tipsize = { 
      dx: parseInt(tooltip.style("width")), 
      dy: parseInt(tooltip.style("height")) 
    };
  
    tooltip.style({
        "top": (d3.event.pageY - tipsize.dy - 5) + "px",
        "left": (d3.event.pageX - tipsize.dx - 5) + "px"
      }).html("<span><b>" + d.Name + ": " + d.Nationality + "<br/>" + 
            "Place: " + d.Place + " | Time: " + d.Time + "<br/>" + 
            "Year: " + d.Year + "<br/><br/>" + 
            "Doping: " + d.Doping + "</b></span>");
  }
  
  /* 
  * This function is like mouse out. 
  * If we mouse out then the tooltip is hidding
  */
  function hideToolTip(d, i) {
    tooltip.style({
      "height": 0,
      "width": 0,
      "opacity": 0
    }).html("");
  }
  
  /* 
  * This function is like click. 
  * If we click in the circle we are transfering to another site
  */
  function openEntry(d) {
    if(d.URL) {
      var win = window.open(d.URL, "_blank");
      win.focus();
    }
  }
  
  /* 
  * d3.json takes the variable url and two more parameters
  * if error, then throw it
  * else map the time-date in the horizontal axis and the rank-position in the verticall axis
  */
  d3.json(url, (error, data) => {
    if(error) {
      throw new Error("d3.json error");
    }
    else {
      var fastest = d3.min(data.map((item) => { return ft.parse(item.Time); }));
      var slowest = d3.max(data.map((item) => { return ft.parse(item.Time); }));
      
      x.domain([slowest, fastest]);
      y.domain([1, d3.max(data, (d) => { return d.Place; }) + 1]);
      //Add a "g" element that provides a reference point for adding our axes.
      svg.append("g")
          .attr("class", "x axis")
          .attr("transform", "translate(0," + height + ")")
          .call(xAxis)
        .append("text") //add text to the axis
          .attr("transform", "translate(" + width + ",-30)")
          .attr("dy", "1.8em")
          .attr("text-anchor", "end")
          .text("Race time for 13.8km");
      
      svg.append("g")
          .attr("class", "y axis")
          .call(yAxis)
        .append("text") //add text to the axis
          .attr("transform", "rotate(-90)")
          .attr("dy", "-0.8em")
          .attr("text-anchor", "end")
          .text("Rank");
      
      /* 
      * we add the cyclists to our scatterplot
      * This block of code creates the cyclists (selectAll(".cyclist")) 
      * and associates each of them with a data set (.data(data)).
      * We then append a circle 
      * with values for x/y position and height/width as configured in our earlier code.
      * we parse the time and the place
      */ 
      var cyclist = svg.selectAll(".cyclist")
          .data(data)
        .enter().append("g")
          .attr("class", "cyclist")
          .attr("x", (d) => { return x(ft.parse(d.Time)); })
          .attr("y", (d) => { return y(d.Place); });
      
      cyclist.append("circle")
          .attr("cx", (d) => { return x(ft.parse(d.Time)); })
          .attr("cy", (d) => { return y(d.Place); })
          .attr("r", 5)
          .attr("fill", (d) => { return doping(d.Doping); })
          //call the functions
          .on("mouseover", showToolTip)
          .on("mouseout", hideToolTip)
          .on("click", openEntry);
      
      //append the text and fix the distance btw the circles and the names
      cyclist.append("text")
          .attr("x", (d) => { return x(ft.parse(d.Time)) + 7; })
          .attr("y", (d) => { return y(d.Place) + 5; })
          .text((d) => { return d.Name; });
      
      //right-bottom explainatory text
      var isDoped = svg.append("g")
          .attr("transform", "translate(" + (width - 150) + "," + (height - 100) + ")")
          .append("text") 
            .attr("x", 10)
            .attr("y", 5)
            .attr("fill", "red")
            .text("* Doping allegiance");;
      var isNotDoped = svg.append("g")
          .attr("transform", "translate(" + (width - 150) + "," + (height - 80) + ")")
          .append("text")
            .attr("x", 10)
            .attr("y", 5)
            .attr("fill", "orange")
            .text("* No doping allegiance");
    } //end of else
    
  });
}());
            
          
!
999px
Loading ..................

Console