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

              
                <h1 id="title">Monthly Global Land-Surface Temperature</h1>
<h3 id="description">1753 - 2015: base temperature 8.66℃</h3>

<div id="container" class="container">
  <div id="tooltip" class="tooltip"></div>
</div>

<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>


              
            
!

CSS

              
                body {
  background-image: radial-gradient( circle 879px at 10.4% 22.3%,  rgba(255,235,238,1) 0%, rgba(186,190,245,1) 93.6% );
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  flex-direction: column;
  font-family: sans-serif;
}

h1 {
  margin-top: -40px;
  margin-bottom: 0;
}

svg {
  border: 2px solid black;
  box-shadow: 4px 4px 6px rgba(0, 0, 0, 0.4);
  background-color: #fff;
}

rect:hover {
  stroke: black;
}

.container {
  position: relative;
}

.tooltip {
  background-color: #000;
  border-radius: 10px;
  box-shadow: 4px 4px 6px rgba(0, 0, 0, 0.4);
  color: #fff;
  display: none;
  position: absolute;
  z-index: 100;
  text-align: center;
  opacity: 0.8;
}

.tooltip.show {
  display: block;
}

.tooltip p {
  margin: 5px;
}
              
            
!

JS

              
                const tooltip = document.getElementById('tooltip');

const colors = ["#67001f","#b2182b","#d6604d","#f4a582","#fddbc7","#f7f7f7","#d1e5f0","#92c5de","#4393c3","#2166ac","#053061"];

const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']

fetch('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json')
  .then(res => res.json())
  .then(res => {
  const { baseTemperature, monthlyVariance } = res;
  
  createStuff(monthlyVariance.map(d => ({
    ...d, 
    temp: baseTemperature - d.variance
  })));
})

function createStuff(data) {
  const width = 800;
  const height = 400;
  const padding = 60;
  
  const cellHeight = (height - (2*padding)) / 12;
  const cellWidth = width / Math.floor(data.length / 12);
  
  // console.log(data);
  
  const yScale = d3.scaleLinear()
      .domain([0, 11])
      .range([padding, height - padding]);
  
  const xScale = d3.scaleTime()
      .domain([
        d3.min(data, d => d.year), 
        d3.max(data, d => d.year)
       ])
      .range([padding, width - padding]);

  const tempScale = d3.scaleLinear()
    .domain([
      d3.min(data, d => d.temp), 
      d3.max(data, d => d.temp)
    ])
    .range([0, 10]);
  
  const svg = d3.select('#container').append('svg')
          .attr('width', width)
          .attr('height', height);

  svg.selectAll('rect')
    .data(data)
    .enter()
    .append('rect')
    .attr('class', 'cell')
    .attr('data-month', d => d.month - 1)
    .attr('data-year', d => d.year)
    .attr('data-temp', d => d.temp)
    .attr('fill', d => colors[Math.floor(tempScale(d.temp))])
    .attr('x', d => xScale(d.year))
    .attr('y', d => yScale(d.month - 1) - cellHeight)
    .attr('width', cellWidth)
    .attr('height', cellHeight)
    .on('mouseover', (d, i) => {
      tooltip.classList.add('show');
      tooltip.style.left = xScale(d.year) - 60 + 'px';
      tooltip.style.top = yScale(d.month - 1) - 60 + 'px';
      tooltip.setAttribute('data-year', d.year);

      tooltip.innerHTML = `
        <p>${d.year} - ${months[d.month - 1]}</p>
        <p>${d.temp}℃</p>
      `;
  }).on('mouseout', () => {
     tooltip.classList.remove('show');
  });
  
  // create axis
  const xAxis = d3.axisBottom(xScale)
    .tickFormat(d3.format('d'));
  const yAxis = d3.axisLeft(yScale)
    .tickFormat((month) => {
    const date = new Date(0);
    date.setUTCMonth(month);
    return d3.timeFormat('%B')(date);
  });
  
  svg.append('g')
    .attr('id', 'x-axis')
    .attr('transform', `translate(0, ${height - padding})`)
    .call(xAxis);
  
  svg.append('g')
    .attr('id', 'y-axis')
    .attr('transform', `translate(${padding}, ${-cellHeight})`)
    .call(yAxis);
  
  // create the legend
  const legendWidth = 200;
  const legendHeight = 50;
  
  const legendRectWidth = legendWidth / colors.length;
  const legend = d3.select('body')
    .append('svg')
    .attr('id', 'legend')
    .attr('width', legendWidth)
    .attr('height', legendHeight)
    .selectAll('rect')
    .data(colors)
    .enter()
    .append('rect')
    .attr('x', (_, i) => i * legendRectWidth)
    .attr('y', 0)
    .attr('width', legendRectWidth)
    .attr('height', legendHeight)
    .attr('fill', c => c)
    
}
              
            
!
999px

Console