Skip to content

Instantly share code, notes, and snippets.

@curran
Last active January 17, 2017 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save curran/c7d740b2e91dda4f257d4174be506c42 to your computer and use it in GitHub Desktop.
Save curran/c7d740b2e91dda4f257d4174be506c42 to your computer and use it in GitHub Desktop.
Live Weather Data
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body {
font-family: sans-serif;
font-size: 4em;
margin: 1em;
}
</style>
</head>
<body>
<p>Temperature in London</p>
<p id="temp-c">loading...</p>
<p id="temp-f"></p>
<script>
var url = "https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=a3d9b50b2022aaff3f6b7eb24288e5c8";
var format = d3.format(".2f");
function update(){
d3.json(url, function (data){
var kelvin = data.main.temp;
d3.select("#temp-c").text(format(celcuis(kelvin)) + " °C");
d3.select("#temp-f").text(format(fahrenheit(kelvin)) + " °F");
});
}
function celcuis(kelvin){ return kelvin - 273.15; }
function fahrenheit(kelvin){ return celcuis(kelvin) * 9/5 + 32; }
update();
var fiveMinutes = 1000 * 60 * 5;
setInterval(update, fiveMinutes);
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment