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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <!-- CODED BY valmassoi -->

<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css"/>

<div class="chart centered">
  <h2><b>Monthly Global Land-Surface Temperature</b></h2>
  <div class="graph"></div>
  <p id="footnote"></p>
</div>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
            
          
!
            
              body{
  margin-top: 50px;
  background: grey;
  background-image: url(https://subtlepatterns.com/patterns/old_mathematics.png);
}
h2{
  color: steelblue;
  padding-top: 8px;
}
h5{
  font-weight: bold;
}
p{
  padding: 0px 10px;
}
.centered {
  margin: 0px auto;
  text-align: center;
}
.chart{
  background: white;
  width: 900px;
  height: 620px;
  box-shadow: 0px 0px 20px #000;
}
.graph{
  text-align: left;
}
.graph rect {
  fill: steelblue;
}
.graph rect:hover {
  fill-opacity: 0.8;
}

.axis text {
  font: 12px sans-serif;
}

.axis path,
.axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}
div.tooltip {   
  position: absolute;           
  text-align: center; 
  padding: 5px;
  width: 150px;                  
  height: 55px;                        
  font: 12px sans-serif;        
  background: black;
  color: white;
  border: 0px;      
  border-radius: 4px;           
  pointer-events: none;         
}

            
          
!
            
              const url = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json";
var jsonData = [];
var baseTemp = 0;

const colors = [
  '#5e519f',
  '#3788ba',
  '#6ac1a5',
  '#acdca6',
  '#e6f49d',
  '#fffec2',
  '#fddf90',
  '#f26d4a',
  '#d34052',
  '#9a0942',
  '#ff0000' 
];

$(function() {
  $.getJSON(url, function(json) {
    //console.log(JSON.stringify(json.data));
    jsonData = json.monthlyVariance;
    baseTemp = json.baseTemperature;
    graph();
  });
});

function graph() {
  const margin = {top: 20, right: 50, bottom: 30, left: 85},
    width = 900 - margin.left - margin.right,
    height = 500 - margin.top - margin.bottom;
  
  
  const min = d3.min(jsonData.map(d => d.variance));
  const max = d3.max(jsonData.map(d => d.variance));
  const colorScale = d3.scale.quantile()
    .domain([min + baseTemp, max + baseTemp])
    .range(colors);
  
  var length = jsonData.length-1, year0 = jsonData[0].year;
  var x = d3.time.scale()
          .range([0, width]);
  var y = d3.time.scale()
          .range([0, height]);
  x.domain([ new Date(year0, 0), new Date(jsonData[length].year, 0) ]);
  y.domain([new Date(2016, 0, 1), new Date(2016, 11, 31)]);
  
  var canvas = d3.select(".graph")
                 .append("svg")
                 .attr("width", width + margin.left + margin.right)
                 .attr("height", height + margin.top + margin.bottom)
              .append("g")
                  .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  var barWidth = width / (length/12); //15;//change to width / NO. years / 12
  var barHeight = height/12;//change to height / 12
  var bars = canvas.selectAll("rect")
                   .data(jsonData)
                   .enter().append("rect")
                      .attr("x", function(d, i){ return (d.year - year0) * barWidth; })//year
                      .attr("y", function(d) { return (d.month -1) * barHeight; })//month
                      .attr("height", barHeight)
                      .attr("width", barWidth)
                      .style('fill', (d) => colorScale(d.variance + baseTemp))//CHANGE
  
  .on("mouseover", function(d) {//TOOLTIP      
            div.transition()        
                .duration(200)      
                .style("opacity", .9);      
            div .html("<b>"+d.year+"</b>"+
                      "<br>"+(baseTemp + d.variance).toFixed(3) +" °C"+
                      "<br>"+d.variance.toFixed(3) +" °C"
                     )  
                .style("left", (d3.event.pageX + 3) + "px")     
                .style("top", (d3.event.pageY - 55) + "px");    
            })                  
        .on("mouseout", function(d) {       
            div.transition()        
                .duration(500)      
                .style("opacity", 0);   
        });

  //AXIS
  var format = d3.format("0000");
  var xAxis = d3.svg.axis()
    .scale(x)
    .orient("bottom")
    .tickFormat(d3.time.format('%Y'));
  var yAxis = d3.svg.axis()
    .scale(y)
    .ticks(d3.time.months)
  .tickFormat(d3.time.format('%B'))
    .orient("left");
  canvas.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis)
  .append("text")
      .attr("class", "label")
      .attr("x", width/2)
      .attr("y", 30)
      .style("text-anchor", "end")
      .text("Year");
  canvas.append("g")
    .attr("class", "y axis")
    .call(yAxis);

  //TOOLTIP
   var div = d3.select("body").append("div")   
    .attr("class", "tooltip")               
    .style("opacity", 0);
  
  //LEGEND
  const legend = [0,2.8,4,5.1,6.2,7.3,8.4,9.5,10.6,11.7,12.8];
  const x_axis = 520;
  const y_axis = 0;
  
  var rectWidth = 30;
  
  var svgContainer = d3.select(".graph").append("svg")
                                    .attr("width", rectWidth*legend.length+x_axis)
                                    .attr("height", 200);
  
  var rect = svgContainer.selectAll(".rect")
                          .data(legend)
                          .enter()
                          .append("rect");
  var rectAttributes = rect
                       .attr("x", (d,i) => x_axis + (rectWidth*i))
                       .attr("y", function (d, i) { return y_axis; })
                       .attr("width", rectWidth)
                       .attr("height", 20)
                       .style("fill", (d) => colorScale(d));
  svgContainer.selectAll('.text')
                        .data(legend)
                        .enter().append('text')
                        .text((d) => d.toString())
                        .attr("x", (d,i) => x_axis + (rectWidth*i))
                        .attr("y", y_axis + 35);
  
}
            
          
!
999px
Loading ..................

Console