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


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.


                <!--<script src=""></script>-->

<title id="title">United States GDP</title>
<h1>United States GDP</h1>


                svg {
    background-color: #fff;

  display: grid;
  align-content: center;
  justify-content: center;
  background: #777;

  text-align: center;
  color: #fff;

 fill: green; 

  fill: purple;


.tooltip {
  position: absolute;
  text-align: center;
  width: auto;					
  height: auto;					
  padding: 2px;				
  font: 16px sans-serif;		
  background: lavender;	
  border: 0px;			
  pointer-events: none;


                // Fetch Data
document.addEventListener("DOMContentLoaded", function () {
  const req = new XMLHttpRequest();
  req.onload = function () {
    const json = JSON.parse(req.responseText);

    // This is the data in an array (the first box is date, the second is gdp)

    const dataset =;
    const padding = 50;
    const w = 800;
    const barW = (w - padding * 2) / dataset.length;
    const scaleW = w / dataset.length;
    const h = 500;

    const dateRegex = /\d{4}/;
    const dateArray = => item[0].match(dateRegex));
    const unixArray = => new Date(item[0]).getTime() / 1000);

    const minDate = d3.min(dataset, (d) => new Date(d[0]));
    const maxDate = d3.max(dataset, (d) => new Date(d[0]));

    dataset.forEach((item) => item.push(new Date(item[0]).getTime() / 1000));

    // Scales

    const xScale = d3
      .domain([minDate, maxDate])
      .range([padding, w - padding]);

    const yScale = d3
      .domain([0, d3.max(dataset, (d) => d[1])])
      .range([h - padding, padding]);

    const unixScale = d3
      .domain([d3.min(dataset, (d) => d[2]), d3.max(dataset, (d) => d[2])])
      .range([padding, w - padding]);

    const svg = d3
      .attr("width", w)
      .attr("height", h);

    const xAxis = d3.axisBottom(xScale);

    const yAxis = d3.axisLeft(yScale);

    // X Axis
      .attr("transform", "translate( 0, " + (h - padding) + ")")
      .attr("id", "x-axis")

    // Y Axis
      .attr("transform", "translate(" + padding + ", 0)")
      .attr("id", "y-axis");


    let tooltip = d3
      .attr("class", "tooltip")
      .attr("id", "tooltip");

      .attr("data-date", (d) => d[0])
      .attr("data-gdp", (d) => d[1])
      .attr("width", scaleW)
      .attr("x", function (d, i) {
        return unixScale(d[2]);
      .attr("height", (d, i) => {
        return h - padding - yScale(d[1]);
      .attr("y", (d, i) => {
        return yScale(d[1]);
      .attr("class", "bar")
      .on("mouseover", function (d) {
        tooltip.transition().style("opacity", 0.9);
          .html(d[0] + "<br>$" + d[1] + " Billion")
          .style("left", d3.event.pageX + 20 + "px")
          .style("top", d3.event.pageY + "px");
        tooltip.attr("data-date", d[0]);
      .on("mouseout", function (d) {
        tooltip.transition().duration(400).style("opacity", 0);