Add Heat Map Certification project

This commit is contained in:
CherryKitten 2022-10-31 15:31:56 +01:00
parent 3f57eb70bc
commit e18a30816b
Signed by: sammy
GPG key ID: 0B696A86A853E955
5 changed files with 169 additions and 0 deletions

View file

@ -0,0 +1,24 @@
html {
text-align: center;
margin: 0;
height: 100vh;
width: 100vw;
background-color: navajowhite;
}
#tooltip {
display: none;
height: 100px;
width: 100px;
opacity: 0.8;
border-radius: 5px;
padding: 10px;
position: fixed;
z-index: 10;
}
#chart {
text-align: center;
}
/*# sourceMappingURL=index.css.map */

View file

@ -0,0 +1 @@
{"version":3,"sourceRoot":"","sources":["index.scss"],"names":[],"mappings":"AAAA;EACE;EACA;EACA;EACA;EACA;;;AAGF;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;;AAGF;EACE","file":"index.css"}

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FreeCodeCamp Heat Map</title>
<link rel="stylesheet" href="index.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/7.6.1/d3.min.js"></script>
</head>
<body>
<h1 id="title">Monthly Global Land-Surface Temperature</h1>
<p id="description">1753 - 2015: base temperature 8.66℃</p>
<div id="chart"></div>
<div id="legend"></div>
<div id="tooltip"></div>
<script src="index.js"></script>
</body>
</html>

View file

@ -0,0 +1,104 @@
const w = 900
const h = 600
const cellColor1 = '#ee0000';
const cellColor2 = '#e7900d';
const cellColor3 = '#6f9898';
const cellcolor4 = '#02cefd';
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
d3.select('#chart')
.append('svg')
.attr('height', h)
.attr('width', w)
d3.json('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/global-temperature.json')
.then(data => {
const xScale = d3.scaleLinear()
.domain([1753, 2015])
.range([40, w])
const yScale = d3.scaleBand()
.domain(months)
.range([0, h])
.padding(0);
const xAxis = d3.axisBottom().scale(xScale)
.tickFormat(d3.format('d'))
const yAxis = d3.axisLeft().scale(yScale)
d3.select('#chart svg')
.append('g')
.call(xAxis)
.attr('id', 'x-axis')
.attr('transform', 'translate(40, 575)')
d3.select('#chart svg')
.append('g')
.call(yAxis)
.attr('id', 'y-axis')
.attr('transform', 'translate(80, -17.5)')
d3.select('#chart svg')
.selectAll('rect')
.data(data["monthlyVariance"])
.enter()
.append('rect')
.attr('class', 'cell')
.attr('data-year', (d) => d["year"])
.attr('data-month', (d) => d["month"] - 1)
.attr('data-temp', (d) => d["variance"])
.attr('fill', (d) => {
const val = d["variance"];
return val >= 0.5 ? cellColor1
: val <= 0.5 && val > 0 ? cellColor2
: val < 0 && val > -0.5 ? cellColor3
: cellcolor4;
})
.attr('x', (d) => xScale(d["year"]) + 40)
.attr('y', (d) => yScale(months[d["month"] - 1]))
.attr('height', 50)
.attr('width', 2)
.on('mouseover', (e) => {
console.log(e)
d3.select('#tooltip')
.style('display', 'initial')
.style('left', e.pageX + 'px')
.style('top', e.pageY + 'px')
.attr('data-year', e.target.attributes['data-year'].nodeValue)
.attr('data-month', e.target.attributes['data-month'].nodeValue)
.attr('data-temp', e.target.attributes['data-temp'].nodeValue)
.style('background-color', e.target.attributes['fill'].nodeValue)
.html(
"<p>" + months[e.target.attributes['data-month'].nodeValue] + " " + e.target.attributes['data-year'].nodeValue + "</p>" +
"Temperature Variance: " + e.target.attributes['data-temp'].nodeValue
)
})
.on('mouseout', () => {
d3.select('#tooltip')
.style('display', 'none')
})
}
)
d3.select('#legend')
.append('svg')
.attr('height', 100)
.attr('width', 400)
.selectAll('rect')
.data([cellColor1, cellColor2, cellColor3, cellcolor4])
.enter()
.append('rect')
.attr('fill', d => d)
.attr('x', (d, i) => i * 100)
.attr('y', 5)
.attr('height', 100)
.attr('width', 100)

View file

@ -0,0 +1,22 @@
html {
text-align: center;
margin: 0;
height: 100vh;
width: 100vw;
background-color: navajowhite;
}
#tooltip{
display: none;
height: 100px;
width: 100px;
opacity: 0.8;
border-radius: 5px;
padding: 10px;
position: fixed;
z-index: 10;
}
#chart {
text-align: center;
}