Pen Settings



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


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


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.


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.


<script src=""></script>
<h1 class="heading" id='title'>US Gross Domestic Product (1947-2015)</h1>
<div id="graph">
    <svg class="chart"></svg>


  margin-top: 50px;
  text-align: center;
  font-family: Courier;

  margin-top: 25px;
  margin-left: auto;
  margin-right: auto;
  width: 910px;

/* .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;

  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 = "";

//pull GDP data from json file into 'result' array
d3.json(url, function(error, data){     
  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.bottom;
  //set width of each bar
  var barWidth = Math.ceil(width / result.length);
  //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()
  //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]);
  //set time along x-axis
  var x = d3.scaleLinear()
    .range([0, width])
    .domain([startDate, endDate]);
  //set x-axis
  var xAxis = d3.axisBottom(x)
  //when mouse over a bar, sets a tooltip with the GDP amount and exact year and month that bar represents.
  var tooltip ="#graph")
    // .style("visibility","hidden")

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

    .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");"visibility","visible").html("Date: "+format(new Date(d[0]))+"<br/>"+"GDP: $"+(d[1]/1000).toFixed(2)+" trillion")
    .on("mousemove", function(){return"top", d3.event.pageY + "px").style("left",(d3.event.pageX)+"px");})

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

    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")

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