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

              
                  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
              
            
!

CSS

              
                body{
    background: dimgray;
}

.App{
    display: flex;
    flex-direction: column;
    align-items: center;
}

#title{
    position: absolute;
}

.main-svg{
    background: aliceblue;
}

.dot:hover{
fill: chartreuse;
}

.text-container{
    fill: rgb(172, 166, 160);
    width: 200px;
    height: 50px;

}

.text-description{
    fill: ghostwhite;
    font-size: 1.2rem;

}
              
            
!

JS

              
                
import { useState, useEffect } from "https://cdn.skypack.dev/react";
import * as d3 from "https://cdn.skypack.dev/d3";
import React from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";


ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);


function App() {

  const [dataPoints, setDataPoints] = useState('');
  let svg = d3.select('svg');


  useEffect(() => {
    fetch('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/cyclist-data.json')
      .then((response) => response.json())
      .then(result => { setDataPoints(result); return; })
      .catch((error) => console.log(error))
  }, []);

  useEffect(() => {

    if (dataPoints) {
      console.log("UseEffect Fired", dataPoints)
      svg.remove();
      const width = 1000;
      const height = 600;
      const margin = 100;

      const parseY = d3.timeParse('%M:%S');
      const formatY = d3.timeFormat('%M:%S');
      const timeMin = d3.min(dataPoints, (d) => parseY(d["Time"]));
      const timeMax = d3.max(dataPoints, (d) => parseY(d["Time"]));


      const parseX = d3.timeParse('%Y');
      const formatX = d3.timeFormat('%Y');
      const yearMin = d3.min(dataPoints, (d) => parseX(d["Year"]));
      const yearMax = d3.max(dataPoints, (d) => parseX(d["Year"]));

      svg = d3.select(".App")
        .append("svg")
        .attr('class', 'main-svg')
        .attr("width", width)
        .attr("height", height);

      const xScale = d3.scaleTime()
        .domain([yearMin, yearMax])
        .range([margin, width - margin])

      const yScale = d3.scaleTime()
        .domain([timeMax, timeMin])
        .range([height - margin, margin]);

      svg.selectAll("dot")
        .data(dataPoints)
        .enter()
        .append("circle")
        .attr('class', 'dot')
        .attr('data-xvalue', (d) => parseX(d['Year']))
        .attr('data-yvalue', (d) => parseY(d['Time']))
        .attr("cx", (d) => xScale(parseX(d['Year'])))
        .attr("cy", (d) => yScale(parseY(d['Time'])))
        .attr("r", 5)
        .attr('data', (d) => d["Time"])
        .attr("fill", (d) => {
          return d['Doping'] ? 'red' : 'blue';
        })
        .on('mouseover', handleMouseOver)
        .on("mouseout", handleMouseOut)


      const xAxis = d3.axisBottom(xScale).tickFormat(formatX);
      const yAxis = d3.axisRight(yScale).tickFormat(formatY);

      // This was first used to generate grids
      // const xGrid = d3.axisBottom(xScale).tickFormat('').tickSize(0).tickPadding(0);
      // svg.append("g")
      // .call(xGrid)
      // .attr('stroke-width', '0.1px')
      // .attr("transform", "translate(0," + (476) + ")")
      // .attr('id', 'x-axis');

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


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





      // To be constructed
      function handleMouseOver() {
        console.log("fired adder")
        let y = parseInt(this.getAttribute('cy'));
        let x = parseInt(this.getAttribute('cx'));
      }
      function handleMouseOut() {
        console.log('fired remover')
        d3.selectAll('#tooltip')
          .remove()
      }

      // still don't understand how to set up the grid properly taking the margin into acount
      // function make_x_axis() {
      //   return d3.axisBottom(xScale)
      //     .ticks(10)

      // }

      // // function for the y grid lines
      // function make_y_axis() {
      //   return d3.axisLeft(yScale)
      //     .ticks(10);
      // }

      // svg.append("g")
      // .attr("class", "grid")
      // .call(make_x_axis()
      //   .tickSize(height-2*margin, -300, +300)
      //   .tickFormat("")
      // );

      // // Draw the y Grid lines
      // svg.append("g")
      //   .attr("class", "grid")
      //   .call(make_y_axis()
      //     .tickSize(-width, 0, 0)
      //     .tickFormat("")
      //   );


    }
  }, [dataPoints]);


  return (
    <div className="App">
      <h3 id='title'>U.S. Bureau of Economic Analysis Gross Domestic Product</h3>
    </div>
  );
}
              
            
!
999px

Console