I’m still failing these test:
8. The “data-month”, “data-year” of each cell should be within the range of the data.
9. My heat map should have cells that align with the corresponding month on the y-axis.
I really cannot understand why…
Everything seems to line up.
import * as d3 from "https://cdn.skypack.dev/d3@7.8.2";
function calculateHex(v) {
if (v >= 2.8 && v <= 5.0) {
return "#008080";
} else if (v > 5.0 && v <= 7.2) {
return "#FFFF00";
} else if (v > 7.2 && v <= 9.5) {
return "#FFA500";
} else if (v > 9.5 && v <= 12.8) {
return "#FF0000";
} else {
return "lightblue";
}
}
const w = 1603;
const h = 540;
const padding = 150;
const title = d3
.select("main")
.append("h1")
.attr("id", "title")
.text("Monthly Global Land-Surface Temperature");
const subtitle = d3
.select("main")
.append("h4")
.attr("id", "description")
.text("1753 - 2015: base temperature 8.66℃");
const svg = d3.select("main").append("svg").attr("width", w).attr("height", h);
const url =
"https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json";
document.addEventListener("DOMContentLoaded", function () {
fetch(url)
.then((response) => response.json())
.then((data) => {
const dataset = data.monthlyVariance.map(function (item) {
return {
year: item.year,
month: item.month,
variance: item.variance,
actualTemp: 8.66 + item.variance,
colorHex: calculateHex(8.66 + item.variance)
};
});
console.log(dataset);
const minYears = d3.min(dataset, (d) => d.year);
const maxYears = d3.max(dataset, (d) => d.year);
const minMonth = d3.min(dataset, (d) => d.month);
const maxMonth = d3.max(dataset, (d) => d.month);
console.log(minYears, maxYears, minMonth, maxMonth);
const yScale = d3
.scaleBand()
.domain([12,11,10,9,8,7,6,5,4,3,2,1])
.range([h - padding, padding]);
const xScale = d3
.scaleLinear()
.domain([1752, maxYears])
.range([padding, w - padding]);
const yAxis = d3.axisLeft(yScale);
const xAxis = d3.axisBottom(xScale);
svg
.append("g")
.attr("id", "x-axis")
.attr("transform", `translate(0,${h - padding})`)
.call(xAxis);
svg
.append("g")
.attr("id", "y-axis")
.attr("transform", `translate(${padding},0)`)
.call(yAxis);
svg
.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", (d, i) => xScale(d.year))
.attr("y", (d, i) => yScale(d.month))
.attr("width", 5)
.attr("height",20)
.attr("data-month", (d) => d.month)
.attr("data-year", (d) => d.year)
.attr("data-temp", (d) => d.actualTemp)
.attr("class", "cell")
.style("fill", (d) => d.colorHex);
});
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36
Challenge: Data Visualization Projects - Visualize Data with a Heat Map
Link to the challenge:
Link to the codepen