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.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              html { height: 100%; }
body {
  align-items: center;
  background-color: #1D1E22;
  display: flex;
  justify-content: center;
  height: 100%;
}

svg {
  background-color: #fff;
  margin: 20px;
  padding: 60px 0 40px 50px;
}

.title {
  font-size: 24px;
}

.subtitle {
  font-size: 20px;
}

.dot {
  stroke: #000;
  fill-opacity: 0.7;
  stroke-opacity: 0.7;
}

div.tooltip {
  background: rgba(112, 127, 140, .85);
  border: none;
  border-radius: 2px;
  color: #fff;
  font-size: 18px;
  padding: 10px 20px;
  pointer-events: none;
  position: absolute;
  text-align: left;
  width: 250px;
}
            
          
!
            
              // store data in variable
const dataURI = 'https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/cyclist-data.json'

// set up svg
const w = 1120
const h = 500
const padding = 60
const svg = d3.select('body')
              .append('svg')
              .attr('width', w)
              .attr('height', h)

// set up chart title
svg.append('text')
   .attr('transform', `translate(${w/2}, ${padding - 60})`)
   .attr('class', 'title')
   .attr('id', 'title')
   .style('text-anchor', 'middle')
   .text('Doping in Professional Bicycle Racing')

// set up chart subtitle
svg.append('text')
   .attr('transform', `translate(${w/2}, ${padding - 30})`)
   .attr('class', 'subtitle')
   .attr('id', 'subtitle')
   .style('text-anchor', 'middle')
   .text('35 Fastest times up Alpe d\'Huez')

// read in data
d3.json(dataURI, {crossOrigin: "anonymous"}).then(data => {
  
  // set up min and max for data points
  const minSeconds = d3.min(data, d => d.Seconds - 10)
  const maxSeconds = d3.max(data, d => d.Seconds + 5)
  const minYears = d3.min(data, d => d.Year - 1)
  const maxYears = d3.max(data, d => d.Year + 1)
  
  // set up time format function
  const formatSeconds = seconds => {
    let duration = moment.duration(seconds, 'seconds')
    return moment(duration.asMilliseconds()).format('mm:ss')
  }
  
  // set up date object for test requirement
  const secondsToDate = seconds => {
    let t = new Date(1970, 0, 1)
    t.setSeconds(seconds)
    return t
  }
  
  // set up x scale
  const xScale = d3.scaleLinear()
                   .domain([minYears, maxYears])
                   .range([padding, w - padding])
  
  // set up y scale
  const yScale = d3.scaleLinear()
                   .domain([minSeconds, maxSeconds])
                   .range([padding, h - padding])
  
  // set up color scale
  const colorScale = d3.scaleOrdinal(d3.schemeAccent)
  
  // append div for use as tooltip
  const tooltip = d3.select('body')
                    .append('div')
                    .attr('class', 'tooltip')
                    .attr('id', 'tooltip')
                    .style('opacity', 0)
  
  // set up scatter plot
  const dot = svg.selectAll('circle')
                 .data(data)
                 .enter()
                 .append('circle')
                 .attr('cx', d => xScale(d.Year))
                 .attr('cy', d => yScale(d.Seconds))
                 .attr('r', d => 8)
                 .attr('class', 'dot')
                 .attr('data-xvalue', d => d.Year)
                 .attr('data-yvalue', d => secondsToDate(d.Seconds))
                 .style('fill', d => colorScale(d.Doping !== ''))
                 // set up mouseover to modify tooltip div
                 .on('mouseover', (d, i) => {
                   tooltip.transition()
                          .duration(200)
                          .style('opacity', 0.9)
                   tooltip.html(`
                            ${d.Name}
                            <br />
                            <br />
                            Year: ${d.Year}, Time: ${d.Time}
                            ${d.Doping 
                                  ? `
                                    <br />
                                    <br />
                                    ${d.Doping}`
                                  : ''
                            }
                          `)
                          .attr('data-year', d.Year)
                          .style('left', (d3.event.pageX) + 20 + 'px')
                          .style('top', (d3.event.pageY) + 'px')
                 })
                 // set up mouseout to modify tooltip div
                 .on('mouseout', d => {
                   tooltip.transition()
                          .duration(200)
                          .style('opacity', 0)
                 })
  
  // set up x axis
  const xAxis = d3.axisBottom()
                  .scale(xScale)
                  .tickFormat(d3.format('d'))
  
  // use call to create x axis
  svg.append('g')
     .attr('transform', `translate(0, ${h - padding})`)
     .attr('id', 'x-axis')
     .call(xAxis)
  
  // set up x axis label
  svg.append('text')
   .attr('transform', `translate(${w/2}, ${h - 10})`)
   .style('text-anchor', 'middle')
   .text('Years')
  
  // set up y axis
  const yAxis = d3.axisLeft(yScale).tickFormat(formatSeconds)
  
  // use call to create y axis
  svg.append('g')
     .attr('transform', `translate(${padding}, 0)`)
     .attr('id', 'y-axis')
     .call(yAxis)
  
  // set up y axis label
  svg.append('text')
     .attr('transform', `translate(10, ${h/2})rotate(-90)`)
     .style('text-anchor', 'middle')
     .text('Time in Minutes')
  
  // set up legend
  const legend = svg.selectAll('.legend')
                    .data(colorScale.domain())
                    .enter()
                    .append('g')
                    .attr('class', 'legend')
                    .attr('id', 'legend')
                    .attr('transform', (d, i) => `translate(0, ${h/2 - i * 25})`)
  
  // add proper text to legend
  legend.append('text')
        .attr('x', w - 70)
        .attr('y', 15)
        .style('text-anchor', 'end')
        .text(d => {
          if (d) {
            return "Riders with doping allegations"
          } else {
            return "No doping allegations"
          }
        })
  
  // add color keys to legend
  legend.append('rect')
        .attr('x', w - padding)
        .attr('width', 20)
        .attr('height', 20)
        .style('fill', colorScale)
  
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console