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

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


              
            
!

CSS

              
                * {
  margin: 0;
}

body {
  display: grid;
  width: 100vw;
  height: 100vh;
  background: #c2c2c2;
}

svg {
  background: #fafafa;
  display: block;
  margin: 0 auto;
}

#tooltip {
  position: absolute;
}
              
            
!

JS

              
                // Standard options
const titlefontsize = 22, subtitlefontsize = 14;
const margin = {top: 50, right: 50, bottom: 75, left: 75}
const width = 900 - margin.left - margin.right
const height = 550 - margin.top - margin.bottom;

const title = "Monthly Global Land-Surface Temperature";
const subtitle = "Hover over a month to see the absolute temperature as well as the variance from the average temperature.";

const legendWidth = 400;

const colors = [
  "#0720e3", // darkblue
  "#6e7efa",
  "#b0b7eb",
  "#b0ebbb",
  "#f0f065",
  "#f2f2b8",
  "#edc77b",
  "#eb9646",
  "#a84545",
  "#4f0303",
  "#2e0101" // dark red
];

const delimiters = [0,2.8,4,5.1,6.2,7.3,8.4,9.5,10.6,11.7,12.8];

var basetemp = 0;

function tooltiptext(data) {
  return "<b>"+ data.year + "</b></br>" + (basetemp + data.variance).toFixed(3) + "<br>" + data.variance;
}


import * as d3 from "https://cdn.skypack.dev/d3@6.5.0";

var years = [], months = [];

// Create SVG
  const svg = d3.select("body")
    .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 + ")")

 
  
function start(data) {
  const json = data.monthlyVariance;
  years = json.map(d => d.year);
  months = json.map(d => d.month);
  basetemp = data.baseTemperature;
  
  const firstYear = d3.min(json, d => d.year),
        lastYear = d3.max(json, d => d.year);
  const barWidth = width / (lastYear - firstYear);
  const barHeight = height / 12;
  // Title
  svg.append("text")
    .attr("id", "title")
    .attr("x", 0)
    .attr("y", - margin.top + titlefontsize + 5)
    .attr("text-anchor", "left")
    .style("font-size", titlefontsize + "px")
    .style("fill", "navy")
    .text(title);
  
  // Subtitle
  svg.append("text")
    .attr("x", 0)
    .attr("y", - margin.top + titlefontsize + 10 + subtitlefontsize)
    .attr("text-anchor", "left")
    .attr("id", "description")
    .style("font-size", subtitlefontsize + "px")
    .style("fill", "grey")
    .style("max-width", 400)
    .text(subtitle);
  
  // Create x-axis
  const xScale = d3.scaleTime()
    .domain([new Date(firstYear +"-1-1"), new Date(lastYear + "-1-1")])
    .range([0, width])
  
  svg.append("g")
    .attr("transform", "translate(0," + height + ")")
    .attr("id", "x-axis")
    .call(d3.axisBottom(xScale));
  
  
  // Create y-axis
  var yScalevals = [new Date(2000, 11, 15), new Date(1999, 11, 15)];
  
  
  
  const yScale = d3.scaleTime()
    .range([height, 0])
    .domain(yScalevals);
  svg.append("g")
    .attr("id", "y-axis")
    .call(d3.axisLeft(yScale).tickFormat(d3.timeFormat("%B")));
  
  // Create colorscale
  const colorScaleDomain = [d3.min(json, d => basetemp + d.variance), d3.max(json, d => basetemp + d.variance)];
  
  const colorScale = d3.scaleQuantize()
    .range(colors)
    .domain(colorScaleDomain);
  
  // Tooltip functions
  const tooltip = d3.select("body")
  .append("div")
  .style("opacity", 0)
  .attr("id", "tooltip")
  .attr("data-year", 0)
  .style("background-color", "white")
  .style("border", "solid")
  .style("border-width", "2px")
  .style("border-radius", "5px")
  .style("padding", "5px");
  
  const mouseover = function(event,d) {
    tooltip
      .style("opacity", 1)
  }
  const mousemove = function(event,d) {
    tooltip
      .html(tooltiptext(d))
      .style("left", (event.pageX + 10) + "px")
      .style("top", (event.pageY + -70) + "px")
      .attr("data-year", d.year);
  }
  const mouseleave = function(event,d) {
    tooltip
      .style("opacity", 0)
  }
  
  // Legend
  const legendRectWidth = legendWidth / colors.length;
  
  var data = [];
  for (var i = 0; i < colors.length; i++) {
    data.push([colors[i], delimiters[i]]);
  }
  
  
  const legendContainer = svg.append("g")
    .attr("transform", "translate(0," + (height + 20) + ")")
    .attr("id", "legend")
  
  
  const rect = legendContainer.selectAll(".legendrect")
    .data(data)
    .enter()
  
  const legendrects = rect
  .append("rect")
  .attr("x", (d, i) => legendRectWidth * i)
  .attr("fill", d => d[0])
  .attr("width", legendRectWidth + "px")
  .attr("height", "20px")
  .attr("stroke", "#fafafa")
  .attr("stroke-width", 2)
  
  const legendtexts = rect
  .append("text")
  .attr("x", (d, i) => legendRectWidth * i)
  .attr("y", "36px")
  .text(d => d[1]);
  
  // Data/bars
  var bars = svg.selectAll()
  .data(json, d => {return d.year + ':' + d.month})
  .enter()
  .append("rect")
  .attr("x", d => {return xScale(new Date("1-1-" + d.year))})
  .attr("y", (d) => {return (d.month - 1) * barHeight})
  .attr("width", barWidth)
  .attr("height", barHeight)  
  .attr("class", "cell")
  .attr("data-month", d => d.month - 1)
  .attr("data-year", d => d.year)
  .attr("data-temp", d => d.variance)
  .style("fill", function(d) {return colorScale(basetemp+d.variance)})
  .style("opacity", 0.8)
  .on("mouseover", mouseover)
  .on("mousemove", mousemove)
  .on("mouseleave", mouseleave)  
}; 



// Create request for data and call the graph function
const req = new XMLHttpRequest();
req.open("GET", "https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json", true);
req.send();
req.onload=function() {
  start(JSON.parse(req.responseText));
}
              
            
!
999px

Console