css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

              <div class="graph" id="barGraph">
  <h1><a href="https://www.freecodecamp.org/challenges/visualize-data-with-a-bar-chart" target="_blank">Visualize Data with a Bar Chart</a></h1>
  <h5 class="text-center"><a href="https://youtu.be/SDKDH7157IM" target="_blank">Video Tutorial</a></h5>

<!-- Footer-->
<div class="row text-center">
			<div class="col-xs-12">
				<div class="tableFooter">
				  <!-- for footer -->
      <div class="col-xs-6">
      <h5><a href="http://www.eleftheriabatsou.com" target="_blank">Eleftheria</a> | <a href="https://codepen.io/EleftheriaBatsou" target="_blank">Projects</a> | <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank"> Coding videos </a></h5>
      <!-- social media -->
      <div class="col-xs-6 text-primary">
        <a href="https://twitter.com/BatsouElef" target="_blank">
     <img class="footerSvg"src="https://image.flaticon.com/icons/svg/60/60580.svg" 
      style="text-align:center" alt="twitter"> 
         <a href="https://github.com/EleftheriaBatsou" target="_blank">
     <img class="footerSvg" src="https://image.flaticon.com/icons/svg/25/25657.svg" 
      style="text-align:center" alt="github"> 
        <a href="https://www.youtube.com/channel/UCC-WwYv3DEW7Nkm_IP6VeQQ" target="_blank">
     <img class="footerSvg" src="https://image.flaticon.com/icons/svg/25/25312.svg" 
       style="text-align:center" alt="youtube"> 
    <!-- end footer -->
<!-- end of Footer -->
              body {
h1, h3, p{
.axis {
    font: 15px Arial, sans-serif;
.graph-svg-component {
  display: block;
  margin: auto;
.axis path, .axis line {
    fill: none;
    stroke: #3A3A3A;

 * Footer
a:link    {color:orangered;  text-decoration:none}
a:visited {color:orangered; text-decoration:none}
a:hover   {color:red; text-decoration:underline}

  margin-top: 15px;
  width: 32px;
 function DrawBar(dataset){
   //our JavaScript section starts and the first thing that happens is that we set the size of the area that we’re going to use for the chart and the margins;
    var margin  = {top: 50, right: 20, bottom: 50, left: 100},
        width   = 800,
        height  = 400

    //The next section declares the function to determine positioning in the x and y domain
    var minDate = dataset[0][0].substr(0,4);
        minDate = new Date(minDate);
    var maxDate = dataset[dataset.length - 1][0].substr(0,4);
        maxDate = new Date(maxDate);
   // x-axis: start from 0 and keep giong until the last date,
   // if we don't use range everything is upon each other
   var xAxisScale = d3.time.scale()
        .domain([minDate, maxDate])
        .range([0, width]);
    var yAxisScale = d3.scale.linear()
         .domain([0, d3.max(dataset, function(d) { 
           //get the 2nd column of json file
            return d[1]; 
         .range([height, 0]);
   //The declarations for our two axes are relatively simple  
   //show data (nums) under the x-axis line
    var xAxis = d3.svg.axis().scale(xAxisScale).orient("bottom"); 
   //show data (nums) left of the y-axis line
    var yAxis = d3.svg.axis().scale(yAxisScale).orient("left");
   var tooltip = d3.select('body').append('div')
        'position' : 'absolute',
        'padding' : '4px',
        'background' : '#fff',
        'border': '1px solid #000',

function mouseoverHandler(d) {
   tooltip.transition().style('opacity', .8)
            'left' : (d3.event.pageX + 10) + 'px',
            'top' : (d3.event.pageY + 15) + 'px'
          .html('<p> Date: ' + d[0] + '</p>'
                  + '<p> Billions: ' + d[1] + '</p>')
      .style('opacity', .1);

function mouseoutHandler(d) {
    tooltip.transition().style('opacity', 0)  
      .style('opacity', 1);

function mouseMoving (d) {
    tooltip.style("top", (d3.event.pageY-10)+"px").style("left",(d3.event.pageX+10)+"px");
      .style('opacity', 0.8);
* The next block of code selects the id #barGraph on the web page 
* and appends an svg object to it of the size 
* that we have set up with our width, height and margin’s.
* It also adds a g element that provides a reference point for adding our axes.
var svg= d3.select("#barGraph")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .attr("class", "graph-svg-component")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  * we add the bars to our chart
  * This block of code creates the bars (selectAll("bar")) 
  * and associates each of them with a data set (.data(dataset)).
  * We then append a rectangle (.append("rect")) 
  * with values for x/y position and height/width as configured in our earlier code.
       .style("fill", "orangered")
             x: function(d, i) { return (i * (width / dataset.length)); },
             y: function(d) { return yAxisScale(d[1]); },
             width: (width / dataset.length),
             height: function(d) { return height - yAxisScale(d[1]); },
        .on('mouseover', mouseoverHandler)
      	.on("mouseout", mouseoutHandler);
   //we append our x axis
   * This is placed in the correct position with:
   * .attr("transform", "translate(0," + height + ")") 
   * and the text is positioned (using dx and dy) 
   * and rotated:
   * (.attr("transform", "rotate(-45)" );) so that it is aligned vertically.
            .attr("class", "x axis")
            .attr("transform", "translate(0, " + height + ")")
            .style("text-anchor", "end")
            .attr("dx", "-0.5em")
            .attr("dy", "-.55em")
            .attr("y", 30)
            .attr("transform", "rotate(-45)" )
   //we append our y axis in a similar way and append a label: text("Value (billions)")
            .attr("class", "y axis")
            .attr("transform", "rotate(-90)")
            .attr("y", -85)
            .attr("dy", "0.8em")
            .attr("text-anchor", "end")
            .text("Value (billions)");

//we call the data using d3.json and inside the function we call the function DrawBar
d3.json("https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json", function(data) {
 var dataset = data.data;

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