cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

            
              <h1>Visualize Data with a Heat Map</h1>
<h3>FreeCodeCamp</h3>
<p><a href="https://youtu.be/w4jAkHd7qlE" target="_blank">Video tutorial</a></p>

<div class="graph">
  <svg id="heatmap"></svg>
  <div class="tooltip"></div>
</div>

<!-- 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>
      </div>
      <!-- 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>
         <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>
        <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"> 
    </a>
      </div>
    <!-- end footer -->
				</div>
			</div>
		</div>
<!-- end of Footer -->
            
          
!
            
              body {
 background-color:lightgray;
 text-align: center;
}
h1, h3, p{
  text-align:center;
}

.graph {
  margin: 0 auto;
  background-color: white; 
}

.tooltip {
  background-color: lightsalmon;
  border: 1px solid black;
  width: 100px;
  opacity: 0;
  color: black;
  position: absolute;
}


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

.tableFooter{
  margin-top: 15px;
}
.footerSvg{
  width: 32px;
}
            
          
!
            
              var height = 500, width = 1000,
    margin = {
      left: 120, right: 20, top: 20, bottom: 70
    },
    url = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json",
    months = ["January","February","March","April","May","June","July","August","September","October","November","December"],
    color = ["#ef5350","#EC407A","#AB47BC","#7E57C2","#5C6BC0","#42A5F5","#26C6DA","#26A69A","#D4E157","#FFEE58","#FFA726"];

var canvas = d3.select('svg').attr({
  height: height + margin.top + margin.bottom,
  width: width + margin.left + margin.right
});

var group = canvas.append('g').attr({
  transform: "translate(" + margin.left + "," + margin.top + ")"
});

var div = d3.select('.tooltip');
var xScale = d3.time.scale().range([0,width]);
var yScale = d3.scale.ordinal().domain(months).rangeBands([0,height]);
var colorScale = d3.scale.quantize().range(color);


//call the json with the url and a function
d3.json(url,function(data){
  data = data.monthlyVariance;
  //map the data
  data.map(function(d){
    d.month = months[d.month-1];
    d.year = d3.time.format("%Y").parse(d.year.toString());
  })
  
  //declair the function xScale
  xScale.domain(d3.extent(data,function(data){
    return data.year;
  }));
  //scale the colors
  colorScale.domain(d3.extent(data,function(d){
    return d.variance;
  }));
  
  var barWidth = width / (data.length / 12)
  var barHeight = height / 12;
  //scale the axis and set orientation
  var xAxis = d3.svg.axis().scale(xScale).orient("bottom");
  var yAxis = d3.svg.axis().scale(yScale).orient("left").ticks(12);
  
  //add a g element that provides a reference point for adding the axes.
  //append the axis according to the size and the position that we have set up with our width, height and margin’s.
  group.append('g').attr({
    class: "xAxis",
    transform: "translate(0," + (height) + ")"
  }).call(xAxis);
  group.append('g').attr({
    class: "yAxis",
    transform: "translate(0,0)",
  }).call(yAxis);
  group.selectAll('g').data(data).enter().append('g').attr({
    transform: function(data){
      return "translate(" + xScale(data.year) + "," + yScale(data.month) +  ")";
    }
    }).append('rect').attr({ //append the rectangulars and scale them in the graph
    width: barWidth,
    height: yScale.rangeBand()
  }).style({ //scale the colors
    fill: function(data){
     return colorScale(data.variance);
    }
  }).on("mouseover",function(d){
    //make the tooltip to follow the mouse
    div.transition().duration(10).style("opacity",0.8).style({
      left: d3.event.pageX + "px",
      top: d3.event.pageY + "px"
    });
    
    //appear/disappear the tooltip on mouse hover
    //8.66 is a standard value
    div.html("<p>Year: " + d3.time.format("%Y")(d.year) + "</p></p>Value = " + (8.66 + d.variance).toFixed(2) + "</p><p>Month: " + d.month + "</p>" );
  }).on("mouseout",function(d){
    div.transition().duration(100).style("opacity",0);
  });

});
            
          
!
999px
🕑 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 ..................

Console