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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Algreya&display=swap')

body
  font-family: "Algreya", serif
  font-weight: 400
  color: black
.d3-tip
  background-color: rgba(0,0,0,0.8)
  border-radius: 6px
  color: white
  text-align: center
  padding: 0.5em

.date .temperature
.variance

section
  width: auto
  overflow: visible
  margin-left: auto
  margin-right: auto
  font-size: 16px
heading
    text-align: center
svg
   stroke-width: 1
   stroke: blacl
.y-axis, .x-axis, .legend
  text
  stroke-width: 0
.map
  stroke-width: 0
rect:hover
  stroke-width: 2
              
            
!

JS

              
                var url =
  'https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json';


// http://colorbrewer2.org/

const colorbrewer = {
  Spectral: {
    3: [
      '#fc8d59',
      '#ffffbf',
      '#99d594'
    ],
    4: [
      '#d7191c',
      '#fdae61',
      '#abdda4',
      '#2b83ba'
    ],
    5: [
      '#d7191c',
      '#fdae61',
      '#ffffbf',
      '#abdda4',
      '#2b83ba'
    ],
    6: [
      '#d53e4f',
      '#fc8d59',
      '#fee08b',
      '#e6f598',
      '#99d594',
      '#3288bd'
    ],
    7: [
      '#d53e4f',
      '#fc8d59',
      '#fee08b',
      '#ffffbf',
      '#e6f598',
      '#99d594',
      '#3288bd'
    ],
    8: [
      '#d53e4f',
      '#f46d43',
      '#fdae61',
      '#fee08b',
      '#e6f598',
      '#abdda4',
      '#66c2a5',
      '#3288bd'
    ],
    9: [
      '#d53e4f',
      '#f46d43',
      '#fdae61',
      '#fee08b',
      '#ffffbf',
      '#e6f598',
      '#abdda4',
      '#66c2a5',
      '#3288bd'
    ],
    10: [
      '#9e0142',
      '#d53e4f',
      '#f46d43',
      '#fdae61',
      '#fee08b',
      '#e6f598',
      '#abdda4',
      '#66c2a5',
      '#3288bd',
      '#5e4fa2'
    ],
    11: 
  [
    '#9e0142',
    '#d53e4f',
    '#f46d43',
    '#fdae61',
    '#fee08b',
    '#ffffbf',
    '#e6f598',
    '#abdda4',
    '#66c2a5',
    '#3288bd',
    '#5e4fa2'
  ]
  },
  RdBu: {
    3: [
      '#ef8a62',
      '#f7f7f7',
      '#67a9cf'
    ],
    4: [
      '#ca0020', 
      '#f4a582', 
      '#92c5de', 
      '#0571b0'
    ],
    5: [
      '#ca0020', 
      '#f4a582', 
      '#f7f7f7', 
      '#92c5de', 
      '#0571b0'
    ],
    6: [
      '#b2182b',
      '#ef8a62', 
      '#fddbc7', 
      '#d1e5f0', 
      '#67a9cf', 
      '#2166ac'
    ],
    7: [
      '#b2182b',
      '#ef8a62',
      '#fddbc7',
      '#f7f7f7',
      '#d1e5f0',
      '#67a9cf',
      '#2166ac'
    ],
    8: [
      '#b2182b',
      '#d6604d',
      '#f4a582',
      '#fddbc7',
      '#d1e5f0',
      '#92c5de',
      '#4393c3',
      '#2166ac'
    ],
    9: [
      '#b2182b',
      '#d6604d',
      '#f4a582',
      '#fddbc7',
      '#f7f7f7',
      '#d1e5f0',
      '#92c5de',
      '#4393c3',
      '#2166ac'
    ],
    10: [
      '#67001f',
      '#b2182b',
      '#d6604d',
      '#f4a582',
      '#fddbc7',
      '#d1e5f0',
      '#92c5de',
      '#4393c3',
      '#2166ac',
      '#053061'
    ],
    11: [
      '#67001f',
      '#b2182b',
      '#d6604d',
      '#f4a582',
      '#fddbc7',
      '#f7f7f7',
      '#d1e5f0',
      '#92c5de',
      '#4393c3',
      '#2166ac',
      '#053061'
    ]
  }
};

d3.json(url)
  .then((data) => callback(data))
  .catch((err) => console.log(err));

function callback(data) {
  console.log('data: ', data);

  data.monthlyVariance.forEach(function (val) {
    val.month -= 1;
  });

  let section = d3.select('body').append('section');

  // heading
  let heading = section.append('heading');
  heading
    .append('h1')
    .attr('id', 'title')
    .text('Monthly Global Land-Surface Temperature');
  heading
    .append('h3')
    .attr('id', 'description')
    .html(
      data.monthlyVariance[0].year +
        ' - ' +
        data.monthlyVariance[data.monthlyVariance.length - 1].year +
        ': base temperature ' +
        data.baseTemperature +
        '&#8451;'
    );

  let fontSize = 16;
  let width = 5 * Math.ceil(data.monthlyVariance.length / 12);
  let height = 33 * 12;
  let padding = {
    left: 9 * fontSize,
    right: 9 * fontSize,
    top: 1 * fontSize,
    bottom: 8 * fontSize
  };
  let tip = d3
    .tip()
    .attr('class', 'd3-tip')
    .attr('id', 'tooltip')
    .html(function (d) {
      return d;
    })
    .direction('n')
    .offset([-10, 0]);

  let svg = section
    .append('svg')
    .attr('width', width + padding.left + padding.right)
    .attr('height', height + padding.top + padding.bottom)
    .call(tip);

  // yaxis
  const yScale = d3
    .scaleBand()
    // months
    .domain([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0 ])
    .rangeRound([0, height])
    .padding(0);

  const yAxis = d3
    .axisLeft()
    .scale(yScale)
    .tickValues(yScale.domain())
    .tickFormat(function (month) {
      let date = new Date(0);
      date.setUTCMonth(month);
      let format = d3.timeFormat('%B');
      return format(date);
    })
    .tickSize(10, 1);

  svg
    .append('g')
    .classed('y-axis', true)
    .attr('id', 'y-axis')
    .attr('transform', 'translate(' + padding.left + ',' + padding.top + ')')
    .call(yAxis)
    .append('text')
    .text('Months')
    .style('text-anchor', 'middle')
    .attr(
      'transform',
      'translate(' + -7 * fontSize + ',' + height / 2 + ')' + 'rotate(-90)'
    )
    .attr('fill', 'black');

  // xaxis
  // ordinal scale
  const xScale = d3
    .scaleBand()
    .domain(
      data.monthlyVariance.map(function (val) {
        return val.year;
      })
    )
    .range([0, width])
    .padding(0);

  const xAxis = d3
    .axisBottom()
    .scale(xScale)
    .tickValues(
      xScale.domain().filter(function (year) {
        // set ticks to years % by 10
        return year % 10 === 0;
      })
    )
    .tickFormat(function (year) {
      let date = new Date(0);
      date.setUTCFullYear(year);
      let format = d3.timeFormat('%Y');
      return format(date);
    })
    .tickSize(10, 1);

  svg
    .append('g')
    .classed('x-axis', true)
    .attr('id', 'x-axis')
    .attr(
      'transform',
      'translate(' + padding.left + ',' + (height + padding.top) + ')'
    )
    .call(xAxis)
    .append('text')
    .text('Years')
    .style('text-anchor', 'middle')
    .attr('transform', 'translate(' + width / 2 + ',' + 3 * fontSize + ')')
    .attr('fill', 'black');

  // legend

  let legendColors = colorbrewer.Spectral[11].reverse();
  let legendWidth = 400;
  let legendHeight = 300 / legendColors.length;

  let variance = data.monthlyVariance.map(function (val) {
    return val.variance;
  });
  let minTemp = data.baseTemperature + Math.min.apply(null, variance);
  let maxTemp = data.baseTemperature + Math.max.apply(null, variance);

  let legendThreshold = d3
    .scaleThreshold()
    .domain(
      (function (min, max, count) {
        let array = [];
        let step = (max - min) / count;
        let base = min;
        for (var i = 1; i < count; i++) {
          array.push(base + i * step);
        }
        return array;
      })(minTemp, maxTemp, legendColors.length)
    )
    .range(legendColors);

  let legendX = d3
    .scaleLinear()
    .domain([minTemp, maxTemp])
    .range([0, legendWidth]);

  let legendXAxis = d3
    .axisBottom()
    .scale(legendX)
    .tickSize(10, 0)
    .tickValues(legendThreshold.domain())
    .tickFormat(d3.format('.1f'));

  let legend = svg
    .append('g')
    .classed('legend', true)
    .attr('id', 'legend')
    .attr(
      'transform',
      'translate(' +
        padding.left +
        ',' +
        (padding.top + height + padding.bottom - 2 * legendHeight) +
        ')'
    );

  legend
    .append('g')
    .selectAll('rect')
    .data(
      legendThreshold.range().map(function (color) {
        let d = legendThreshold.invertExtent(color);
        if (d[0] === null) {
          d[0] = legendX.domain()[0];
        }
        if (d[1] === null) {
          d[1] = legendX.domain()[1];
        }
        return d;
      })
    )
    .enter()
    .append('rect')
    .style('fill', function (d) {
      return legendThreshold(d[0]);
    })
    .attr('x', (d) => legendX(d[0]))
    .attr('y', 0)
    .attr('width', (d) =>
      d[0] && d[1] ? legendX(d[1]) - legendX(d[0]) : legendX(null)
    )
    .attr('height', legendHeight);

  legend
    .append('g')
    .attr('transform', 'translate(' + 0 + ',' + legendHeight + ')')
    .call(legendXAxis);

  // map
  svg
    .append('g')
    .classed('map', true)
    .attr('transform', 'translate(' + padding.left + ',' + padding.top + ')')
    .selectAll('rect')
    .data(data.monthlyVariance)
    .enter()
    .append('rect')
    .attr('class', 'cell')
    .attr('data-month', function (d) {
      return d.month;
    })
    .attr('data-year', function (d) {
      return d.year;
    })
    .attr('data-temp', function (d) {
      return data.baseTemperature + d.variance;
    })
    .attr('x', (d) => xScale(d.year))
    .attr('y', (d) => yScale(d.month))
    .attr('width', (d) => xScale.bandwidth(d.year))
    .attr('height', (d) => yScale.bandwidth(d.month))
    .attr('fill', function (d) {
      return legendThreshold(data.baseTemperature + d.variance);
    })
    .on('mouseover', function (event, d) {
      var date = new Date(d.year, d.month);
      let str =
        "<span class='date'>" +
        d3.timeFormat('%Y - %B')(date) +
        '</span>' +
        '<br />' +
        "<span class='temperature'>" +
        d3.format('.1f')(data.baseTemperature + d.variance) +
        '&#8451;' +
        '</span>' +
        '<br />' +
        "<span class='variance'>" +
        d3.format('+.1f')(d.variance) +
        '&#8451;' +
        '</span>';
      tip.attr('data-year', d.year);
      tip.show(str, this);
    })
    .on('mouseout', tip.hide);
}



              
            
!
999px

Console