cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-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.

            
              <div id="container">
  <div id="title">
    <h1>Monthly Global Land-Surface Temperature</h1>
    <h2>1753 - 2015</h2>
    <h3>Temperatures are in Celsius and reported as anomalies relative to the Jan 1951-Dec 1980 average.</h3>
    <h3>Estimated Jan 1951-Dec 1980 absolute temperature ℃: 8.66 +/- 0.07</h3>
  </div>
  <div id="chart"></div>
   <div id="notes">
    Code by <a href="http://lukewalker.org" targget="_blank">Luke Walker</a>. This is my solution to a <a href="https://www.freecodecamp.com/challenges/visualize-data-with-a-heat-map" target="_blank">Free Code Camp</a> project. I got my colors from <a href="http://colorbrewer2.org/?type=diverging&scheme=RdYlBu&n=9" target="_blank">ColorBrewer</a> and used a number of tutorials and examples, especially <a href="https://bl.ocks.org/tjdecke/5558084">this one</a> for guidance.
  </div>
</div>
 
            
          
!
            
              body {
  font: 14px sans-serif;
  shape-rendering: crispEdges;
  padding: 20px;
}

h1 {
  font-size: 30px;
  font-weight: 700;
  margin-bottom: 10px
}

h2 { 
  font-size: 24px;
  margin-bottom: 10px;
}

h3 {
  font-size: 12px;
  margin-bottom: 5px;
}

#container {
  width: 1220px;
  box-shadow: 10px 10px 40px -2px rgba(0,0,0,0.62);
  padding: 20px 0;
  margin: 20px auto;
}

#title {
  width: 100%;
  text-align: center;
}

rect {
  stroke-width: 0px;
}

.axis {
  font-size: 14px;
}

.tooltip {
  position: absolute;
  text-align: center;
  width: 150px;
  height: auto;
  padding: 10px 5px;
  font: 16px sans-serif;
  line-height: 1.4;
  font-weight: 100;
  background: #000;
  border: 0px;
  border-radius: 8px;
  display: none;
  opacity: 0.9;
  color: white;
  
}

.date {
  font-weight: 700;
}


#notes {
  width: 100%;
  text-align: center;
  margin: 20px auto 0 auto;
}
            
          
!
            
              const dataUrl = 'https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json';

d3.json(dataUrl, function(error, response) {
  if (error) throw error;

  /* Settings */
  const colors =  ['#d73027','#f46d43','#fdae61','#fee090','#ffffbf','#e0f3f8','#abd9e9','#74add1','#4575b4'],
        margin = { 
          top: 0,
          right: 10,
          bottom: 120,
          left: 100
        },
        width = 1200 - margin.left - margin.right,
        height = 700 - margin.top - margin.bottom;

  /* Tooltip */
  const tooltip = d3.select('body').append('div').attr('class', 'tooltip'),
      showTooltip = function(d) {
        tooltip.html('<div class="date">' + formatMonth(d.month) + ', '  + d.year + '</div><div>' + formatNum(baseTemp + d.variance) + '&deg;C</div><div>&Delta; ' + formatNum(d.variance) + '&deg;C</div>')
        .style('left', (d3.event.pageX + 10) + 'px')
        .style('top', (d3.event.pageY + 10) + 'px')
        .style('display', 'block');
      },
      hideTooltip = function(d) {
        tooltip.style('display', 'none');
      };

  /* Data prep */
  const baseTemp = response.baseTemperature,
        data = response.monthlyVariance;
  
  let years = data.map(month => month.year);
  years = years.filter((v, i) => years.indexOf(v) == i);
  
  const minYear = data[0].year,
        maxYear = data[data.length - 1].year,
        variance = data.map(month => month.variance),
        minVariance = d3.min(variance),
        maxVariance = d3.max(variance);
  
  const monthHeight = height / 12,
        monthWidth = width / (variance.length / 12),
        legendWidth = width / 2;
  
  /* Axes & Scales */
  const colorScale = d3.scaleQuantile()
          .domain([minVariance, maxVariance])
          .range(colors.reverse()),
        x = d3.scaleTime()
          .domain([minYear, maxYear])
          .range([0, width]),
        y = d3.scaleTime()
          .domain([12, 1])
          .range([height, height / 12]);
  
  const formatTime = d3.timeFormat('%B'),
        formatMonth = function(month) {
          return formatTime(new Date(2016, month -1))
        },
        formatNum = function(num) { 
          return num.toFixed(3);
        },
        valueLegend = function(color) { 
          return colorScale.invertExtent(color)[0]; 
        };
  
  const xAxis = d3.axisBottom()
    .scale(x)
    .ticks(20)
    .tickFormat(d3.format(''));

  const yAxis = d3.axisLeft()
    .scale(y)
    .tickFormat(formatMonth);
  
  /* Draw heat map */
  const svg = d3.select('#chart').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 + ')');
 
  svg.selectAll('rect')
    .data(data)
    .enter().append('rect')
      .attr('x', function(d) { return x(d.year); })
      .attr('y', function(d) { return y(d.month)})
      .attr('width', monthWidth)
      .attr('height', monthHeight)
      .attr('fill', function(d) {
        return colorScale(d.variance);
       })
       .on('mouseover', showTooltip)
       .on('mouseout', hideTooltip);
  
  svg.append('g')
    .attr('class', 'axis')
    .attr('transform', 'translate(0,' + (height + height / 12) + ')')
    .call(xAxis);
  
  svg.append('g')
    .attr('class', 'axis')
    .attr('transform', 'translate(0,' + (height / 24) + ')')
    .call(yAxis);
  
  const legend = svg.append('g')
    .attr('class', 'legend');
  
  legend.selectAll('rect')
    .data(colors)
    .enter()
    .append('rect')
      .attr('y', height + margin.bottom - 40)
      .attr('x', function(d, i) { return i * (legendWidth / colors.length); })
      .attr('width', legendWidth / colors.length)
      .attr('height', 20)
      .style('fill', function(d) { return d });
   
  legend.selectAll('text')
    .data(colors)
    .enter()
    .append('text')
      .text(function(d) {
        return ((baseTemp + valueLegend(d)).toFixed(1) );
      })
      .attr('text-anchor', 'middle')
      .attr('y', height + margin.bottom - 5)
      .attr('x', function(d, i) { 
        return 15 + i * (legendWidth / colors.length);
      });
}); 
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console