|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
body { |
|
font: 10px sans-serif; |
|
} |
|
|
|
svg { |
|
background: #f5f5f5; |
|
} |
|
.axis path, |
|
.axis line { |
|
fill: none; |
|
stroke: #a9a9a9; |
|
shape-rendering: crispEdges; |
|
} |
|
|
|
.axis text { |
|
fill: #a9a9a9; |
|
} |
|
.x.axis path { |
|
|
|
} |
|
|
|
.line { |
|
fill: none; |
|
stroke: steelblue; |
|
stroke-width: 1.5px; |
|
} |
|
|
|
</style> |
|
<body> |
|
<script src="//d3js.org/d3.v3.min.js"></script> |
|
<script> |
|
|
|
var margin = {top: 20, right: 50, bottom: 30, left: 80}, |
|
width = 960 - margin.left - margin.right, |
|
height = 500 - margin.top - margin.bottom; |
|
|
|
var parseDate = d3.time.format("%Y%m%d").parse; |
|
|
|
var x = d3.time.scale() |
|
.range([0, width]); |
|
|
|
var y = d3.scale.linear() |
|
.range([height, 0]); |
|
|
|
var color = d3.scale.category10(); |
|
|
|
var xAxis = d3.svg.axis() |
|
.scale(x) |
|
.orient("bottom"); |
|
|
|
var yAxis = d3.svg.axis() |
|
.scale(y) |
|
.orient("right") |
|
.tickFormat(function(d) {return d + "º"}); |
|
|
|
var line = d3.svg.line() |
|
.interpolate("linear") |
|
.x(function(d) { return x(d.date); }) |
|
.y(function(d) { return y(d.temperature); }); |
|
|
|
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 + ")"); |
|
|
|
d3.tsv("data.tsv", function(error, data) { |
|
if (error) throw error; |
|
|
|
color.domain(d3.keys(data[0]).filter(function(key) { return key !== "date"; })); |
|
|
|
data.forEach(function(d) { |
|
d.date = parseDate(d.date); |
|
}); |
|
|
|
var cities = color.domain().map(function(name) { |
|
return { |
|
name: name, |
|
values: data.map(function(d) { |
|
return {date: d.date, temperature: +d[name]}; |
|
}) |
|
}; |
|
}); |
|
|
|
x.domain(d3.extent(data, function(d) { return d.date; })); |
|
|
|
y.domain([ |
|
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); }), |
|
d3.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); }) |
|
]); |
|
|
|
y.domain([ |
|
d3.min(y.domain()) - 5, |
|
d3.max(y.domain()) + 5 |
|
]); |
|
|
|
svg.append("g") |
|
.attr("class", "x axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(xAxis); |
|
|
|
svg.append("g") |
|
.attr("class", "y axis") |
|
.attr("transform", "translate(" + width + ", 0)") |
|
.call(yAxis) |
|
.append("text") |
|
.attr("transform", "rotate(-90)") |
|
.attr("y", 6) |
|
.attr("dy", "-1.25em") |
|
.style("text-anchor", "end") |
|
.text("Temp (ºF)"); |
|
|
|
var city = svg.selectAll(".city") |
|
.data(cities) |
|
.enter().append("g") |
|
.attr("class", "city"); |
|
|
|
city.append("path") |
|
.attr("class", "line") |
|
.attr("d", function(d) { return line(d.values); }) |
|
.style("stroke", function(d) { return color(d.name); }); |
|
|
|
city.append("text") |
|
.datum(function(d) { return {name: d.name, value: d.values[0]}; }) |
|
.attr("transform", function(d) { return "translate(0, " + y(d.value.temperature) + ")"; }) |
|
.attr("x", -3) |
|
.attr("text-anchor", "end") |
|
.attr("dy", ".35em") |
|
.style("fill", function(d) { return color(d.name);}) |
|
.text(function(d) { return d.name; }); |
|
}); |
|
|
|
</script> |