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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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://d3js.org/d3-scale.v3.min.js"></script>
<h1 id="title">United States GDP</h1>
<div class="chart"></div>



              
            
!

CSS

              
                body{
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
}

.bar{
  fill:orange;
}

.bar:hover{
  fill:black;
}

.chart{
  position:relative;
}

#tooltip {
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  width: 160px;
  height:60px;
  padding: 0.5em;
  font: 1rem;
  background: black;
  color:white;
  border-radius: 4px;
  pointer-events: none; 
}
              
            
!

JS

              
                //step 1: bring in the data
d3.json("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json",json=>{
  const dataset = json["data"];

  //step 2: setting up svg & dynamic scale
  const width = 800;
  const height = 500;
  const padding = 50;
  const dataSize = dataset.length;
  //dynamic scale for x-axis
  const maxDate = d3.max(dataset,d=>d[0]);
  const minDate = d3.min(dataset,d=>d[0]);
  const xScale = d3.scaleTime()
  .domain([new Date(minDate),new Date(maxDate)])
  .range([padding,width-padding]);
  //dynamic scale for y-axis
  const maxGDP = d3.max(dataset,d=>d[1]);
  const yScale = d3.scaleLinear()
  .domain([0,maxGDP])
  .range([height-padding,padding]);
  //append svg
  const svg = d3.select(".chart")
  .append("svg")
  .attr("width",width)
  .attr("height",height);
  //tooltip is a div
  const tooltip= d3.select(".chart").append("div")
  .attr("id", "tooltip")
  .style("opacity", 0);
  //append bars
  svg.selectAll("rect")
    .data(dataset)
    .enter()
    .append("rect")
    .attr("class","bar")
    .attr("x",(d,i)=>xScale(new Date(d[0])))
    .attr("y",(d,i)=>yScale(d[1]))
    .attr("width",width/dataSize)
    .attr("height",(d,i)=>height-yScale(d[1])-padding)
    .attr("data-date",(d,i)=>d[0])
    .attr("data-gdp",(d,i)=>d[1])
  //step 4:add tooltip
    .on('mouseover', function(d, i) {
    tooltip.transition()
      .duration(200)
      .style('opacity', 0.85);
    tooltip.html("Date: " + d[0] + "<br>" + "GDP: $" + d[1] + " billion")
      .attr('data-date', d[0])
      .style('left', (width-padding)+5+"px")
      .style('top', (yScale(d[1])-padding)-50 + 'px');
  })
    .on('mouseout', function(d) {
    tooltip.transition()
      .duration(200)
      .style('opacity', 0);
  });

  //step 3: adding axes
  const xAxis=d3.axisBottom(xScale);
  svg.append("g")
    .attr("id","x-axis")
    .attr("transform","translate(0,"+(height-padding)+")")
    .call(xAxis);

  const yAxis=d3.axisLeft(yScale);
  svg.append("g")
    .attr("id","y-axis")
    .attr("transform","translate("+padding+",0)")
    .call(yAxis);
})








              
            
!
999px

Console