Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="container">
  <h1 id="title">United States GDP</h1>
  <div id="bar-chart"></div>
</div>
              
            
!

CSS

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

* {
  margin: 0;
  padding: 0;
  font-family: 'Roboto', sans-serif;
  color: #2F3E46;
}

body {
  display: flex;
  height: 100vh;
  justify-content: center;
  align-items: center;
  background: #cad2c5;
}

.container {
  width: 900px;
  height: 550px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  background: #e8e8e8;
}

.bar {
  fill: #52796f;
  margin-left: 1px;
}

.bar:hover {
  fill: #e8e8e8;
}

#tooltip {
  color: #e8e8e8;
  width: 150px;
  height: 70px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  position: absolute;
  border-radius: 8px;
  background: #2F3E46;
}

@media screen and (max-width: 900px) {
  .container {
    transform: scale(0.9);
  }
}
@media screen and (max-width: 800px) {
  .container {
    transform: scale(0.8);
  }
}
@media screen and (max-width: 700px) {
  .container {
    transform: scale(0.7);
  }
}
@media screen and (max-width: 600px) {
  .container {
    transform: scale(0.6);
  }
}
@media screen and (max-width: 500px) {
  .container {
    transform: scale(0.5);
  }
}
@media screen and (max-width: 400px) {
  .container {
    transform: scale(0.4);
  }
}
@media screen and (max-width: 300px) {
  .container {
    transform: scale(0.3);
  }
}
              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', () => {

  async function getGDPData() {
    const url = 'https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json';

    try {
      let response = await fetch(url);
      let data = await response.json();
      const dataset = data.data;

      const width = 800;
      const height = 450;
      const padding = 40;
      const barWidth = (width / 275) - 1;

      const svg = d3.select('#bar-chart')
                    .append('svg')
                    .attr('width', width)
                    .attr('height', height);

      const GDPMin = d3.min(dataset, (d) => d[1]);
      const GDPMax = d3.max(dataset, (d) => d[1]);

      const yearsDate = dataset.map((i) => new Date(i[0]));

      const QMin = d3.min(yearsDate);
      const QMax = new Date(d3.max(yearsDate));
      QMax.setMonth(QMax.getMonth() + 3);
      
      const xScale = d3.scaleTime()
                       .domain([QMin, QMax])
                       .range([padding, width - padding]);      

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

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

      const yScale = d3.scaleLinear()
                       .domain([0, GDPMax])
                       .range([height - padding, padding]);

      const yAxis = d3.axisLeft()
                      .scale(yScale);

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

      svg.append('text')
         .attr('transform', 'rotate(-90)')
         .attr('x', -300)
         .attr('y', 60)
         .attr('class', 'tick')
         .text('Gross Domestic Product');   
         
      svg.append('text')   
         .attr('x', width / 2 - 65)
         .attr('y', height - 3)
         .attr('class', 'tick')
         .text('1947-2015 Quarterly');

      const GDP = dataset.map(i => i[1]);
      
      const linearScale = d3.scaleLinear()
                            .domain([0, GDPMax])
                            .range([0, height - padding * 2]);
                            
      const scaledGDP = GDP.map(i => linearScale(i));

      const tooltip = d3.select('#bar-chart')
                        .append('div')
                        .attr('id', 'tooltip')
                        .style('opacity', 0);

      const bars = d3.select('svg')
                     .selectAll('rect')
                     .data(dataset)
                     .enter()
                     .append('rect')
                     .attr('class', 'bar')
                     .attr('data-date', (d) => d[0])
                     .attr('data-gdp', (d) => d[1])
                     .attr('x', (d, i) => xScale(yearsDate[i]))
                     .attr('y', (d, i) => height - scaledGDP[i])
                     .attr('width', barWidth)
                     .attr('height', (d, i) => scaledGDP[i])
                     .attr('transform', 'translate(0, -40)');

      const mouseover = (event, d) => {
        tooltip
          .transition()
          .duration(0)
          .style('opacity', 0.8)
          .attr('data-date', d[0])
          .style('left', width - 153 + 'px')
          .style('top', height - 55 + 'px');
        tooltip
          .html(
            d[0] + '<br/>' + '$' + 
            d[1].toFixed(1).replace(/(\d)(?=(\d{3})+\.)/g, '$1,') + 
            ' Billion'
          );
      } 

      const mouseout = () => {
        tooltip
          .transition()
          .duration(200)
          .style('opacity', 0);
      }

      bars.on('mouseover', mouseover);
      bars.on('mouseout', mouseout);    

    } catch(error) {
      console.log(error);
    }
  }

  getGDPData();
                
});
              
            
!
999px

Console