Created
October 28, 2017 05:36
-
-
Save gavlooth/79bc4b0f5ad35721e0ef0b2590e386f0 to your computer and use it in GitHub Desktop.
Visualize Data with a HeatMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="chart"> | |
<div id="tooltip" class="tooltip"> | |
<span id="Date"> | |
</span> | |
<span id="Temp"> | |
</span> | |
<span id="Variance"> | |
</span> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$.getJSON('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/global-temperature.json', | |
function(data) { | |
const margin = { | |
top: 20, | |
right: 20, | |
bottom: 30, | |
left: 80 | |
}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
const mL = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], | |
mS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']; | |
const min = d3.min(data.monthlyVariance, (d) => d.year), | |
max = d3.max(data.monthlyVariance, (d) => d.year); | |
let sqrWidth = width / (max - min); | |
let sqrHeight = height / 12; | |
const minTemp = data.baseTemperature + d3.min(data.monthlyVariance, (d) => d.variance); | |
const maxTemp = data.baseTemperature + d3.max(data.monthlyVariance, (d) => d.variance); | |
const tempRange = maxTemp - minTemp; | |
console.log(tempRange); | |
let Colors = d => d3.interpolateRgb("#fab572", "#450214")((d.variance - minTemp) / tempRange); | |
const minTime = d3.min(data, d => ToSeconds(d.Time)), | |
maxTime = d3.max(data, d => ToSeconds(d.Time)); | |
let x = d3.scaleLinear().range([0, width]).domain([min, max + 1]), | |
y = d3.scaleLinear().range([height, 0]).domain([0, 12]); | |
let chart = d3.select("#chart").append("svg") | |
.attr("class", "chart") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
let valArr = []; | |
for (let i = min + 1; i < max; i++) | |
if (!(i % 10)) | |
valArr.push(i); | |
let xAxis = d3.axisBottom(x).tickValues(valArr).tickFormat(d3.format("d")), | |
yAxis = d3.axisLeft(y).tickFormat((m) => m ? mL[m - 1] : ""); | |
// x-axis | |
chart.append("g") | |
.attr("class", "x-axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
// y-axis | |
chart.append("g") | |
.attr("class", "y-axis") | |
.call(yAxis); | |
chart.selectAll(".tile") | |
.data(data.monthlyVariance) | |
.enter().append("rect") | |
.attr("class", "tile") | |
.attr("x", function(d) { | |
return x(d.year); | |
}) | |
.attr("y", function(d) { | |
return y(d.month); | |
}) | |
.attr("width", sqrWidth) | |
.attr("height", sqrHeight) | |
.style("fill", (d) => Colors(d)) | |
.on("mouseover", function() { | |
d3.select('#tooltip').style("visibility", "visible") | |
}) | |
.on("mousemove", function(d, i) { | |
d3.select('#tooltip').style("top", (d3.event.pageY - 10) + "px").style("left", (d3.event.pageX + 10) + "px"); | |
d3.select('#Date').text(d.year + " - " + mL[d.month - 1]); | |
d3.select('#Temp').text((data.baseTemperature + d.variance).toFixed(3) + " ℃"); | |
d3.select('#Variance').text(d.variance); | |
}) | |
.on("mouseout", function() { | |
d3.select('#tooltip').style("visibility", "hidden"); | |
}); | |
}); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background:lavender; | |
width: 100%; | |
height: 100; | |
display:flex; | |
align-items: center; | |
align-content: center; | |
justify-content: center; | |
flex-flow: column; | |
font: 11px sans-serif; | |
} | |
.axis path { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.dot { | |
stroke: #000; | |
} | |
.tooltip { | |
position: absolute; | |
width: auto; | |
height: auto; | |
pointer-events: none; | |
color: black; | |
padding: 6px; | |
background-color: rgba(70, 130, 180,0.65); | |
font-family: sans-serif; | |
font-size: 70%; | |
visibility: hidden; | |
z-index: 10; | |
display: flex; | |
flex-flow: column; | |
align-items: center; | |
align-content: center; | |
text-align: center; | |
border-radius: 10px | |
/*opacity: 0.65;*/ | |
} | |
.tooltip > span{ | |
margin-top: 3%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment