Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 class="container">
  <h1 id="title">Doping in Professional Cycling</h1>
  <h2 id="subtitle">35 Fastest Alpe d'Huez Ascents</h2>
  <div id="scatter-plot"></div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Open+Sans&display=swap');

* {
  margin: 0;
  padding: 0;
}

body {
  font-family: 'Open Sans', sans-serif;
  color: #0d1317;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: gainsboro;
}

.container {
  position: relative;
  width: 870px;
  height: 600px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: white;
  border: 1px solid rgba(0,0,0,0.2);
}

#subtitle {
  font-size: 1.2rem;
  margin: 5px;
}

#scatter-plot {
  user-select: none;
}

#tooltip {
  font-size: 0.8rem;
  color: white;
  position: absolute;
  width: auto;
  height: auto;
  padding: 8px;
  background: #0d1317;
  border-radius: 6px;
}

@media screen and (max-width: 870px) {
  .container {
    transform: scale(0.9);
  }
}
@media screen and (max-width: 770px) {
  .container {
    transform: scale(0.8);
  }
}
@media screen and (max-width: 670px) {
  .container {
    transform: scale(0.7);
  }
}
@media screen and (max-width: 570px) {
  .container {
    transform: scale(0.6);
  }
}
@media screen and (max-width: 470px) {
  .container {
    transform: scale(0.5);
  }
}
@media screen and (max-width: 370px) {
  .container {
    transform: scale(0.4);
  }
}
@media screen and (max-width: 270px) {
  .container {
    transform: scale(0.3);
  }
}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', () => {

  async function getCyclingData() {
    const url = 'https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json';

    try {
      let response = await fetch(url);
      let data = await response.json();
      // console.log(data);

      const width = 800;
      const height = 450;
      const padding = 40;
    
      const svg = d3.select('#scatter-plot')
                    .append('svg')
                    .attr('width', width)
                    .attr('height', height);

      const tooltip = d3.select('#scatter-plot')
                        .append('div')
                        .attr('id', 'tooltip')
                        .style('opacity', 0);              
      
      const fastestTime = d3.min(data, (d) => new Date(d.Seconds * 1000));
      const slowestTime = d3.max(data, (d) => new Date(d.Seconds * 1000));

      const minYear = d3.min(data, (d) => d.Year - 1); 
      const maxYear = d3.max(data, (d) => d.Year + 1);

      const xScale = d3.scaleUtc()
                       .domain([minYear, maxYear])
                       .range([padding + 30, width - padding]);

      const xAxis = d3.axisBottom()
                      .tickFormat(d3.format('d'))
                      .scale(xScale);

      const yScale = d3.scaleTime()
                       .domain([slowestTime, fastestTime])
                       .range([height - padding, padding]);

      const yAxis = d3.axisLeft()
                      .tickFormat(d3.timeFormat('%M:%S'))
                      .scale(yScale);

      svg.append('g')
         .attr('transform', 'translate(0, ' + (height - padding) + ')')
         .attr('id', 'x-axis')
         .call(xAxis);

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

      svg.append('text')
         .attr('transform', 'rotate(-90)')  
         .attr('x', -260)
         .attr('y', 20)
         .style('font-size', 12 + 'px')
         .text('Time in Minutes');

      const dots = svg.selectAll('.dot')
                      .data(data)   
                      .enter()
                      .append('circle')
                      .attr('class', 'dot')
                      .attr('r', 5)
                      .attr('cx', (d) => xScale(d.Year))
                      .attr('cy', (d) => yScale(new Date(d.Seconds * 1000)))
                      .attr('data-xvalue', (d) => d.Year)
                      .attr('data-yvalue', (d) => new Date(d.Seconds * 1000))
                      .style('stroke', '#0d1317')
                      .style('stroke-width', 0.5)
                      .style('fill', (d) => d.Doping === '' ? '#6564db' : '#ff6347');   

      const mouseover = (event, d) => {
        const [x, y] = d3.pointer(event);
        tooltip
          .attr('data-year', d.Year)
          .style('left', (x + 50) + 'px')
          .style('top', (y + 100) + 'px')
          .style('opacity', 0.8);
        tooltip.html(
          d.Name + ': ' + d.Nationality + '<br/>' + 'Year: ' + 
          d.Year + '<br/>' + ' Time: ' + d.Time + '<br/>' + d.Doping
        )
      }                

      const mouseout = () => {
        tooltip
          .transition()
          .duration(200)
          .style('opacity', 0);
      }
                      
      dots.on('mouseover', mouseover);
      dots.on('mouseout', mouseout);                

      const legend = svg.selectAll('.legend')   
                        .data(data)
                        .enter()
                        .append('g')
                        .attr('class', 'legend')
                        .attr('id', 'legend');

      legend.append('rect')
            .attr('width', 12)
            .attr('height', 12)
            .attr('x', width - 200)
            .attr('y', padding)
            .style('stroke', '#0d1317')
            .style('stoke-width', 0.5)
            .style('fill', '#6564db');

      legend.append('rect')
            .attr('width', 12)
            .attr('height', 12)
            .attr('x', width - 200)
            .attr('y', padding + 20)
            .style('stroke', '#0d1317')
            .style('stoke-width', 0.5)
            .style('fill', '#ff6347');

      svg.append('text')
            .attr('x', width - 180)
            .attr('y', padding + 10)
            .style('font-size', 12 + 'px')
            .text('No Doping Allegations');   
            
      svg.append('text')
            .attr('x', width - 180)
            .attr('y', padding + 30)
            .style('font-size', 12 + 'px')
            .text('Doping Allegations')      

    } catch(error) {
      console.log(error);
    }
  }

  getCyclingData();

});
              
            
!
999px

Console