Skip to content

Instantly share code, notes, and snippets.

@andrewwitherspoon
Last active January 16, 2018 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewwitherspoon/caf7a3887ef474a0a89b071c549fb61a to your computer and use it in GitHub Desktop.
Save andrewwitherspoon/caf7a3887ef474a0a89b071c549fb61a to your computer and use it in GitHub Desktop.
Africa 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

forked from andrewwitherspoon's block: Percent change GDP

date Algeria Angola Benin Botswana Burkina Faso Burundi Cabo Verde Cameroon Central African Republic Chad Comoros Congo Congo, Dem. Rep. Côte d’Ivoire Djibouti Egypt Equatorial Guinea Eritrea Ethiopia Gabon Gambia Ghana Guinea Guinea-Bissau Kenya Lesotho Liberia Libya Madagascar Malawi Mali Mauritania Mauritius Morocco Mozambique Namibia Niger Nigeria Rwanda Sao Tome and Principe Senegal Seychelles Sierra Leone South Africa Sudan Swaziland Tanzania Togo Tunisia Uganda Zambia Zimbabwe AFRICA
2008 2.4 11.2 5.0 6.2 5.8 4.9 6.7 2.9 2.1 3.1 0.6 5.6 6.2 2.3 5.8 7.2 17.8 -9.8 10.8 5.3 5.7 8.4 4.9 3.2 0.2 5.7 6.2 2.7 7.2 8.6 5.0 1.1 5.5 5.9 6.8 2.6 9.6 6.0 11.1 9.1 3.7 -2.1 5.4 3.2 3.8 2.4 5.6 2.4 4.5 10.4 7.8 -17.7 5.3
2009 1.6 2.4 2.7 -7.7 2.9 3.8 -1.3 1.9 1.7 4.2 1.1 7.5 2.9 3.8 1.6 4.9 1.3 3.9 8.8 -2.7 6.4 4.0 -0.3 3.3 3.3 3.4 5.4 -0.8 -3.5 7.6 4.7 -1.0 3.1 4.2 6.5 0.3 -0.7 7.0 6.3 4.0 2.4 -1.1 3.2 -1.5 4.5 1.3 5.4 3.4 3.1 6.9 9.2 5.3 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</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);
var line = d3.line()
.curve(d3.curveCardinal)
.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 -60; }); }),
d3.max(cities, function(c) { return d3.max(c.values, function(d) { return 90; }); })
]);
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