Skip to content

Instantly share code, notes, and snippets.

@thole
Last active September 30, 2016 21:18
Show Gist options
  • Save thole/115ab09659ef3ee4a3acb4cb2a43ec2b to your computer and use it in GitHub Desktop.
Save thole/115ab09659ef3ee4a3acb4cb2a43ec2b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.temperatureLine {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.humidityLine {
fill: none;
stroke: red;
stroke-width: 1.5px;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 960 - margin.left - margin.right,
height = 350 - margin.top - margin.bottom;
var formatDate = d3.time.format("%d-%b-%y");
var x = d3.time.scale()
.range([0, width]);
var yTemperature = d3.scale.linear()
.range([height, 0]);
var yHumidity = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.tickFormat(d3.time.format("%H:%M"))
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yTemperature)
.orient("left");
var yHumidityAxis = d3.svg.axis()
.scale(yHumidity)
.orient("right");
var temperatureLine = d3.svg.line()
.interpolate('basis')
.x(function(d) { return x(d.date); })
.y(function(d) { return yTemperature(d.temp); });
var humidityLine = d3.svg.line()
.interpolate('basis')
.x(function(d) { return x(d.date); })
.y(function(d) { return yHumidity(d.hum); });
d3.csv("https://tholeschneider.de/services/wetter/Output", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.date; }));
yTemperature.domain(d3.extent(data, function(d) { return d.temp; }));
yHumidity.domain(d3.extent(data, function(d) { return d.hum; }));
var sensors = [];
data.forEach(function(d){
if(sensors.indexOf(d.path) != -1){ return; }
sensors.push(d.path);
})
sensors.forEach(function(sensorName){
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("text")
.attr("x",0)
.attr("y",0)
.text(sensorName);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.style("fill","steelblue")
.text("°C");
svg.append("g")
.attr("transform", "translate(" + width + ",0)")
.attr("class", "y axis")
.call(yHumidityAxis)
.append("text")
.attr("transform", "rotate(90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "start")
.style("fill","red")
.text("%");
svg.append("path")
.datum(data.filter(function(d){
return d.path === sensorName;
}))
.attr("class", "temperatureLine")
.attr("d", temperatureLine);
svg.append("path")
.datum(data.filter(function(d){
return d.path === sensorName;
}))
.attr("class", "humidityLine")
.attr("d", humidityLine);
})
});
function type(d) {
d.hum = parseFloat(d.hum);
d.temp = parseFloat(d.temp);
d.date = new Date(Math.round(parseInt(d.date)));
return d;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment