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

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

              
                <html lang="en">

<head>
  <meta charset="utf-8">

  <head>
    <title>D3-Zipline: GDP Bar Graph</title>
  </head>

  <body>
    <div class="card">
      <div class="title">Gross Domestic Product</div>
      <svg class="chart"></svg>
      <div class="notes"></div>
    </div>
  </body>

</html>
              
            
!

CSS

              
                body {
  background-color: #ddd;
  background-repeat: repeat;
  background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAACgCAMAAACsXRuGAAAADFBMVEWIiIjd3d3y8vLm5uZzdc63AAAAbklEQVR42u3ZIQ4AIAwDwI39/8/wAlTFxFUgm5ALmFZV51Iv52Vyx/7CzhdONr3/ylCgQIECBQoUKFCgQIECBQoUKFCgQIECBcrmccGe4qX4vqBAgQIFChQoUKBAgQIFChQoUKBAgQIFij3lX3gBXBBzc8FaGvIAAAAASUVORK5CYII=)
}

.chart {
  margin: 0px auto;
  padding: 0px;
  background-color: white;
}

.notes {
  font-size: 12px;
  font-family: sans-serif;
  color: steelblue;
  padding: 20px;
  text-align: center;
}

.title {
  font-size: 2.5em;
  font-family: sans-serif;
  color: steelblue;
  text-align: center;
  padding: 15px 0px 5px 0px;
}

p {
  margin: 0;
}

.chart .mouseover {
  fill: lightsteelblue;
}

.chart rect,
.chart .mouseoff {
  fill: steelblue;
}

.chart text {
  fill: black;
  font: 10px sans-serif;
  text-anchor: end;
}

.axis text {
  font: 15px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.card {
  background-color: white;
  margin: 40px auto;
  padding: 0px;
  width: 1050px;
  height: 640px;
  box-shadow: 0 3px 6px 0 rgba(0, 0, 0, 0.32), 0 3px 12px 0 rgba(0, 0, 0, 0.24);
}

.tooltip {
  position: absolute;
  text-align: left;
  width: auto;
  height: auto;
  padding: 3px 5px 3px 5px;
  font: 1em sans-serif;
  border: 0px;
  border-radius: 5px;
  pointer-events: none;
  box-shadow: 3px 3px 6px rgba(0, 0, 0, 0.2);
  background-color: lightsteelblue;
}

.amount {
  font-size: 1em;
  font-weight: bold;
}

.year {
  font-size: 0.8em;
  font-weight: normal;
}
              
            
!

JS

              
                $('document').ready(function() {

  var url = 'http://2am.ninja/json/gdp.json';

  var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

  var formatCurrency = d3.format("$,.2f");

  $.get(url).success(function(jsonData) {
    var data = jsonData.data;

    console.log(data);
    console.log(JSON.stringify(jsonData));

    d3.select(".notes")
      .append("text")
      .text(jsonData.description);

    var margin = {
        top: 5,
        right: 10,
        bottom: 30,
        left: 75
      },
      width = 1000 - margin.left - margin.right,
      height = 500 - margin.top - margin.bottom;

    var barWidth = Math.ceil(width / data.length);

    minDate = new Date(data[0][0]);
    maxDate = new Date(data[274][0]);

    var x = d3.time.scale()
      .domain([minDate, maxDate])
      .range([0, width]);

    var y = d3.scale.linear()
      .range([height, 0])
      .domain([0, d3.max(data, function(d) {
        return d[1];
      })]);

    var xAxis = d3.svg.axis()
      .scale(x)
      .orient("bottom")
      .ticks(d3.time.years, 5);

    var yAxis = d3.svg.axis()
      .scale(y)
      .orient("left")
      .ticks(10, "");

    var infobox = d3.select(".infobox");

    var div = d3.select(".card").append("div")
      .attr("class", "tooltip")
      .style("opacity", 0);

    var chart = d3.select(".chart")
      .attr("width", width + margin.left + margin.right)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

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

    chart.append("g")
      .attr("class", "y axis")
      .call(yAxis)
      .append("text")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", "0.8em")
      .style("text-anchor", "end")
      .text("Gross Domestic Product, USA");

    chart.selectAll(".bar")
      .data(data)
      .enter().append("rect")
      .attr("class", "bar")
      .attr("x", function(d) {
        return x(new Date(d[0]));
      })
      .attr("y", function(d) {
        return y(d[1]);
      })
      .attr("height", function(d) {
        return height - y(d[1]);
      })
      .attr("width", barWidth)
      .on("mouseover", function(d) {
        var rect = d3.select(this);
        rect.attr("class", "mouseover");
        var currentDateTime = new Date(d[0]);
        var year = currentDateTime.getFullYear();
        var month = currentDateTime.getMonth();
        var dollars = d[1];
        div.transition()
          .duration(200)
          .style("opacity", 0.9);
        div.html("<span class='amount'>" + formatCurrency(dollars) + "&nbsp;Billion </span><br><span class='year'>" + year + ' - ' + months[month] + "</span>")
          .style("left", (d3.event.pageX + 5) + "px")
          .style("top", (d3.event.pageY - 50) + "px");
      })
      .on("mouseout", function() {
        var rect = d3.select(this);
        rect.attr("class", "mouseoff");
        div.transition()
          .duration(500)
          .style("opacity", 0);
      });

  });

});
              
            
!
999px

Console