Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ add another resource

JavaScript

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

Packages

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.

Behavior

Auto Save

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.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Josefin+Sans');

body {
   margin: 0 auto;
   font-family: 'Josefin Sans',sans-serif;
   h1 {
      font-size: 30px;
      margin-top: 25px;
   }
   h2 {
      font-size: 22px;
   }
   h3 {
      font-size: 15px;
   }
   #graphcontainer {
      margin: 0 auto;
   }
}
              
            
!

JS

              
                // jshint esversion:6
/*

User Story: I can view a heat map with data represented both on the Y and X axis.

User Story: Each cell is colored based its relationship to other data.

User Story: I can mouse over a cell in the heat map to get more exact information.

API sample:

{
  "baseTemperature": 8.66,
  "monthlyVariance": [
    {
      "year": 1753,
      "month": 1,
      "variance": -1.366
    },
    {
      "year": 1753,
      "month": 2,
      "variance": -2.223
    },
    {
      "year": 1753,
      "month": 3,
      "variance": 0.211
    }
 }

*/

const URL = "https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json";

class App extends React.Component {
   
   constructor() {
    super();
    this.state = {
      data: [],
      baseTemp: "",
      monthsInfo: [],
      colorscheme: ["#923FF5","#7A9ECC","#99C6FF","#FFDF52","#FFDF80","#FFB42D","#CC9024","#996C1B","#A00303"],
      months: ["Jan.","Feb.","Mar","April","May","June","July","Aug.","Sept.","Oct.","Nov.","Dec."]
    };
  }

   
   componentDidMount(){
   d3.json(URL, (data) => {
      //getting all data from api
      //setting states
      this.setState({
        data: data,
        baseTemp: data.baseTemperature,
        monthsInfo: data.monthlyVariance
      });
      
    });
   }
   
   _heatmapGraph(){
      //build the graph with d3!
      let data = this.state.monthsInfo;
      let base = this.state.baseTemp;
      let mon = this.state.months;
      
      const MARGINS = {
         top: 15,
         left: 100,
         right: 30,
         bottom: 100
      }
      
      const WIDTH = 1200 - MARGINS.left - MARGINS.right;
      const HEIGHT = 600 - MARGINS.top - MARGINS.bottom;

      
      let tooltip = d3.select('body').append('div')
         .style('color', 'white')
         .style('position', 'absolute')
         .style('background', 'rgba(0,0,0,0.7)')
         .style('padding', '15 15px')
         .style('border', '2px black solid')
         .style('border-radius', '5px')
         .style('font-size', '15px')
         .style('opacity', '0')
         .data(data);
      
      let yScale = d3.scaleLinear()
                     .domain([0,d3.max(data,function(d){return d.month;})])
                     .range([0, HEIGHT]);

      let xScale = d3.scaleLinear()
                     .domain([d3.min(data,function(d){return d.year;}),d3.max(data,function(d){return d.year;})])
                     .range([0, WIDTH]);
      
      let color = d3.scaleQuantile()
                    .domain([d3.min(data,function(d){return base + d.variance;}),d3.max(data,function(d){return base + d.variance;})])
                    .range(this.state.colorscheme)
                    
      
      let monthConvert = function(d){
         let m = mon;
         for(let j = 0; j<m.length; j++){
         if(d - 1 === j){
            return m[j];
         }
        }
      }
      
      let heatmap = d3.select('#heatmap').append('svg')
                      .attr('width', WIDTH + MARGINS.left + MARGINS.right)
                      .attr('height', HEIGHT + MARGINS.top + MARGINS.bottom)
                      .append('g')
                      .attr('transform', 'translate(' + MARGINS.left + ',-' + (MARGINS.top + 15) + ')')
                      .selectAll('rect')
                      .data(data)
                      .enter()
                        .append('rect')
                        .style('fill',function(d){
                           return color(base + d.variance);
                        })
                        .attr('width','10px')
                        .attr('height', '45px')
                        .attr('x',function(d){return xScale(d.year);})
                        .attr('y',function(d){return yScale(d.month);})
      
                        .on('mouseover', function(d, i) {

         tooltip.transition()
            .style('opacity', 1);

         tooltip.html(function() {
               let month = monthConvert(data[i].month);
               let temperature = base + data[i].variance;
               return month + " " + data[i].year + "<br>" + temperature + "*C" + "<br>" + "Difference: " + data[i].variance;

            })
            .style('left', (d3.event.pageX) + 'px')
            .style('top', (d3.event.pageY) + 'px')
         d3.select(this).style('opacity', '0.8')
            .style('fill', 'blue');
      })

      .on('mouseout', function(d, i) {
         tooltip.transition()
            .style('opacity', 0);

         d3.select(this).style('opacity', '1')
            .style('fill', color(base + d.variance))

      });
                        
     
      let vScale = d3.scaleLinear()
         .domain([0,d3.max(data,function(d){return d.month;})])
         .range([0, HEIGHT]);

      let vAxis = d3.axisLeft()
         .scale(vScale)
         .ticks(12)
         .tickPadding(5)
         .tickFormat(function(d,i){
            return mon[i];
         });

      let vGuide = d3.select('svg')
         .append('g')
      vAxis(vGuide)
      vGuide.attr('id', 'vGuide')
      vGuide.attr('transform', 'translate(' + MARGINS.left + ',' + MARGINS.top + ')')
      vGuide.selectAll('path')
         .style('fill', 'none')
         .style('stroke', 'black')
      vGuide.selectAll('line')
         .style('stroke', 'black');

      let hScale = d3.scaleTime()
         .domain([d3.min(data,function(d){return d.year;}), d3.max(data,function(d){return d.year;})])
         .range([0, WIDTH]);

      let hAxis = d3.axisBottom()
         .scale(hScale)
         .ticks(10)
         .tickFormat(function(d,i){
            return d * 1;
         });
        

      let hGuide = d3.select('svg')
         .append('g')
      hAxis(hGuide)
      hGuide.attr('id', 'hGuide')
      hGuide.attr('transform', 'translate(' + MARGINS.left + ',' + (HEIGHT + MARGINS.top) + ')')
      hGuide.selectAll('path')
         .style('fill', 'none')
         .style('stroke', 'black')
      hGuide.selectAll('line')
         .style('stroke', 'black');

      hGuide.append("text")
         .attr("text-anchor", "middle")
         .attr('x', (WIDTH / 2))
         .attr('y', 40)
         .style('fill','black')
         .text("Years");

      vGuide.append("text")
         .attr("text-anchor", "middle")
         .attr('x', -50)
         .attr("y", -50)
         .attr("transform", "rotate(-90)")
         .style('fill','black')
         .text("Months");

      
      let legendXScale = d3.scaleLinear()
                           .domain([0,this.state.colorscheme.length])
                           .range([0,400]);
      
      let legend = d3.select('svg').append('g')
                     .attr('width',400)
                     .attr('height',25)
                     .attr('transform','translate(800,0)')
                     .append('g')
                     .selectAll('rect')
                     .data(this.state.colorscheme)
                     .enter()
                     .append('rect')
                     .attr('width',(400 / this.state.colorscheme.length))
                     .attr('height',25)
                     .attr('x',function(d,i){return legendXScale(i);})
                     .attr('y',(HEIGHT + 50))
                     .style('fill',function(d){
                        return d;
                     });
      
      let textData = ["0-1.5","1.5-3","3-5","5-6.5","6.5-8","8-9","9-10","10-12","12-14"];
      let legendText = d3.select('svg').append('g')
                           .attr('transform','translate(335,20)')
                           .selectAll('text')
                           .data(textData)
                           .enter()
                           .append('text')
                           .attr('x',function(d,i){ return HEIGHT + (i * 45)})
                           .attr('y', (HEIGHT + 50))
                           .style('color','black')
                           .attr('text-anchor','middle')
                     .text(function(d,i){
                        return d;
                     });
                     
      
      
   }
   
   
   render(){
      let graph = this._heatmapGraph();
 
      return (
          <div>
            <h1 className="text-center">Free Code Camp</h1>
            <h1 className="text-center">Monthly Global Land Surface Temperature</h1>
            <h2 className="text-center">Years 1753-2015</h2>
            <h3 className="text-center">Temperatures are in Celsius and reported as anomalies relative to the Jan 1951-Dec 1980 average.
            </h3>
            <h3 className="text-center">Estimated Jan 1951-Dec 1980 absolute temperature ℃: 8.66 +/- 0.07</h3>
            <div id="graphcontainer">
            <div id="heatmap">
               {graph}
            </div>
            </div>
            
          </div>
      )
   }
}


ReactDOM.render(<App />,document.getElementById('app'));
              
            
!
999px

Console