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

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
 <div id="container">
        <h1 id="title">United States Gross Domestic Product (GDP)</h1>
        <div id="chart-container">

        </div>
    </div>
              
            
!

CSS

              
                body {
    background: #FFD898;
}

#container {
    display: flex;
    flex-direction: column;
    align-items: center;    
    position: relative;
    font-family: 'Open Sans', sans-serif;
  }
  
  #tooltip {
      position: absolute;
      background: #FFCE7F;
      border: #333 solid 1.px;
      padding: 10px;
      box-shadow: 2px 2px 5px 0px rgba(0,0,0,0.75);
  }
  
  .bar {
    fill : #3576B2;
  }
              
            
!

JS

              
                var json = "";

document.addEventListener("DOMContentLoaded", function() {
  loadJSON();
});

function loadJSON() {
  var req = new XMLHttpRequest();
  req.open(
    "GET",
    "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json",
    true
  );
  req.onload = function() {
    if (req.readyState == 4 && req.status == 200) {
      json = JSON.parse(req.responseText);
      renderD3BarChart();
    }
  };
  req.send();
}

function renderD3BarChart() {
  const w = 960;
  const h = 800;
  padding = 50;

  // Create x and y scales
  
  // Scale x by date - took a while to figure this out!
  // Thanks D3.js documentation!
  // https://github.com/d3/d3-scale/blob/master/README.md#scaleTime
  var minDate = new Date(json.data[0][0]);
  var maxDate = new Date(json.data[json.data.length - 1][0]);
  
  const xScale = d3.scaleTime()
                   .domain([minDate, maxDate])
                   .range([padding, w - padding ]);

  // Simple linear scale for y
  const yScale = d3.scaleLinear()
                   .domain([d3.min(json.data, (d) => d[1]), d3.max(json.data, (d) => d[1])])
                   .range([h - padding, padding]);
  
  // Declare a toolTip.  The method of appending a title doesn't pass the test
  var toolTip = d3.select("#container")
                  .append("div")
                  .attr("id", "tooltip")
                  .style("visibility", "hidden");
 
  const svg = d3
    .select("#chart-container")
    .append("svg")
    .attr("width", w)
    .attr("height", h);
  
  svg
    .selectAll("rect")
    .data(json.data)
    .enter()
    .append("rect")
    .attr("class", "bar")
    .attr("x", (d, i) => xScale(new Date(d[0])))
    .attr("y", d => yScale(d[1]))
    .attr("width", w / json.data.length)
    .attr("height", d => yScale(0) - yScale(d[1]) )
    .attr("data-date", d => d[0])
    .attr("data-gdp", d => d[1])    
    .on("mouseover", d => {
        toolTip.html(`Date: ${d[0]}<br>GDP: ${d[1]} Billion`)
               .attr("data-date", d[0])
               .style("top", `${d3.event.pageY}px`)
               .style("left",`${d3.event.pageX}px`)
               .style("visibility", "visible");
    })
    .on("mouseout", d => {
        toolTip.style("visibility", "hidden");
    });
  
  // Create the xAxis
  const xAxis = d3.axisBottom(xScale);

  svg.append("g")
     .attr("transform", "translate(0, " + (h - padding + 10) + ")")
     .attr("id", "x-axis")
     .call(xAxis);
  
  // Create the yAxis
  const yAxis = d3.axisLeft(yScale);
  
  svg.append("g")
     .attr("transform", "translate(" + (padding) + ", 0)")
     .attr("id", "y-axis")
     .call(yAxis);
}


              
            
!
999px

Console