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.

      Gross Domestic Product

  position: relative

#chart text, .axis text, .description  tspan, .tooltip
  fill: #444
  font-size: 12px
  font-weight: 700
  font-family: 'Cutive Mono', monospace
.axis .path,
.axis .line,
.domain, line 
  fill: none
  stroke: #444

  opacity: 0
  background: #fff
  box-shadow: 0 0 5px rgba(0,0,0,.1)
  color: #444
  padding: 10px 0
  overflow: hidden
  position: absolute
  width: 160px
  overflow: hidden
  white-space: nowrap
  border-radius: 25px 
  text-align: center
  border: solid 1px #eee
.tooltip > span
  display: block

.tooltip > span:first-child
  color: rgb(132, 200, 70)
  font-weight: 700

  stroke: rgba(0,0,0,0)
  stroke: #efefef 

  padding: 20px 0
#descriptionText, #heading
  color: #444
  font-size: 12px
  font-weight: 700
  font-family: 'Cutive Mono', monospace
  text-align: center

  text-align: center
  font-size: 20px
  margin: 10px 0

  padding: 0 10px 
  margin: 0
function plot() {
  // JSON url
var url = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json";

// Setting margin
var margin = {top: 30, bottom: 40, left: 70, right:35};

// Assigning width and height to vars for svg
var width = window.innerWidth - margin.right - margin.left;
var height = (window.innerHeight - 140 - margin.bottom - margin.top);

// Scale for y axis
var y = d3.scaleLinear()
        .range([height, 0]);
// Scale for handling data of x-axis
var x = d3.scaleBand()
        .range([0, width]);
// Scale for x axis
var xD = d3.scaleTime()
        .range([0, width]);
// Scale for colors of rect
var helperScale = d3.scaleLinear()
        .range([1, .5])
        .domain([0, height]);
var colorScale = d3.interpolateCool;
// Defining x-axis and y-axis
if(width < 600)
  ticks = 7;
  ticks = 12;
var xaxis = d3.axisBottom(xD)
var yaxis = d3.axisLeft(y);

// Setting svg element's height
var chart = d3.select("#chart")
  .attr("width", width + margin.right + margin.left)
  .attr("height", height + margin.bottom + margin.top)
  .attr("transform", "translate(" + margin.left + "," + margin.top + ")" );
// Sending get request
$.getJSON(url, function(data, status) {
  // Unsuccesful get request case
  if(status !== "success") {
    alert("Data fetching failed!");
  // Formatting data
  var formattedData = data["data"].map(function(elem) {
    return {x: new Date(elem[0]), y: elem[1]}; 
  // Setting Domain
  y.domain([0, d3.max(formattedData.map(function(e){return e.y;}))]);
  x.domain(formattedData.map(function(e){return new Date(e.x)}));
  xD.domain( [ new Date(formattedData[0]["x"]), new Date(formattedData[formattedData.length - 1]["x"]) ]);
  // Adding x-axis
      .attr("class", "x axis")
      .attr("transform", "translate(0," + height + ")")
  // adding y-axis
    .attr("class", "y axis")
    .attr("y", "15")
    .attr("class", "yinfo")
    .text("Gross Domestic Product, USA")
  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
               'Sep' ,'Oct', 'Nov', 'Dec']; 
  // Tooltip object
  var div = d3.select(".tooltip");
  // adding rects 
  var bar = chart.selectAll(".rect")
    .attr("transform", function(d, i) { return "translate(" + x(d.x) + ", 0)"; })
    .attr("class", "rect")
    .attr("y", function(d){ return y(d.y);})
    .attr("height", function(d){ return height - y(d.y);})
    .attr("width", x.step())
    .style("fill", function(d){ return colorScale(helperScale(y(d.y)))})
    .on("mouseover", function(d) {
        .style("opacity", 1);
      div.html('<span class="gdp">'+ d.y + " Billion $" + '</span><span class="date">' + months[d.x.getMonth()] + ", " + (d.x.getFullYear()) + '</span>')
        .style("left", (((- d3.event.pageX + $("body").width()) > $("body").width()/2) ?  d3.event.pageX + 10 : d3.event.pageX - 170 )+ "px")
        .style("top", (d3.event.pageY - 60) + "px");
    .on("mouseout", function(d) {
        .style("opacity", 0);
});// getJSON ends

// problems
// x.step() and x.bandwidth()
//why after getJSON axis ranges from 0 to 1
// ticks and tickFormat - scale difference

function addDescription(description) {

$(document).ready(function() {
  // for ie 9
  if(window.attachEvent) {
    window.attachEvent('onresize', function() {
        alert('attachEvent - resize');
  else if(window.addEventListener) {
    window.addEventListener('resize', function() {
    }, true);
🕑 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.