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

              
                
<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>
              
            
!

CSS

              
                .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;
}

              
            
!

JS

              
                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

Console