Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="chart"></div>
              
            
!

CSS

              
                svg {
  background-color: #F5E1E1;
}
body {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
#tooltip {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  width: 150px;
  overflow: auto;
  padding: 10px;
  font: 12px;
  background-color: white;
  box-shadow: 1px 1px 10px;
  border-radius: 20px;
  pointer-events: none;
}

              
            
!

JS

              
                //Retrieve data for chart
d3.json('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json', function(unmappedDataset) {
  
  
  //Filter data required and organise it by seperating it
  const mappedDataset = unmappedDataset.map(item => ({
                                    year: item["Year"],
                                    time: item["Seconds"]  
                                    }));
  const yearsArray = unmappedDataset.map(item => item["Year"]);
  const stringTimesArr = unmappedDataset.map(item => item["Time"]);
  console.log(unmappedDataset);
  
  
  //Convert yearsArray from number format to date format
  const convYearsArray = yearsArray.map(item => new Date(item, 0, 0, 0, 0, 0, 0));
  
  
  //Change stringTimesArr array from string to date format
  unmappedDataset.forEach(function (d) {
    var parsedTime = d.Time.split(':');
    d.Time = new Date(1970, 0, 1, 0, parsedTime[0], parsedTime[1]);
  });
  const dateTimeArr = unmappedDataset.map(item => item["Time"]);
  
  
  //Set svg (where graph will be) dimensions
  const w = 1000;
  const h = 500;
  const padding = 60;
  
  
  //Create svg area (where graph will be)
  const svg = d3.select("#chart")
  .append("svg")
  .attr("width", w)
  .attr("height", h);
  
  
  //Create scales to make graph fit page
  const xScale = d3.scaleTime()
  .domain([new Date(1993,0,0,0,0,0,0), new Date(2016,0,0,0,0,0,0)])
  .range([padding+10, w - padding]);

  const yScale = d3.scaleLinear()
  .domain([d3.min(dateTimeArr), d3.max(dateTimeArr)])
  .range([h - padding, padding]);
  

  //Create and place dots
  svg.selectAll("circle")
    .data(unmappedDataset)
    .enter()
    .append("circle")
    .attr('data-xvalue', (d, i) => yearsArray[i])
    .attr('data-yvalue', (d, i) => dateTimeArr[i])
    .attr("cx", (d, i) => xScale(convYearsArray[i]))
    .attr("cy", (d, i) => yScale(dateTimeArr[i]))
    .attr("r", 5)
    .attr("fill", (item) => {if(item["Doping"] == ""){return "orange"}else{return "navy"}})
    .attr("class", "dot")
    .on("mouseover", handleMouseOver)
    .on("mouseout", handleMouseOut)
  
  
  //Create chart title 
  svg.append("text")
    .attr("x", w / 2 )
    .attr("y", padding/2)
    .style("text-anchor", "middle")
    .text("Doping in Professional Bicycle Racing - 35 Fastest times up Alpe d'Huez")
    .attr("id","title")

  
  //Create X axis 
  const xAxis = d3.axisBottom(xScale);

  svg.append("g")
    .attr("transform", "translate(0," + (h - padding) + ")")
    .call(xAxis)
    .attr("id","x-axis");
  
  
  //Create Y axis 
  const yAxis = d3.axisLeft(yScale).tickFormat(d3.timeFormat('%M:%S'));

  svg.append("g")
    .attr("transform", "translate(" + (padding+10) + ",0)")
    .call(yAxis)
    .attr("id","y-axis");

  
  //X axis title
  svg.append("text")
    .attr("class", "x label")
    .attr("text-anchor", "end")
    .attr("x", w/2)
    .attr("y", h - padding*0.25)
    .text("Year");
  
  
  //Y axis title
  svg.append("text")
    .attr("class", "y label")
    .attr("text-anchor", "end")
    .attr("y", 6)
    .attr("x", -h/4)
    .attr("dy", ".75em")
    .attr("transform", "rotate(-90)")
    .text("Time up Alpe d'Huez (seconds)");
  
  
  //Create tooltip   
  const tooltip = d3.select('#chart')
                    .append('div')
                    .attr('id', 'tooltip')
                    .style('opacity', 0);
  
  
  // Create Event Handlers for mouse
  function handleMouseOver(d, i) {  
    tooltip
      .style("opacity", 0.9)
      .attr("data-year",yearsArray[i])
      .html(yearsArray[i] + '<br>' + stringTimesArr[i] + '<br>' + unmappedDataset[i]["Name"] + '<br>' + unmappedDataset[i]["Nationality"] + '<br>' + unmappedDataset[i]["Doping"])
      .style('left', d3.event.pageX + 10 + 'px')
      .style('top', d3.event.pageY - 28 + 'px');
  }
  
  function handleMouseOut(d, i) {  
    tooltip.style("opacity", 0);
    d3.select(this)
      .attr("fill", (item) => {if(item["Doping"] == ""){return "orange"}else{return "navy"}})
  }
  
  
  // Create legend
  // Handmade legend
  const legend = svg.append("g")
                    .attr("id", "legend")
  
  legend.append("rect")
        .attr("x",w*0.65)
        .attr("y",h*0.75)
        .attr("width", 20)
        .attr("height", 20)
        .style("fill", "orange")
  
  legend.append("rect")
        .attr("x",w*0.65)
        .attr("y",h*0.8)
        .attr("width", 20)
        .attr("height", 20)
        .style("fill", "blue")
  
  legend.append("text")
        .attr("x", w*0.69)
        .attr("y", h*0.77)
        .text("No doping allegations")
        .style("font-size", "15px")
        .attr("alignment-baseline","middle")
  
  legend.append("text")
        .attr("x", w*0.69)
        .attr("y", h*0.82)
        .text("Riders with doping allegations")
        .style("font-size", "15px")
        .attr("alignment-baseline","middle")
});
              
            
!
999px

Console