Skip to content

Instantly share code, notes, and snippets.

@andrewwitherspoon
Last active August 15, 2017 22:45
Show Gist options
  • Save andrewwitherspoon/479c3d37010739023f8dfc5830b937f7 to your computer and use it in GitHub Desktop.
Save andrewwitherspoon/479c3d37010739023f8dfc5830b937f7 to your computer and use it in GitHub Desktop.
Percent change GDP
license: mit

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 Argentina Australia Brazil China France Germany India Indonesia Italy Japan Korea Mexico Netherlands Russia Saudi Arabia Spain Switzerland Turkey United Kingdom United States Algeria Austria Bangladesh Belgium Canada Chile Colombia Czech Republic Denmark Egypt Finland Greece Hong Kong Iran Ireland Israel Kazakhstan Malaysia New Zealand Nigeria Norway Pakistan Peru Philippines Poland Portugal Qatar Romania Singapore South Africa Sweden Thailand United Arab Emirates Venezuela Vietnam
1990 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1991 34.22 4.70 30.50 6.24 0.02 5.49 -15.85 9.87 5.50 13.95 16.60 19.70 2.88 0.22 12.41 7.57 1.18 -0.43 4.54 3.25 -26.32 4.40 -2.03 2.52 2.76 14.26 2.40 -26.69 0.71 -14.28 -9.65 7.41 15.64 -48.93 0.98 12.73 -7.62 11.63 -6.05 -10.94 1.74 13.60 31.28 2.50 29.59 13.36 -6.48 -25.64 25.79 7.26 4.73 15.11 1.68 10.04 48.54
1992 61.86 4.58 -13.28 18.31 10.46 20.29 -10.21 20.62 11.76 24.14 25.31 38.41 14.02 -10.94 3.68 17.59 5.33 5.17 7.91 9.36 -22.63 17.19 0.35 14.34 -0.26 38.81 22.36 -14.54 10.61 -2.95 -20.42 18.73 35.54 -48.93 13.41 25.30 -7.52 34.40 -8.45 -4.73 9.22 21.56 36.84 19.55 42.98 36.69 3.88 -35.58 44.27 16.44 8.58 30.59 6.98 24.29 52.46
1993 67.48 0.30 -5.23 23.24 4.30 17.20 -12.99 48.87 -9.84 42.25 38.29 91.83 11.06 -15.81 -3.01 -2.14 2.44 19.57 -2.91 15.04 -19.50 14.37 4.96 9.44 -2.82 48.87 38.56 0.34 3.58 7.99 -36.93 11.15 56.45 -48.93 6.31 25.60 -13.08 51.95 2.81 -48.67 0.66 28.66 33.12 22.69 45.57 20.70 -2.77 -32.39 67.75 16.23 -18.67 51.03 9.71 23.59 103.67
1994 82.13 3.79 20.82 56.38 9.91 24.99 1.96 66.66 -6.94 56.28 63.09 100.72 19.10 -23.56 1.66 -1.12 13.40 -13.26 4.33 22.23 -31.43 22.27 6.87 19.26 -2.66 72.16 102.87 17.48 12.96 20.33 -26.99 19.11 76.54 -42.44 15.80 42.25 -21.10 69.18 21.58 -41.20 6.13 29.70 69.94 44.62 67.94 26.65 0.19 -22.88 104.08 20.94 -12.42 71.88 16.97 20.21 151.65
1995 82.55 18.29 70.07 103.56 26.24 46.84 12.24 90.44 -0.56 73.54 99.08 30.86 42.09 -23.47 6.04 14.55 32.76 12.48 20.77 28.17 -32.69 44.80 20.07 40.27 1.70 121.80 129.69 47.68 33.82 39.48 -5.17 39.83 88.04 -22.75 40.23 90.45 -24.35 101.49 40.49 -7.19 26.91 51.55 101.86 67.27 115.43 50.07 10.56 -3.42 143.11 34.54 2.28 98.35 29.67 59.28 220.41
1996 92.53 29.03 84.09 139.36 26.58 41.85 22.41 114.22 11.18 53.94 114.10 51.27 41.82 -24.20 10.69 19.79 28.05 20.44 27.43 35.46 -24.34 42.55 46.96 36.30 5.83 135.67 141.25 65.63 35.72 56.80 -6.66 49.00 107.62 -3.53 53.71 109.13 -21.90 129.09 54.17 13.76 36.50 58.26 109.21 86.97 142.42 55.78 23.08 -4.65 166.66 27.74 11.60 114.47 45.11 45.16 281.00
1997 107.18 40.03 91.19 166.48 14.54 25.71 29.56 103.27 5.24 40.60 99.57 82.92 31.16 -21.65 4.60 10.02 11.34 25.99 40.61 43.96 -22.35 27.86 52.68 23.44 9.92 156.55 164.83 52.85 25.53 81.86 -10.38 46.24 130.54 -8.73 67.80 118.22 -17.70 127.16 45.23 16.47 34.70 56.04 120.17 85.83 141.17 48.68 53.49 -8.10 177.06 32.05 2.45 75.97 55.50 82.54 314.78
1998 111.49 28.37 86.97 185.17 18.46 27.10 31.27 -10.08 7.56 28.42 33.97 91.09 37.61 -47.57 -11.56 15.31 14.59 83.02 48.52 52.00 -22.33 31.09 58.19 26.24 6.38 146.35 144.43 64.63 28.03 96.68 -5.36 47.54 119.54 -11.65 82.58 120.68 -17.81 63.93 23.59 4.06 28.69 55.44 110.15 62.95 164.31 57.49 39.33 7.64 137.08 19.23 3.35 33.20 49.25 94.21 320.44
1999 100.58 24.96 29.75 203.17 17.64 24.65 42.94 31.90 6.05 45.29 73.71 120.57 40.64 -62.09 10.18 18.33 12.61 69.82 51.14 61.56 -21.60 30.51 62.26 26.05 13.83 127.02 114.00 60.53 28.73 110.32 -4.45 45.61 115.48 -8.78 100.15 123.30 -37.36 79.79 29.16 16.63 35.47 57.39 90.03 87.30 157.23 61.92 68.37 -7.21 138.67 18.24 4.92 48.42 66.55 108.34 343.21
2000 101.06 33.40 41.88 235.68 7.30 10.48 45.93 55.47 -3.02 55.65 101.05 160.23 31.36 -49.75 17.19 11.27 5.53 81.17 49.61 72.00 -11.69 18.28 68.90 15.25 24.98 135.13 148.02 52.48 18.74 131.48 -11.29 32.94 123.15 -12.20 102.32 152.23 -32.08 113.04 15.67 50.81 43.01 84.83 95.93 82.86 160.52 50.35 141.29 -3.99 165.09 18.01 0.64 48.10 105.79 149.09 419.80
2001 90.09 21.67 21.09 271.17 8.38 10.52 51.24 51.16 -1.27 37.06 90.82 175.86 35.74 -40.67 -2.84 16.98 8.24 32.90 47.56 77.63 -11.77 18.60 70.87 15.22 23.98 114.35 143.84 67.12 19.20 126.36 -8.67 39.13 120.21 1.65 121.09 149.10 -17.75 110.76 18.41 43.51 45.25 80.73 97.01 72.10 188.77 54.40 138.28 4.41 146.97 5.16 -7.06 40.96 103.76 161.34 445.31
2002 -30.86 26.72 9.96 307.51 17.65 17.80 60.43 84.34 7.58 31.06 118.01 182.27 48.08 -33.22 2.97 31.78 16.98 58.24 60.78 83.58 -8.52 28.25 73.19 25.40 27.62 110.60 143.17 102.64 29.21 103.69 -1.39 57.14 116.24 3.06 159.23 130.70 -8.53 129.07 46.45 92.21 63.13 80.72 107.41 83.60 201.13 70.51 163.08 18.41 154.32 -0.06 2.24 57.37 116.59 97.53 486.36
2003 -9.74 49.91 20.86 360.09 44.92 41.97 89.33 121.19 33.32 41.58 143.61 171.51 81.97 -16.73 13.82 69.47 36.73 106.95 85.56 92.50 9.38 57.00 90.39 54.53 50.25 128.43 135.10 146.31 57.76 92.26 20.88 106.27 109.79 23.02 232.87 141.47 14.48 150.32 93.98 119.97 90.96 108.06 122.38 89.36 229.68 109.55 219.73 53.53 168.32 51.67 28.26 78.43 145.25 77.81 560.06
2004 16.49 96.94 44.89 441.86 66.56 59.73 120.93 141.98 52.75 53.35 173.81 193.20 107.00 14.36 19.89 99.88 52.88 168.64 118.54 105.28 37.52 80.57 106.05 79.67 72.28 199.60 190.69 195.11 81.83 82.81 39.04 145.70 119.81 47.18 292.81 157.99 60.22 183.37 128.39 185.61 120.68 144.88 152.81 106.20 286.65 140.32 331.14 95.45 215.86 97.83 47.86 102.59 191.56 139.12 663.69
2005 40.60 122.77 93.01 533.48 72.80 62.12 155.42 169.33 57.36 51.45 221.51 229.77 115.91 47.83 26.94 116.27 58.32 232.78 129.43 118.97 66.33 89.48 119.77 87.65 96.88 271.34 263.92 237.31 91.30 107.94 44.46 153.12 136.03 76.14 328.91 172.12 112.10 226.03 152.15 264.95 157.72 173.68 188.00 132.61 364.00 150.64 505.00 155.66 252.45 123.08 50.70 121.83 256.24 209.41 790.54
2006 64.52 140.05 139.77 662.66 82.31 70.11 190.60 243.48 65.00 44.28 262.20 267.43 131.22 91.54 14.75 136.32 66.73 266.68 145.00 131.72 88.62 101.32 127.29 98.52 121.48 367.44 303.71 284.99 104.62 149.21 53.02 179.21 151.58 107.23 370.41 194.36 200.76 269.55 145.31 372.83 188.35 243.07 235.64 175.80 422.64 164.94 727.15 216.79 308.82 135.08 62.71 159.84 338.09 290.15 925.56
2007 103.41 174.15 202.43 884.37 108.82 94.90 279.26 307.21 87.12 43.80 301.89 297.20 167.10 151.48 10.36 176.46 85.46 348.48 180.19 142.12 117.55 132.72 151.95 128.56 146.66 424.27 415.01 368.35 131.05 202.52 80.46 225.36 175.06 170.38 447.15 242.09 289.30 339.64 201.82 441.18 234.65 280.87 286.86 237.07 550.60 205.09 982.98 339.89 397.85 159.11 88.96 208.10 408.70 389.84 1096.19
2008 155.78 238.87 267.10 1174.24 129.24 112.60 274.79 380.71 103.06 60.44 258.77 319.20 197.91 221.36 24.96 205.55 114.26 407.26 163.04 146.15 175.61 157.50 189.99 151.23 160.83 442.49 505.80 483.41 155.60 277.50 100.50 262.10 185.04 218.23 457.03 312.95 395.46 424.29 192.95 576.48 285.63 325.08 356.45 293.11 709.08 232.83 1466.08 433.86 431.72 148.17 99.09 241.43 522.22 571.84 1431.74
2009 135.56 197.72 260.86 1316.05 111.23 93.66 318.05 408.36 85.60 66.61 222.87 240.66 172.99 136.57 -17.45 180.15 109.59 327.84 116.54 141.13 121.15 139.42 224.31 134.73 130.86 420.60 480.57 410.30 132.37 338.16 77.72 237.11 178.24 219.66 377.70 296.39 328.14 359.42 166.70 451.03 222.55 320.27 357.48 279.89 566.58 209.63 1228.70 329.34 432.22 156.10 66.43 230.09 400.08 601.26 1538.12
2010 199.70 266.98 378.16 1590.59 107.55 93.61 423.09 611.41 80.50 81.53 291.80 300.11 166.14 195.06 23.10 167.54 125.78 412.27 122.26 150.26 159.82 134.98 264.83 134.24 171.66 559.96 612.66 413.49 132.91 407.50 75.10 205.81 197.21 274.79 348.48 345.33 449.69 479.26 222.19 1099.93 257.73 343.40 458.60 350.43 626.49 202.72 1599.93 330.81 553.97 224.83 89.18 299.69 471.74 736.08 1691.35
2011 275.07 346.51 466.34 1998.48 124.47 112.90 475.64 741.31 93.34 96.10 330.45 345.81 184.39 293.13 27.08 178.08 170.48 452.54 138.65 159.51 222.37 158.36 307.10 155.29 201.15 661.77 732.83 465.41 148.83 447.18 93.39 194.00 223.05 374.34 387.48 397.95 615.21 576.79 270.28 1238.70 315.85 433.83 550.36 405.83 701.51 211.09 2179.42 375.34 662.33 260.37 118.13 334.50 592.11 572.97 1994.33
2012 286.26 393.92 433.65 2272.27 110.26 100.80 477.09 764.77 76.06 97.56 337.73 351.68 163.77 319.91 9.64 149.66 158.35 480.04 142.05 170.17 236.93 145.36 322.03 141.19 207.16 706.68 817.86 414.38 136.64 547.74 81.40 150.96 241.40 370.47 357.55 390.84 672.29 614.25 287.27 1398.69 325.49 460.81 629.44 464.39 658.35 174.85 2438.35 340.22 699.85 242.98 110.68 365.84 639.26 710.76 2307.70
2013 290.53 403.23 435.30 2562.32 120.22 112.61 486.28 759.73 80.96 64.20 367.37 380.37 175.78 331.61 1.45 154.49 166.04 530.89 148.77 179.14 238.11 157.88 374.68 151.96 210.24 740.69 844.01 419.40 148.53 569.10 90.77 145.03 258.38 309.91 384.80 458.80 778.61 634.32 318.77 1574.30 336.38 477.90 661.89 513.47 694.53 187.18 2599.94 391.21 736.77 217.28 124.18 392.75 670.05 688.90 2545.69
2014 272.35 368.68 431.66 2804.84 123.42 119.79 542.69 739.28 82.76 54.42 405.22 394.23 179.90 299.30 1.30 157.12 172.98 519.98 174.32 190.87 244.88 163.98 447.13 157.59 201.87 688.16 839.05 415.48 154.83 608.39 92.63 141.17 278.87 240.77 419.25 488.24 722.11 667.92 341.13 1748.35 316.01 510.74 661.25 542.23 726.27 191.70 2701.80 411.58 752.35 203.63 122.28 376.34 695.24 688.90 2777.20
2015 313.66 332.01 290.44 2966.21 90.82 90.58 566.80 711.43 55.00 39.59 394.99 338.14 138.75 164.29 -13.50 122.94 160.58 470.35 161.72 201.64 165.58 127.00 517.37 120.41 161.45 632.38 623.84 359.26 117.95 671.38 64.19 99.06 302.20 215.22 474.85 470.42 584.63 573.00 285.89 1464.08 222.71 577.45 616.43 560.72 623.48 152.89 2136.84 355.24 721.09 174.68 92.01 367.80 605.99 688.90 2885.92
2016 286.17 286.81 288.83 3003.47 93.32 96.42 614.73 778.32 57.13 57.31 405.19 298.16 145.28 148.28 -1.20 130.25 156.32 469.27 139.57 210.54 151.56 132.70 600.72 125.92 157.57 645.99 601.35 378.53 121.45 679.72 67.32 98.75 317.16 215.22 495.80 507.24 396.26 573.17 306.67 1217.04 209.33 608.97 627.34 588.09 611.62 159.86 1971.46 378.75 721.44 155.16 97.94 376.71 587.84 688.90 3030.78
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.axis--x path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<svg width="650" height="500"></svg>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 150, bottom: 30, left: 50},
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");
var x = d3.scaleTime().range([0, width]),
y = d3.scaleLinear().range([height, 0]),
z = d3.scaleOrdinal(d3.schemeCategory10);
// y = d3.scaleLog()
// .domain([Math.exp(0), Math.exp(9)])
// .range([height, 0])
var line = d3.line()
.curve(d3.curveBasis)
.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]};
})
};
});
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([
d3.min(cities, function(c) { return d3.min(c.values, function(d) { return d.temperature; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(d) { return d.temperature; }); })
]);
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")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Percent change in GDP");
var city = g.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 z(d.id); });
city.append("text")
.datum(function(d) { return {id: d.id, 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", "0.35em")
.style("font", "10px sans-serif")
.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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment