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=""></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";
  // });

🕑 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.