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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

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


                <!DOCTYPE html>
<html lang="en">

  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="style.css" />
  <title>Visualize Data with a Bar Chart</title>

  <h1>Visualize Data with a Bar Chart</h1>

  <div class="container">
    <div id="title">United States GDP</div>
    <div class="svg-container"></div>



                @import url(",400i,700");

body {
  background-color: rgb(83, 102, 102);
  font-family: Roboto, sans-serif;
  margin: 0;
  box-sizing: border-box;

h1 {
  color: #333;
  text-align: center;

.container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80%;
  min-height: 80vh;

#title {
  z-index: 1;
  font-size: 2em;
  position: absolute;
  top: 0;
  padding-top: 80px;

.svg-container {
  background-color: #fff;
  box-shadow: 2px 2px 10px 10px#413f3f;
  position: absolute;

.bar-chart {
  background-color: transparent;
  padding: 30px;
  padding-top: 100px;
  padding-bottom: 100px;

.bar {
  display: inline-block;
  fill: rgb(24, 78, 42);

.bar:hover {
  fill: rgb(238, 179, 17);

.tooltip {
  background-color: rgb(235, 235, 235);
  border: 1px solid #999;
  border-radius: 10px;
  padding: 5px;
  font-weight: bold;
  text-align: center;
  position: absolute;
  box-sizing: border-box;


                // get data
d3.json("").then(function (dataSet) {
  const data =;
  const w = 800;
  const h = 400;
  const padding = 50;

  // create a tooltip
  var tooltip ='.svg-container')
    .attr("id", "tooltip")
    .attr('class', 'tooltip')
    .style("opacity", 0)

  const xScale = d3.scaleTime()
        new Date(d3.min(data, (d) => d[0])),
        new Date(d3.max(data, (d) => d[0]))
    .range([padding, w])

  const barWidth = (w - padding) / data.length;

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

  const svg =".svg-container")
    .attr("class", "bar-chart")
    .attr("width", w)
    .attr("height", h+30)
    .style("padding-bottom", '50px');

  // RECT
    .attr("class", "bar")
    .attr("width", barWidth)
    .attr("height", d => h - yScale(d[1]))
    .attr("data-date", d => d[0])
    .attr("data-gdp", d => d[1])
    .attr("x", (d, i) => (i * barWidth) + padding)
    .attr("y", (d) => yScale(d[1]) - padding + 20)

    .on("mouseover", function (d, i) {
        .style('opacity', .9)
      tooltip.html('<p>Date:' + d[0] + '</p>' + '<p>Data:' + d[1] + '</p>')
        .attr("data-date", d[0])
        .style('left', (i * barWidth) + 30 + 'px')
        .style('top', h - 200 + 'px');

    .on("mouseout", function (d, i) {
        .style('opacity', 0)

    .attr('transform', 'rotate(-90)')
    .attr('x', -200)
    .attr('y', 70)
    .text('Gross Domestic Product');
    .attr('x', 320)
    .attr('y', 420)
    .text('More Information:');

  // AXIS
  const xAxis = d3.axisBottom(xScale);
  const yAxis = d3.axisLeft(yScale);

    .attr("transform", "translate(0," + (h - padding + 20) + ")")
    .attr("id", "x-axis")
    .attr("class", "tick")

    .attr("transform", "translate(50, -30)")
    .attr("id", "y-axis")
    .attr("class", "tick")