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">
  <div id="tooltip">
    <p id="tooltip-date"></p>
    <p id="tooltip-gdp"></p>
  </div>
</div>
              
            
!

CSS

              
                $shadowColor: rgba(0, 0, 0, 0.5);

body {
  background-color: #363;
  padding: 0;
  margin: 0;
}

$chartWidth: 965px;
$chartHeight: 500px;

#container {
  margin-top: 10vh;
  position: absolute;
  width: $chartWidth;
  height: $chartHeight;
  left: 50%;
  margin-left: -$chartWidth / 2;
}

svg {
  background-color: white;
  box-shadow: 10px 10px 30px $shadowColor;
}

.bar:hover {
  fill: white;
}

$tooltipWidth: 160px;

#tooltip {
  background-color: white;
  width: $tooltipWidth;
  height: 50px;
  opacity: 0;
  padding: 14px;
  position: absolute;
  top: 320px;
  transition: .2s;
  transition-property: opacity;
  pointer-events: none;
  border: 1px solid #334;
  box-shadow: 5px 5px 15px $shadowColor;
}

p {
  margin: 0;
  padding: 4px;
  text-align: center;
}

.chart-text {
  font-family: sans-serif;
  font-size: 15px;
}

.chart-title {
  font-size: 36px;
  text-align: center;
}

@media (max-width: $chartWidth) {
  #container {
    left: 0;
    margin-left: 0;
  }
}
              
            
!

JS

              
                const quarterTable = {
  "01": ["Q1", 0.0],
  "04": ["Q2", 0.25],
  "07": ["Q3", 0.5],
  "10": ["Q4", 0.75]
}

// Returns a different bar color depending on the year
function chooseBarColor(index) {
  const colors = ["#6ECC6E", "#7D7", "#8F8", "#9F9"];
  const cap = 8;
  return colors[Math.min(index % cap, (cap - 1) - (index % cap))];
}

// Returns a date formatted as: quarter year.
function getReadableDate(date) {
  const splitDate = date.split("-");
  const quarter = quarterTable[splitDate[1]][0];
  return quarter + " " + splitDate[0];
}

// Returns the year plus a fractional value depending on which quarter the date is in
function getYear(date) {
  const splitDate = date.split("-");
  const quarters = quarterTable[splitDate[1]][1];
  return Number.parseFloat(splitDate[0]) + quarters;
}

window.addEventListener("load", function() {
  // send request for data
  const url = "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json";
  let request = new XMLHttpRequest();
  request.open("GET", url);
  request.responseType = "json";
  request.send();
  request.onload = function() {
    // data has been loaded
    let data = request.response;
    
    const padding = 75; // needed to show the axes
    const barWidth = 3;
    const chartWidth = padding * 2 + barWidth * data.data.length;
    const chartHeight = 500;
    
    let xScale = d3.scaleLinear()
      .domain([1947, 2016])
      .range([padding, chartWidth - padding]);
    
    let yScale = d3.scaleLinear()
      .domain([0, 18000])
      .range([chartHeight - padding, padding]);
    
    let chart = d3.select("#container")
      .append("svg")
      .attr("width", chartWidth)
      .attr("height", chartHeight);
    
    // generate bars
    chart.selectAll("rect")
      .data(data.data, (d) => (d[1]))
      .enter()
      .append("rect")
      .attr("class", "bar")
      .attr("x", (d) => (xScale(getYear(d[0]))))
      .attr("y", (d) => (yScale(d[1])))
      .attr("width", barWidth)
      .attr("height", (d) => (chartHeight - padding - yScale(d[1])))
      .attr("fill", (d, i) => (chooseBarColor(i)))
      .attr("data-date", (d) => (d[0]))
      .attr("data-gdp", (d) => (d[1]))
      .on("mouseover", function(d) {
        // bar mouse over event
        
        const mousePos = d3.mouse(this);
        const tooltipWidth = 160;
        const tooltipOffset = 20;
        const maxX = chartWidth - tooltipWidth - tooltipOffset;
        const tooltipX = (mousePos[0] < maxX ? mousePos[0] + tooltipOffset : mousePos[0] - tooltipWidth - tooltipOffset);
        
        let tooltip = d3.select("#tooltip");
        tooltip
          .style("opacity", 0.9)
          .style("left", tooltipX + "px")
          .attr("data-date", d[0])
          .attr("data-gdp", d[1]);
        tooltip.select("#tooltip-date")
          .html("Date: " + getReadableDate(d[0]));
        tooltip.select("#tooltip-gdp")
          .html("GDP: $" + d[1] + " billion");
      })
      .on("mouseout", function() {
        // bar mouse leave event
      
        d3.select("#tooltip")
          .style("opacity", 0);
      });
    
    // Axes
    let xAxis = d3.axisBottom(xScale)
      .ticks(10, "f");
    chart.append("g")
      .attr("id", "x-axis")
      .attr("transform", "translate(0, " + (chartHeight - padding) + ")")
      .call(xAxis);
    
    let yAxis = d3.axisLeft(yScale)
      .ticks(10, ",f");
    chart.append("g")
      .attr("id", "y-axis")
      .attr("transform", "translate(" + padding + ", 0)")
      .call(yAxis);
    
    // Y-axis label
    chart.append("text")
      .html("Gross Domestic Product (Billions of Dollars)")
      .attr("class", "chart-text")
      .attr("transform", "translate(95, 360) rotate(-90)");
    
    // Title
    chart.append("text")
      .html("United States GDP")
      .attr("class", "chart-text chart-title")
      .attr("id", "title")
      .attr("x", chartWidth / 2)
      .attr("dx", -150)
      .attr("y", 60);
    
    // Source
    chart.append("text")
      .html("Source: " + data.source_name)
      .attr("class", "chart-text")
      .attr("x", 640)
      .attr("y", 464);
  }
});
              
            
!
999px

Console