Skip to content

Instantly share code, notes, and snippets.

@jmduke
Forked from mbostock/.block
Last active August 29, 2015 13:57
Show Gist options
  • Save jmduke/9718177 to your computer and use it in GitHub Desktop.
Save jmduke/9718177 to your computer and use it in GitHub Desktop.

This line chart is constructed from a TSV file storing the daily average temperatures of New York, San Francisco and Austin over the last year. The chart employs conventional margins and a number of D3 features:

date minnesota-timberwolves phoenix-suns denver-nuggets dallas-mavericks los-angeles-clippers oklahoma-city-thunder san-antonio-spurs memphis-grizzlies houston-rockets utah-jazz los-angeles-lakers sacramento-kings golden-state-warriors new-orleans-hornets portland-trail-blazers
20121030 2 2 2 1 2 2 2 2 2 2 15 2 2 2 2
20121106 3 11 11 2 7 7 1 3 3 11 11 11 7 3 7
20121113 4 7 7 7 4 2 1 2 11 7 13 15 11 6 13
20121120 5 12 11 8 1 3 3 1 12 8 5 15 5 12 8
20121127 9 9 6 9 4 3 1 2 8 6 9 15 4 14 13
20121204 7 13 7 9 4 1 2 2 6 9 11 15 5 14 11
20121211 8 14 8 7 4 2 1 3 10 6 12 13 5 15 11
20121218 8 13 6 10 2 1 3 4 9 6 12 14 5 15 10
20121225 7 13 7 12 1 2 3 4 6 7 10 14 5 15 10
20130101 9 14 6 12 1 1 3 5 6 11 10 13 4 15 8
20130108 9 14 7 12 1 2 3 4 6 10 11 12 5 15 7
20130115 10 14 6 12 2 1 3 4 7 8 11 13 5 14 8
20130122 10 15 6 11 3 1 2 4 8 7 12 13 5 14 9
20130129 11 14 6 11 3 2 1 4 7 7 10 13 5 14 9
20130205 12 13 4 11 3 2 1 4 8 7 10 14 6 15 9
20130212 12 15 5 11 3 2 1 4 8 7 10 13 6 14 9
20130219 12 15 5 11 3 2 1 4 8 6 9 14 6 13 9
20130226 12 14 5 11 3 2 1 4 8 7 9 14 6 13 10
20130305 12 13 5 11 3 2 1 4 7 8 9 15 6 14 10
20130312 12 13 5 10 3 2 1 4 7 9 8 14 6 14 11
20130319 12 14 3 10 4 2 1 4 7 9 8 13 6 15 11
20130326 12 15 3 9 4 2 1 5 7 9 8 13 6 13 11
20130402 12 15 3 10 5 2 1 3 7 8 8 13 6 14 11
20130409 12 15 3 10 5 2 1 4 7 9 8 13 6 14 11
20130416 12 15 3 10 4 1 2 4 7 9 8 13 6 14 11
20130423 12 15 3 10 4 1 2 4 7 9 7 13 6 14 11
20130430 12 15 3 10 4 1 2 4 7 9 7 13 6 14 11
<!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;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
.line:hover {
stroke-width: 6px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 20, right: 80, bottom: 30, left: 50},
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("left");
var line = d3.svg.line()
.interpolate("basis")
.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) {
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.max(cities, function(c) { return d3.max(c.values, function(v) { return v.temperature; }); }),
d3.min(cities, function(c) { return d3.min(c.values, function(v) { return v.temperature; }); })
]);
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")
.text("Temperature (º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); })
.on("mouseover", function() { d3.select(this).classed("active", true ) }) // classed("active",boolean) not working
.on("mouseout", function() { d3.select(this).classed("active", false) });
city.append("text")
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.date) + "," + y(d.value.temperature) + ")"; })
.attr("x", 3)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment