Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

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.

HTML

            
              <!--you can reduce the window size to see how it works when the table is too wide -->  
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
  <link href='https://fonts.googleapis.com/css?family=Open Sans' rel='stylesheet'>

<div id="sticky-wrapper">
    <div id="heat-map"></div>
 </div>
Data available from <a href="https://www.metoffice.gov.uk/climate/uk/summaries/datasets">Met Office UK</a>
            
          
!

CSS

            
              body{
  font-family: Consolas, courier;
}

.sticky-wrapper{
    overflow: auto;
    position:relative;
    margin: 0 auto 1.5em;
    width: 75%;
}

table{
  width:10px;
  border-collapse: collapse;
  margin-bottom: 1em;
}

caption {
  caption-side: top;
  font-weight: bold;
  font-size: 1.1em;
  font-family: "open sans", roboto;
}

th {
    background-color: white;
    font-weight: normal;
}

thead tr:nth-child(1) th {
  position: sticky;
  top: 0;
  z-index: 10;
}

tbody th {
  position: sticky;
  left: 0;
  z-index: 10;
}

td, th{
  padding: 0.75em 1.5em;
  text-align: left;
}

td{
  min-width: 20px;
  width: 20px;
  max-width: 30px;
}

.legend{
  margin: 0 auto;
  width:25%;
  font-family: roboto, arial;
  font-size: 0.75em;
}

.color-square{
  float:left;
  width: 9%;
  margin-right: 1%;
  height:1.5em;
}

.align-left{
  float: left;
}

.align-right{
  float: right;
}

.fa-snowflake{
  color:#3C2EA8;
}

.cold{
  color:#7A2EA1;
}

.fa-sun{
  color:#F06E1D;
}

.hot{
  color:#E61717;
}

            
          
!

JS

            
              //data source: "https://gist.githubusercontent.com/robole/9ee26cdc0da17aebaacd295374763930/raw/aff6657d375a2669d94c18e8d91e32d5117cb71e/tempatures.json"

var min, max, colorScale, temps, tempsArr;
var colors = ["#7A2EA1", "#3C2EA8", "#5D2EE8", "#2F9EEE", "#2FC8EE", "#2DD91A", "#CBF22C", "#F2CE2C", "#F06E1D", "#E61717"];
var months = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var table, thead, tbody, rows, headCells, cells;

var data = d3.json("https://gist.githubusercontent.com/robole/9ee26cdc0da17aebaacd295374763930/raw/aff6657d375a2669d94c18e8d91e32d5117cb71e/tempatures.json", function(error, data){
  temps = data.meanTemp;
  tempsArr = createTempArr(temps);

  initScale();
  initTable();
  addTopHeader();
  addRows();
  setColorTransition();

  addLegend();
});

function initTable(){
  table = d3.select('#heat-map').append('table');

  table.append("caption")
  .html("<i class='fas fa-snowflake cold'></i> UK Average Tempatures 1910 - 2012 <i class='fas fa-sun hot'></i>");
  thead = table.append('thead');
  tbody = table.append('tbody');
}

function initScale(){
  min = d3.min(d3.values(temps), function(d) { return d3.min(d); });
  max = d3.max(d3.values(temps), function(d) { return d3.max(d); });
  colorScale = d3.scaleQuantile()
       .domain([min, max])
       .range(colors);
}

function addTopHeader(){
  //make top heading
  thead.append('tr')
       .selectAll('th')
       .data(months)
    .enter()
    .append("th")
    .text(function(d){return d;});
}

function addRows(){
  // create a row for each object in the data
  rows = tbody.selectAll('tr')
  .data(tempsArr).enter()
  .append('tr');

  // create vertical heading (first col of each row)
  headCells = rows.append('th')
  .text(function (d) { return d.year; });

  //create a data cell for each monthly tempature
   cells = rows.selectAll('td')
     .data(function (row, i) {
       return row.temps;
     })
     .enter()
     .append('td')
     .text(function (d) { return d; })
     .style("background-color", colors[0]);
}

function createTempArr(){
  var tempsArr = [];
  for(var k in temps){
    if (temps.hasOwnProperty(k)) {
           tempsArr.push({year: k, temps: temps[k]});
    }
  }
  return tempsArr;
}

function setColorTransition(){
  cells.transition()
    .duration(1000)
    .style("background-color", function(d) { return colorScale(d); });
}

function addLegend(){
  var rangeValues = [min];
  rangeValues = rangeValues.concat(colorScale.quantiles());

  var legend = d3.select('caption').append('div');
  legend.attr("class", "legend");

  var colorSq = legend.append("div");

  colorSq.selectAll("div")
            .data(rangeValues).enter()
            .append("div")
            .attr("class", "color-square")
            .style("background-color", function(d, i) {return colors[i];});
            //.text(function(d) { return "≥ " + Math.round(d); }); //add range

  var labels = legend.append("div");
  labels.append("div")
  .attr("class", "align-left")
  .text("colder");

  labels.append("div")
  .attr("class", "align-right")
  .text("hotter");

}

            
          
!
999px

Console