Skip to content

Instantly share code, notes, and snippets.

@dunlapea
Last active February 11, 2016 16:19
Show Gist options
  • Save dunlapea/6e75ad8f8664c245d0a0 to your computer and use it in GitHub Desktop.
Save dunlapea/6e75ad8f8664c245d0a0 to your computer and use it in GitHub Desktop.
Multi-Series Line Chart
license: gpl-3.0

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:

forked from mbostock's block: Multi-Series Line Chart

date Guinea Liberia Sierra Leone
3/1/2014 0 0 0
3/2/2014 0 0 0
3/3/2014 0 0 0
3/4/2014 0 0 0
3/5/2014 0 0 0
3/6/2014 0 0 0
3/7/2014 0 0 0
3/8/2014 0 0 0
3/9/2014 0 0 0
3/10/2014 0 0 0
3/11/2014 0 0 0
3/12/2014 0 0 0
3/13/2014 0 0 0
3/14/2014 0 0 0
3/15/2014 0 0 0
3/16/2014 0 0 0
3/17/2014 0 0 0
3/18/2014 0 0 0
3/19/2014 0 0 0
3/20/2014 0 0 0
3/21/2014 0 0 0
3/22/2014 0 0 0
3/23/2014 0 0 0
3/24/2014 0 0 0
3/25/2014 86 0 0
3/26/2014 86 0 0
3/27/2014 103 8 6
3/31/2014 112 8 0
4/1/2014 122 8 0
4/2/2014 127 8 0
4/7/2014 151 18 0
4/10/2014 157 22 0
4/17/2014 197 27 0
4/21/2014 203 27 0
4/23/2014 208 34 0
4/30/2014 221 13 0
5/5/2014 231 13 0
5/14/2014 233 12 0
5/23/2014 258 12 0
5/27/2014 258 12 1
5/28/2014 281 12 16
6/2/2014 291 13 50
6/5/2014 344 13 81
6/10/2014 372 15 89
6/11/2014 376 15 117
6/18/2014 398 33 97
6/24/2014 390 51 158
7/2/2014 413 107 239
7/7/2014 412 115 252
7/8/2014 408 131 305
7/14/2014 409 142 337
7/16/2014 406 172 386
7/21/2014 410 196 442
7/24/2014 415 224 454
7/28/2014 427 249 525
7/31/2014 460 329 533
8/3/2014 472 391 574
8/4/2014 485 486 646
8/8/2014 495 554 717
8/12/2014 506 599 730
8/13/2014 510 670 783
8/15/2014 519 786 810
8/19/2014 543 834 848
8/21/2014 579 972 907
8/22/2014 607 1082 910
8/28/2014 648 1378 1026
9/6/2014 812 1871 1261
9/8/2014 862 2046 1361
9/12/2014 861 2081 1424
9/16/2014 936 2407 1620
9/18/2014 942 2710 1673
9/22/2014 1008 3022 1813
9/24/2014 1022 3280 1940
9/26/2014 1074 3458 2021
10/1/2014 1157 3696 2304
10/3/2014 1199 3834 2437
10/8/2014 1298 3924 2789
10/10/2014 1350 4076 2950
10/15/2014 1472 4249 3252
10/17/2014 1519 4262 3410
10/22/2014 1540 4665 3706
10/25/2014 1553 4665 3896
10/29/2014 1906 6535 5235
10/31/2014 1667 6535 5338
11/5/2014 1731 6525 4759
11/7/2014 1760 6619 4862
11/12/2014 1878 6822 5368
11/14/2014 1919 6878 5586
11/19/2014 1971 7069 6073
11/21/2014 2047 7082 6190
11/26/2014 2134 7168 6599
11/28/2014 2155 7635 7109
12/3/2014 2164 7635 7312
12/10/2014 2292 7719 7897
12/17/2014 2416 7797 8356
12/24/2014 2597 7862 9004
12/31/2014 2707 8018 9446
1/7/2015 2775 8157 9780
1/14/2015 2806 8331 10124
1/21/2015 2871 8478 10340
1/28/2015 2917 8622 10518
2/4/2015 2975 8745 10740
2/11/2015 3044 8881 10934
2/18/2015 3108 9007 11103
2/25/2015 3155 9238 11301
3/4/2015 3219 9249 11466
3/11/2015 3285 9343 11619
3/18/2015 3389 9526 11751
3/25/2015 3429 9602 11841
4/1/2015 3492 9712 11974
4/8/2015 3515 9862 12138
4/15/2015 3548 10042 12201
4/22/2015 3565 10212 12267
4/29/2015 3584 10322 12371
5/6/2015 3589 10564 12440
5/13/2015 3597 10604 12523
5/20/2015 3635 10666 12632
5/27/2015 3641 10666 12706
6/3/2015 3652 10666 12827
6/10/2015 3670 10666 12901
6/17/2015 3674 10666 12965
6/24/2015 3718 10666 13059
7/1/2015 3729 10666 13119
7/8/2015 3748 10670 13155
7/15/2015 3760 10673 13209
7/22/2015 3783 10672 13250
7/29/2015 3786 10672 13290
8/5/2015 3784 10672 13406
8/12/2015 3787 10672 13470
8/19/2015 3786 10672 13494
8/26/2015 3792 10672 13541
9/3/2015 3792 10672 13638
9/10/2015 3792 10672 13683
9/17/2015 3792 10672 13756
9/24/2015 3800 10672 13823
10/1/2015 3805 10672 13911
10/8/2015 3804 10672 13945
10/15/2015 3800 10672 13982
10/22/2015 3803 10672 14001
10/29/2015 3806 10672 14061
11/5/2015 3810 10672 14089
11/11/2015 3805 10672 14122
11/18/2015 3804 10672 14122
11/25/2015 3804 10675 14122
12/2/2015 3804 10675 14122
12/9/2015 3804 10675 14122
12/16/2015 3807 10675 14122
12/23/2015 3804 10675 14122
12/30/2015 3804 10675 14122
1/6/2016 3804 10675 14122
1/13/2016 3804 10675 14122
1/20/2016 3804 10675 14123
1/27/2016 3804 10675 14124
2/3/2016 3804 10675 14124
2/10/2016 3804 10675 14124
<!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;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.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("%m/%d/%Y").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("graph.txt", 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; }); })
]);
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); });
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