|
<!DOCTYPE html> |
|
<meta charset="utf-8"> |
|
<style> |
|
|
|
.line { |
|
fill: none; |
|
stroke: steelblue; |
|
stroke-width: 1.5px; |
|
} |
|
|
|
</style> |
|
<svg width="960" height="500"></svg> |
|
<script src="//d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
|
|
var svg = d3.select("svg"), |
|
margin = {top: 20, right: 80, bottom: 30, left: 70}, |
|
width = svg.attr("width") - margin.left - margin.right, |
|
height = svg.attr("height") - margin.top - margin.bottom, |
|
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
var parseTime = d3.timeParse("%Y%m%d"); |
|
|
|
var x = d3.scaleTime().range([0, width]), |
|
y = d3.scaleLinear().range([height, 0]), |
|
z = d3.scaleOrdinal(d3.schemeCategory10); |
|
|
|
var line = d3.line() |
|
.curve(d3.curveLinear) |
|
.x(function(d) { return x(d.date); }) |
|
.y(function(d) { return y(d.temperature); }); |
|
|
|
d3.tsv("data.tsv", type, function(error, data) { |
|
if (error) throw error; |
|
|
|
var cities = data.columns.slice(1).map(function(id) { |
|
return { |
|
id: id, |
|
values: data.map(function(d) { |
|
return {date: d.date, temperature: d[id]}; |
|
}) |
|
}; |
|
}); |
|
|
|
var minTemp, maxTemp; |
|
var yAxisPad = 5; |
|
|
|
x.domain(d3.extent(data, function(d) { return d.date; })); |
|
|
|
y.domain([ |
|
(minTemp = d3.min(cities, function(c) { return d3.min(c.values, function(d) { return d.temperature; }) })) - yAxisPad, |
|
(maxTemp = d3.max(cities, function(c) { return d3.max(c.values, function(d) { return d.temperature; }) })) + yAxisPad |
|
]); |
|
|
|
z.domain(cities.map(function(c) { return c.id; })); |
|
|
|
g.append("g") |
|
.attr("class", "axis axis--x") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x)); |
|
|
|
g.append("g") |
|
.attr("class", "axis axis--y") |
|
.attr("transform", "translate(" + width + ", 0)") |
|
.call(d3.axisRight(y).tickFormat(function(d) {return d + "º"})) |
|
.append("text") |
|
.attr("transform", "rotate(90)") |
|
.attr("y", 6) |
|
.attr("dy", "0.71em") |
|
.attr("fill", "#000") |
|
.text("Fahrenheit"); |
|
|
|
var city = g.selectAll(".city") |
|
.data(cities) |
|
.enter().append("g") |
|
.attr("class", "city"); |
|
|
|
var marks = city.append("g") |
|
.selectAll("circle", "text") |
|
.data(function(d) {return d.values;}) |
|
.enter(); |
|
|
|
marks.append("circle") |
|
.attr("cx", function(d) { return x(d.date);} ) |
|
.attr("cy", function(d) { return y(d.temperature);} ) |
|
.attr("r", "0.12em") |
|
.style("fill", function (d) { return z(this.parentNode.__data__.id); }); |
|
|
|
marks.filter(function (d) {return d.temperature == minTemp;}) |
|
.append("text") |
|
.attr("x", function(d) {return x(d.date);}) |
|
.attr("y", function(d) {return y(d.temperature);}) |
|
.attr("dy", "1em") |
|
.attr("dx", "-1em") |
|
.style("font", "0.7em sans-serif") |
|
.style("fill", function (d) { return z(this.parentNode.__data__.id); }) |
|
.text(function (d) {return d.temperature + "º";}); |
|
|
|
marks.filter(function (d) {return d.temperature == maxTemp;}) |
|
.append("text") |
|
.attr("x", function(d) {return x(d.date);}) |
|
.attr("y", function(d) {return y(d.temperature);}) |
|
.attr("dy", "-0.3em") |
|
.attr("dx", "-1em") |
|
.style("font", "0.7em sans-serif") |
|
.style("fill", function (d) { return z(this.parentNode.__data__.id); }) |
|
.text(function (d) {return d.temperature + "º";}); |
|
|
|
|
|
city.append("path") |
|
.attr("class", "line") |
|
.attr("d", function(d) { return line(d.values); }) |
|
.style("stroke", function(d) { return z(d.id); }); |
|
|
|
city.append("text") |
|
.datum(function(d) { return {id: d.id, value: d.values[0]}; }) |
|
.attr("transform", function(d) { return "translate(" + (x(d.value.date)-margin.left) + "," + y(d.value.temperature) + ")"; }) |
|
.attr("x", 3) |
|
.attr("dy", function(d) { |
|
if (d.id == "San Francisco") {v = "1.2em";} |
|
else {v = "0.35em";} |
|
return v; |
|
} |
|
) |
|
.attr("dx", function(d) { |
|
if(d.id == "Austin") return ("3.5em"); |
|
else if (d.id == "New York") return ("2em"); { |
|
} |
|
}) |
|
.style("font", "10px sans-serif") |
|
.style( "fill", function(d) {return z(d.id); }) |
|
.text(function(d) { return d.id; }); |
|
}); |
|
|
|
function type(d, _, columns) { |
|
d.date = parseTime(d.date); |
|
for (var i = 1, n = columns.length, c; i < n; ++i) d[c = columns[i]] = +d[c]; |
|
return d; |
|
} |
|
|
|
</script> |