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>

<body>
  <h1 id='title'> US GDP</h1>
  <box class='chartHere'></box>
  <button id='showCode'>Show Code</button>
</body>
              
            
!

CSS

              
                h1{
  padding: 300;
}
.bar :hover{
  fill: #87B37A;
}
body{
  background-color: #000500;
  color: #ACADBC;
}

              
            
!

JS

              
                
//constant height and width
const height = 400;
const width = 800;

//gets json data
let jsonPromise = d3.json('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json');

//uses json data to make graph
  jsonPromise.then((data)=>{
  //preps GDP data
  var gDP = data.data.map(function(i){
    return i[1];
  })
  //scale for GDP
  var scaleGDP = []; 
  var linearScale = d3.scaleLinear().domain([0, d3.max(gDP)]).range([0,height])
  var linearAxis = d3.scaleLinear().domain([0, d3.max(gDP)]).range([height,0])
  
  scaleGDP= gDP.map(function(i){
    return linearScale(i);
  })
    //axis for GDP
   const yAxis =d3.axisLeft(linearAxis)
    
    //preps the dates
  var yearquar = data.data.map(function(day){
    var month = day[0].substring(5,7)
    var quar;
    if(month ==='01'){
      quar = 'Q1';
    }else if(month ==='04'){
      quar = 'Q2';
    }else if(month ==='07'){
      quar = 'Q3';
    }else if(month ==='10'){
      quar = 'Q4';
    }
    return day[0].substring(0, 4) + ' ' + quar;
  })
  var dates = data.data.map(function(i){
    return new Date(i[0]);
  })
  var lastDay = new Date(d3.max(dates))
  lastDay.setMonth(lastDay.getMonth()+3)
    
    //scale for dates
  var xScale = d3.scaleTime()
                  .domain([d3.min(dates), lastDay])
                  .range([0, width])
  //axis for dates 
  const xAxis= d3.axisBottom(xScale)
  //setup for container
  svgContainer = d3.select('box')
  .append('svg')
  .attr('width', width+60)
  .attr('height', height+200)
  .attr('padding', '50px')
  .attr('margin', '100px')

    //adding Y axis
    svgContainer.append('g')
                .call(yAxis)
                .attr("transform", "translate(100, 50)")
                .attr("id", "y-axis")    
    // adding X axis
    svgContainer.append('g')
                .call(xAxis)
                .attr("transform",  "translate(100,450)")
                .attr("id","x-axis")

    //setup tooltips
    var tooltip = d3.select('body')
    .append('div')
    .style('position', 'absolute')
    .style('visibility', 'hidden')
    .style('background-color', '#9B9ECE')
    .style('z-index', 10)
    .style('color', '#473BF0')
    .attr('id','tooltip')
    .text('tooltip test')

   
// creates bars for GDP and puts them in container
 d3.select('svg')
  .selectAll('rect')
  .data(scaleGDP)
  .enter()
  .append('rect')
  .style('fill', '#6665DD')
  .attr('width', width/275)
  .attr('height', (d, i)=>{
  return d;
  })
  .attr('x', (d,i)=>{
  return xScale(dates[i])+100;
})
  .attr('y', (d,i)=>{
  return height-d+50;
})
  .attr('class', 'bar')
  .attr('data-gdp', (d,i)=>data.data[i][1])
  .attr('data-date', (d,i)=>data.data[i][0])
  
   
   d3.selectAll('rect')
     .on('mouseover',(event,d)=> {    
        console.log(scaleGDP.indexOf(d))
        tooltip.text(data.data[scaleGDP.indexOf(d)][0]) 
          .attr('data-date', data.data[scaleGDP.indexOf(d)][0])
          return tooltip.style('visibility','visible')
        
 })
    .on('mousemove', function(e){
     var where = d3.pointer(e)
     tooltip.style("top",where[1]+'px')
     .style('left', where[0]+'px')
   })
    .on('mouseout', function(){
     return tooltip.style('visibility','hidden')
   })

  })
              
            
!
999px

Console