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

              
                visHolder<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>

<div class="main">
  <div class="container">
    <h1 id='title'>GDP</h1>
    <div class="visHolder">
    </div>
  </div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;900&display=swap');

* {
  margin: 0;
  padding: 0; }

.main {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Roboto";
  background-color: #708090;
  background-size: 64px 128px; }
  .main .container {
    height: 560px;
    width: 900px;
    background-color: #fff;
    display: flex;
    flex-direction: column;
    padding: 20px 20px 20px 20px;
    align-self: center;
    position: relative; }
    @media (min-width: 1000px) {
      .main .container {
        box-shadow: 2px 2px 20px; } }
    .main .container #title {
      text-align: center;
      font-size: 2.5em; }
    .main .container .visHolder {
      position: absolute;
      top: 6em; }

#tooltip {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  text-align: center;
  width: 150px;
  height: 50px;
  padding: 2px;
  font: 12px;
  background: lightsteelblue;
  box-shadow: 1px 1px 10px;
  border-radius: 2px;
  pointer-events: none; }

.overlay {
  position: absolute;
  background: #fff;
  pointer-events: none; }

#y-axis path {
  stroke: black;
  stroke-width: 1;
  fill: none; }

#x-axis path {
  stroke: black;
  stroke-width: 1;
  fill: none; }

.info {
  font-size: 0.8em; }
              
            
!

JS

              
                // Original pen here = https://codepen.io/freeCodeCamp/full/GrZVaM
// Help with d3 V6 = https://observablehq.com/@d3/d3v6-migration-guide#events

const url = "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json"

var yMargin = 40;
const width = 800;
const height = 400;
const padding = 60;
const barWidth = width/275;

var tooltip = d3.select(".visHolder").append("div")
  .attr("id", "tooltip")
  .style("opacity", 0);

var overlay = d3.select(".visHolder").append("div")
  .attr("class", "overlay")
  .style("opacity", 0);
    
var svgContainer = d3.select(".visHolder")
  .append("svg")
  .attr("width", width + 100)
  .attr("height", height + 60);

// Since v5 d3.json is promise-based, hence the change.
d3.json(url)
  .then(data => {
    svgContainer.append('text')
      .attr('transform', 'rotate(-90)')
      .attr('x', -200)
      .attr('y', 80)
      .text('Gross Domestic Product');

    svgContainer.append('text')
      .attr('x', width/2 + 120)
      .attr('y', height + 50)
      .text('More Information: http://www.bea.gov/national/pdf/nipaguid.pdf')
      .attr('class', 'info');

    var years = data.data.map(function(item) {
      var quarter;
      var temp = item[0].substring(5, 7);

      if(temp === '01') {
        quarter = 'Q1';
      }
      else if (temp === '04'){
        quarter = 'Q2';
      }
      else if(temp === '07') {
        quarter = 'Q3';
      }
      else if(temp ==='10') {
        quarter = 'Q4';
      }

      return item[0].substring(0, 4) + ' ' + quarter
    });

    var yearsDate = data.data.map((item) => {
      return new Date(item[0]);
    });

    var xMax = new Date(d3.max(yearsDate));
    xMax.setMonth(xMax.getMonth() + 3);
    var xScale = d3.scaleTime()
      .domain([d3.min(yearsDate), xMax])
      .range([0, width]);

    var xAxis = d3.axisBottom()
      .scale(xScale);

    var xAxisGroup = svgContainer.append('g')
      .call(xAxis)
      .attr('id', 'x-axis')
      .attr('transform', 'translate(' + padding + ', ' + height + ')');

    var GDP = data.data.map((item) => {
      return item[1]
    });

    var scaledGDP = [];

    var gdpMin = d3.min(GDP);
    var gdpMax = d3.max(GDP);

    var linearScale = d3.scaleLinear()
      .domain([0, gdpMax])
      .range([0, height]);
    scaledGDP = GDP.map((item) => {
      return linearScale(item);
    });

    var yAxisScale = d3.scaleLinear()
      .domain([0, gdpMax])
      .range([height, 0]);
    var yAxis = d3.axisLeft(yAxisScale)

    var yAxisGroup = svgContainer.append('g')
      .call(yAxis)
      .attr('id', 'y-axis')
      .attr('transform', 'translate(' + padding + ', 0)');

    svgContainer.selectAll('rect')
      .data(scaledGDP)
      .enter()
      .append('rect')
      .attr('data-date', function(d, i) {
        return data.data[i][0]
      })
      .attr('data-gdp', function(d, i) {
        return data.data[i][1]
      })
      .attr('class', 'bar')
      .attr('x', function(d, i) {
        return xScale(yearsDate[i]);
      })
      .attr('y', function(d, i) {
        return height - d;
      })
      .attr('width', barWidth)
      .attr('height', function(d) {
        return d;
      })
      .style('fill', '#33adff')
      .attr('transform', 'translate(60, 0)')
      .on('mouseover', function(event, d) {
        // Event management is different in v6. Here, d = datum; e = element; i = index.
        const e = svgContainer.selectAll('rect')
                              .data(scaledGDP)
                              .nodes();
        const i = e.indexOf(this);
        overlay.transition()
          .duration(0)
          .style('height', d + 'px')
          .style('width', barWidth + 'px')
          .style('opacity', 0.9)
          .style('left', (i * barWidth) + 0 + 'px')
          .style('top', height - d + 'px')
          .style('transform', 'translateX(' + padding + 'px)');
        tooltip.transition()
          .duration(200)
          .style('opacity', 0.9);
        tooltip.html(years[i] + '<br>' + '$' + GDP[i].toFixed(1).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + ' Billion')
          .attr('data-date', data.data[i][0])
          .style('left', (i * barWidth) + 30 + 'px')
          .style('top', height - 100 + 'px')
          .style('transform', 'translateX(' + padding + 'px)');
      })
      .on('mouseout', function(d) {
        tooltip.transition()
          .duration(200)
          .style('opacity', 0);
        overlay.transition()
          .duration(200)
          .style('opacity', 0);
      });

  });

              
            
!
999px

Console