Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <noscript id="no-js">
  <h1>JavaScript is disabled</h1>
  <p>Please enable JavaScript and refresh</p>
</noscript>
              
            
!

CSS

              
                html {
  height: 100%;
}

body {
  min-height: 100%;
  display: flex;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  background-color: LightYellow;
  box-sizing: border-box;
  padding: 8px;
  margin: 0;
}

#container {
  background-color: Ivory;
  text-align: center;
  border: thin solid SlateGray;
  border-radius: 4px;
  box-shadow: 2px 2px 8px;
  margin: auto;
}

#title {
  margin: .5em 0 0 0;
}

#description > h2:first-child {
  margin: .5em 0 0 0;
}
#description > p {
  margin: .5em 0 1em 0;
}

#tooltip {
  position: absolute;
  top: 0;
  let: 0;
  transition: opacity .2s;
  pointer-events: none;
  background-color: rgba(0, 0, 0, .8);
  color: white;
  box-sizing: border-box;
  line-height: 1.4;
  padding: .5em;
  border-radius: .2em;
  backdrop-filter: blur(2px);
}
#tooltip:after {
  content: "";
  position: absolute;
  top: 100%;
  --arrow_size: 5px;
  left: calc(50% - var(--arrow_size));
  border-width: var(--arrow_size);
  border-style: solid;
  border-color: rgba(0, 0, 0, .8) transparent transparent transparent;
}

.cell {
  cursor: pointer;
}
.cell:hover {
  stroke: black;
}

#legend rect {
  stroke: black;
}


#no-js {
  text-align: center;
  margin: auto;
}
              
            
!

JS

              
                const projectName = "heat-map",  // freeCodecamp Testing
      //
      container = d3.select("body")
                    .append("div")
                    .attr("id", "container");

container
  .append("h1")
  .text("Monthly Global Land-Surface Temperature")
  .attr("id", "title");


d3.json("https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json").then(({ monthlyVariance, baseTemperature }) => {
  const description = container
                        .append("div")
                        .attr("id", "description"),
        yearsExtent = d3.extent(monthlyVariance, ({ year }) => year);
  
  description
    .append("h2")
    .text(yearsExtent.join(" ― "));
  
  description
    .append("p")
    .html(`Base Temperature: <strong>${baseTemperature}℃</strong>`);
  
  const svgParent = container
                        .append("div")
                        .style("position", "relative"),  // IMPORTANT
        width = 1260,
        height = 495,
        svg = svgParent
                .append("svg")
                .attr("width", width)
                .attr("height", height),
        //
        tooltip = svgParent
                    .append("div")
                    .attr("id", "tooltip")
                    .style("opacity", 0),  // IMPORTANT
        months = [
          "January",
          "February", 
          "March", 
          "April", 
          "May", 
          "June",
          "July", 
          "August", 
          "September", 
          "October", 
          "November", 
          "December"
        ],
        //
        padding = 70,
        chartTop = height * (18 / 25),
        //
        xScaleRange = [padding, width - padding],
        xScale = d3.scaleTime()
                   .domain(yearsExtent.map(year => new Date(year, 0)))
                   .range(xScaleRange);

  svg
    .append("g")
    .attr("id", "x-axis")
    .attr("transform", `translate(0 ${chartTop})`)
    .call(d3.axisBottom(xScale));
  
  const yScale = d3.scaleBand()
                   .domain(months)
                   .range([0, chartTop]);
  
  svg
    .append("g")
    .attr("id", "y-axis")
    .attr("transform", `translate(${padding} 0)`)
    .call(d3.axisLeft(yScale));
  
  const colors = d3.schemeRdYlBu[11].reverse(),
        colorDomain = d3.extent(monthlyVariance, ({ variance }) => variance),
        getColor = d3.scaleQuantize()
                     .domain(colorDomain)
                     .range(colors);
  
  const dataWidth = (xScaleRange[1] - xScaleRange[0]) / (yearsExtent[1] - yearsExtent[0] + 1),
        dataHeight = yScale.step();
  
  svg
    .selectAll("rect")
    .data(monthlyVariance)
    .join("rect")
    .attr("class", "cell")
    .attr("data-month", ({ month }) => month - 1)
    .attr("data-year", ({ year }) => year)
    .attr("data-temp", ({ variance }) => +(baseTemperature + variance).toFixed(4))
    .attr("width", dataWidth)
    .attr("height", dataHeight)
    .attr("x", ({ year }) => xScale(new Date(year, 0)))
    .attr("y", ({ month }, i, nodes) => yScale(months[nodes[i].getAttribute("data-month")]))
    .attr("fill", ({ variance }) => getColor(variance))
    .on("mouseover", ({ currentTarget }, { year, month, variance }) => {
      tooltip
        .html(`${months[month - 1]} ${year}<br>${currentTarget.getAttribute("data-temp")}℃<br>${variance}℃`)
        .attr("data-year", year)
        .style("opacity", 1)
        .style("left", (+currentTarget.getAttribute("x") - (tooltip.node().offsetWidth - dataWidth) / 2) + "px")
        .style("top", (+currentTarget.getAttribute("y") - tooltip.node().offsetHeight - 6) + "px");
    })
    .on("mouseout", () => tooltip.style("opacity", 0))
  
  const legend = svg
                   .append("g")
                   .attr("id", "legend"),
        legendScaleRange = [padding, 400],
        legendScale = d3.scaleLinear()
                        .domain(colorDomain.map(n => n + baseTemperature))
                        .range(legendScaleRange)
                        .nice(),
        legendTop = height - padding,
        legendDataY = legendTop - dataHeight;
  
  legend
    .append("g")
    .attr("transform", `translate(0 ${legendTop})`)
    .call(d3.axisBottom(legendScale));
  
  legend
    .selectAll("rect")
    .data(colors)
    .join("rect")
    .attr("x", (d, i) => legendScale(getColor.invertExtent(d)[0] + baseTemperature))
    .attr("y", legendDataY)
    .attr("width", d => {
      const [x0, x] = getColor.invertExtent(d);
      return legendScale(x + baseTemperature) - legendScale(x0 + baseTemperature);
    })
    .attr("height", dataHeight)
    .attr("fill", d => d);
  
  legend
    .append("text")
    .text("℃")
    .attr("x", (legendScaleRange[0] + legendScaleRange[1]) / 2)
    .attr("y", legendTop + 35);
  
}).catch(err => {
  container
    .append("h2")
    .text(err);
});
              
            
!
999px

Console