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 class="main">
  <div class="container">
    <h1 id="title">Gross Domestic Product</h1>
    <div id="svg-box"></div>
  </div>  
</div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  
}

.main {
  background-color: yellowgreen;
}
.container{
  text-align: center;
  padding-top: 5%;
}

#svg-box {
  margin: 0 auto;
  text-align: center;
  margin-top: 5%;  
}

svg {
  border: 2px solid grey;
  background-color: honeydew;
}

#tooltip {
  position: absolute;
  text-align: center;
  width: auto;
  height: auto;
  padding: 2px;
  font: 12px sans-serif;
  background: lightsteelblue;
  border: 0px;
  border-radius: 8px;
  pointer-events: none;
}
              
            
!

JS

              
                var w= 1000;
var h= 600;
var ypadding = 30;
var xpadding = 50;
var barWidth = 2;

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

var svg = d3.select("#svg-box")
              .append("svg")
              .attr("width", w)
              .attr("height", h)

d3.json('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json',
  function (e, dataset) {
    svg.append('text')
      .attr('transform', 'rotate(-90)')
      .attr('x', -300)
      .attr('y', 80)
      .text('Gross Domestic Product');
  
   svg.append('text')
      .attr('x',  w/2)
      .attr('y',  ypadding)
      .text('More Information: http://www.bea.gov/national/pdf/nipaguid.pdf')
      .attr('class', 'info');  
  
  var dataArr = dataset.data;  
 
  var ymax = d3.max(dataArr, (d) => d[1])
  var yScale = d3.scaleLinear()
                 .domain([0, ymax])
                 .range([h-ypadding, ypadding])
   
    const yAxis = d3.axisLeft(yScale)  
  
   svg.append("g")
     .attr("transform","translate("+xpadding+",0)")
     .call(yAxis)
     .attr('id', 'y-axis')
 
  dataArr.forEach(function(d){
    d[0]= new Date(d[0].substring(0,4), d[0].substring(5,7), 1)
  })
    
 /* dataArr.forEach(function(d){
    let time = parseInt(d[0].substring(0,4));
    let temp=0;
    if (d[0].substring(5,7) == "01"){
      temp= 0.00;
    } else if (d[0].substring(5,7) == "04"){
      temp= 0.25;
    } else if (d[0].substring(5,7) == "07"){
      temp= 0.5;
    } else if (d[0].substring(5,7) == "10"){
      temp = 0.75;
    } 
    d[0]=time+temp;    
  })
 
 */
//x axis  
  var xmin = d3.min(dataArr, (d) => d[0]);
  var xmax = d3.max(dataArr, (d) => d[0]);
  var xScale = d3.scaleTime()
                 .domain([xmin, xmax])
                 .range([xpadding, w-xpadding])
  var xAxis = d3.axisBottom(xScale) 
           
  svg.append("g")
    .attr("transform","translate(0, "+(h-ypadding)+")")
    .call(xAxis)
    .attr('id', 'x-axis')
     
    svg.selectAll("rect")
        .data(dataArr)
        .enter()
        .append("rect") 
        .attr('data-date', function (d, i) {
        return dataArr[i][0];
      })
        .attr('data-gdp', function (d, i) {
        return dataArr[i][1];
      })
        .attr("x", (d) => xScale(d[0]))
        .attr("y", (d) => yScale(d[1]))
        .attr("width", barWidth)
        .attr("height", (d) => h-ypadding-yScale(d[1]))
        .attr("fill", "green")
        .attr("class", "bar")
  
        .on("mouseover", function(d) {
             tooltip.transition()
               .duration(200)
               .style("opacity", .9);
             tooltip.attr('data-date', (d,i) => dataArr[i][0]);           
             tooltip.html(function(d,i) {
                let quarter = "";
                if (dataArr[i][0].getMonth() == "01"){
                      quarter = "Q1"
                    } else if (dataArr[i][0].getMonth() == "04"){
                      quarter = "Q2"
                    } else if (dataArr[i][0].getMonth() == "07"){
                      quarter = "Q3"
                    } else if (dataArr[i][0].getMonth() == "10"){
                      quarter = "Q4"
                    } 
                return dataArr[i][0].getFullYear() +" "+quarter+"<br>"+dataArr[i][1]+"$"
              })
              .style("left", (d3.event.pageX) + "px")
              .style("top", (d3.event.pageY - 28) + "px");
             })
           .on("mouseout", function(d) {
             tooltip.transition()
               .duration(500)
               .style("opacity", 0);
             });
  
  
  })
              
            
!
999px

Console