Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<script src="https://d3js.org/d3.v4.js"></script>
<h1 class="heading" id='title'>US Gross Domestic Product (1947-2015)</h1>
<div id="graph">
    <svg class="chart"></svg>
</div>
            
          
!
            
              .heading{
  margin-top: 50px;
  text-align: center;
  font-family: Courier;
}

#graph{
  margin-top: 25px;
  margin-left: auto;
  margin-right: auto;
  width: 910px;
  height:500px;
}

/* .chart{
  border: dotted 2px gray;
}  */
/*  .chart div {
    background-color: steelblue;  
    padding: 1px; 
    margin: 1px; 
    border: solid 1px blue;   
    fill: steelblue;    
  */
  
/*  .chart rect {
  border: solid 1px green;   */
/*   fill: gray; */
/* }     */

.x.axis path {
/*   display: none; */
}

.bar {
    fill: silver;
    cursor: pointer;
}

.bar:hover {
    fill: darkgray;
}

.d3-tip{
  text-align: center;
  line-height: 1;
  border: solid 1px gray;
  border-radius: 2px;
  font-family: Arial;
  font-size: 10px;
}

.d3-tip: after{
  box-sizing: border-box;
  display: inline;
  position: absolute;
  font-size: 10px;
}

.d3-tip.n: after{
  margin: -1px 0 0 0;
  top: 100%;
  left: 0;
}

            
          
!
            
              var url = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json";

//pull GDP data from json file into 'result' array
d3.json(url, function(error, data){     
  result = data.data;
  console.log(result);
  
  //set dimensions of graph
  var chartWidth = 910;
  var chartHeight = 500;  
  var margin = {
      top: 20,
      right: 30,
      bottom: 30,
      left: 60
    }
  var width = chartWidth - margin.right - margin.left;
  var height = chartHeight - margin.top - margin.bottom;
  //console.log(width);
  //console.log(height);
  
  //set width of each bar
  var barWidth = Math.ceil(width / result.length);
  //console.log(barWidth)
  
  //set range and domain for GDP value
  var y = d3.scaleLinear()
    .range([0, height])
    .domain([d3.max(result, function(v){return v[1];}),0])
  
  //set y-axis
  var yAxis = d3.axisLeft()
    .scale(y)
    .ticks(20);
  
  //set and convert start dates and end date for x-axis
  var startDate = new Date(result[0][0]);
  var endDate = new Date(result[result.length-1][0]);
  //console.log(startDate)
  //console.log(endDate)
  
  //set time along x-axis
  var x = d3.scaleLinear()
    .range([0, width])
    .domain([startDate, endDate]);
  
  //set x-axis
  var xAxis = d3.axisBottom(x)
    .tickFormat(d3.timeFormat("%Y"))
    .ticks(16);
  
  //when mouse over a bar, sets a tooltip with the GDP amount and exact year and month that bar represents.
  var tooltip = d3.select("#graph")
    .append("div")
    .attr("class","d3-tip")
    .attr("id","tooltip")
    .style("position","absolute")
    .style("margin-left","-50px")
    .style("margin-top","-40px")
    .style("padding","5px")
    .style("text-align","left")
    .style("background-color","white")
    .attr("id","tooltip")
    // .style("visibility","hidden")

  //sets chart
  var chart = d3.select(".chart")
    .attr("width", chartWidth)
    .attr("height", chartHeight)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  
  //sets x- and y-values in the chart
  var bar = chart.selectAll("g")
    .data(result)
    .enter()
    .append("g")
    .attr("transform", function(d, i) {
      return "translate(" + barWidth * i + ",0)";
    })

  
  bar.append("rect")
    .attr("width", barWidth)
    .attr("height", function(v) {return height - y(v[1]);})
    .attr("y", function(v) {return y(v[1]);})
  	.style('stroke', 'lightgray')
    .attr("class", "bar")
    .on("mouseover", function(d)             { var format = d3.timeFormat("%B %Y"); tooltip.style("visibility","visible").html("Date: "+format(new Date(d[0]))+"<br/>"+"GDP: $"+(d[1]/1000).toFixed(2)+" trillion")
    .attr("data-date",v[0])                                 
                                              
    })
    .on("mousemove", function(){return tooltip.style("top", d3.event.pageY + "px").style("left",(d3.event.pageX)+"px");})

  // .on("mouseout", function(){ return tooltip.style("visibility","hidden");})

  chart.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
    .attr("id","x-axis");

  chart.append("g")
    .call(yAxis)
    .attr("id","y-axis");
  
  chart.append("g")
    .append("text")
    .attr("x", -50)
    .attr("y", -10)
    .style("font-size", "10px")
    .style("font-family","Arial")
    .text("US$ billions")
  
  // d3.selectAll("rect").style("fill", function(d, i) {
  //   return i % 2 ? "darkgray" : "silver";
  // });
  
});

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console