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.


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.


                <div id="container">
  <div id="title"><h1>U.S. Gross Domestic Product</h1></div>
  <svg width="960" height="500" id="chart"></svg>
  <div id="notes"></div>
<div id="credits">Code by <a href="">Luke Walker</a>, as my solution for <a href="">this Free Code Camp project</a>. See all my Data Visualization Projects and notes on <a href="">Github</a>.</div>
<div id="projectnotes">
  <h2>Some Notes</h2>
  <p>I found the following resources helpful in constructing this chart:</p>
    <li><strong>IMPORTANT!</strong> <a href="">This slideshow</a> was very helpful when it came to understanding the many differences (breaking diffs) between v3 and v4 of d3. Most of the tutorials on the d3 site are still written for v3 and won't work if you use the v4 library.</li>
    <li><a href="">A sample v4 bar chart</a></li>
    <li><a href="">A sample barchart with tooltips</a></li>
    <li><a href="">This Stack Overflow question</a> helped me sort out how to make the labels on the x axis work in v4 as they do on the sample chart.</li>


                body {
  background-color: lightgray;

#container {
  background-color: white;

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

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
.axis text {
  font: 10px sans-serif;

.bar { 
  fill: steelblue; 

.mouseover {
  fill: lightgray;

.tooltip {
  position: absolute;
  text-align: center;
  width: 140px;
  height: 40px;
  padding: 10px 5px;
  font: 16px sans-serif;
  background: lightsteelblue;
  border: 0px;
  border-radius: 8px;
  display: none;
  opacity: 0.8;

.date, .dollars {
  display: block;
  padding-bottom: 5px;

.date {
  font-weight: 700;

#notes {
  font-family: sans-serif;
  font-size: 12px;
  text-align: center;

#container {
  border: 1px solid black;
  width: 960px;
  height: 600px;
  margin: 10px auto;
  padding: 10px 30px;

#chart {
  margin : 20px;

#title {
  width: 100%;

#title h1 {
  font-family: sans-serif;
  font-size: 30px;
  text-align: center;
  color: steelblue;

#projectnotes {
  width: 960px;
  margin: 10px auto;
  font-size: 14px;
  font-family: sans-serif;

#projectnotes h2 {
  color: steelblue;
  font-size: 18px;
#projectnotes ul {
  list-style: disc outside none;
  margin: 10px 50px;

#projectnotes li {
    line-height: 1.3;

#credits {
  font-family: sans-serif;
  font-size: 14px;
  width: 960px;
  text-align: center;
  margin: 20px auto;
  border: 1px solid black;
  padding: 5px;
  background-color: white;


                var dataURL = '',
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    formatBillions = d3.format("$,.1f");

  var tooltip ='body').append("div").attr("class", "tooltip");

$.get(dataURL, function(response, status) {
  var svg ="svg"),
      margin = {top: 20, right: 20, bottom: 40, left: 40},
      width = +svg.attr("width") - margin.left - margin.right,
      height = +svg.attr("height") - - margin.bottom;

  var x = d3.scaleTime().range([0, width]),
      y = d3.scaleLinear().rangeRound([height, 0]);

  var g = svg.append("g")
      .attr("transform", "translate(" + margin.left + "," + + ")");

  var responseJSON = JSON.parse(response);'#notes')
  var dataArr  =;  
  var data =[];

  dataArr.forEach(function(d) {
    var obj = {}; = new Date(d[0]);
    obj.gdp = d[1];
  var chartWidth = Math.ceil(width / data.length);
  var dateA = data[0].date;
  var dateZ = data[data.length - 1].date;
  x.domain([dateA, dateZ]);
  y.domain([0, d3.max(data, function(d) { return d.gdp; })]);

      .attr("class", "axis axis--x")
      .attr("transform", "translate(0," + height + ")")
      .call(d3.axisBottom(x).ticks(d3.timeYear, 5).tickFormat(d3.timeFormat("%Y")).tickSizeOuter(0))
        .style("text-anchor", "end")
        .attr("dx", "-.8em")
        .attr("dy", ".15em")
        .attr("transform", "rotate(-65)" );
      .attr("class", "axis axis--y")
      .attr("transform", "rotate(-90)")
      .attr("y", 6)
      .attr("dy", "0.71em")
      .attr("text-anchor", "end")
      .text("U.S Gross Domestic Product (in Billions of $)");

      .attr("class", "bar")
      .attr("x", function(d) { return x(; })
      .attr("y", function(d) { return y(d.gdp); })
      .attr("width", chartWidth)
      .attr("height", function(d) { return height - y(d.gdp); })
      .on("mouseover", showTip)
      .on("mouseout", hideTip);

function showTip(d) {
  var bar =;
  bar.attr("class", "mouseover");
  var year =;
  var month =;
  tooltip.html('<span class="date">' + months[month] + ' ' + year + '</span><span class="dollars">' + formatBillions(d.gdp) + ' billion</span>')
  .style('left', (d3.event.pageX + 10) + 'px')
  .style('top', (d3.event.pageY + 10) + 'px')
  .style('display', 'block');

function hideTip(d) {
  var bar =;
  bar.attr('class', 'bar')'display', 'none');