Skip to content

Instantly share code, notes, and snippets.

@ritchieking
Last active January 3, 2016 18:59
Show Gist options
  • Save ritchieking/8505213 to your computer and use it in GitHub Desktop.
Save ritchieking/8505213 to your computer and use it in GitHub Desktop.
Line chart, clean

A clean version of this block. The curve above shows the world's life expectancy over time.

year lifeExpectancy
1/1/1961 53.00618814
1/1/1962 53.49544588
1/1/1963 54.14244036
1/1/1964 54.97746907
1/1/1965 55.82684751
1/1/1966 56.74868644
1/1/1967 57.61738778
1/1/1968 58.33389775
1/1/1969 59.00507154
1/1/1970 59.59217127
1/1/1971 60.08082426
1/1/1972 60.49296656
1/1/1973 60.85638163
1/1/1974 61.26343093
1/1/1975 61.62668426
1/1/1976 61.97408697
1/1/1977 62.33021711
1/1/1978 62.63088187
1/1/1979 62.94032196
1/1/1980 63.18719679
1/1/1981 63.49452384
1/1/1982 63.79853101
1/1/1983 64.03326825
1/1/1984 64.29128534
1/1/1985 64.54567853
1/1/1986 64.84644025
1/1/1987 65.10007882
1/1/1988 65.3044991
1/1/1989 65.51592402
1/1/1990 65.69645159
1/1/1991 65.86228667
1/1/1992 65.99723977
1/1/1993 66.08334808
1/1/1994 66.25878373
1/1/1995 66.43599275
1/1/1996 66.69034593
1/1/1997 66.95916797
1/1/1998 67.18587271
1/1/1999 67.41704789
1/1/2000 67.69058418
1/1/2001 67.98333357
1/1/2002 68.23767908
1/1/2003 68.49809111
1/1/2004 68.80300304
1/1/2005 69.03462379
1/1/2006 69.33145142
1/1/2007 69.59024029
1/1/2008 69.81932781
1/1/2009 70.07339995
1/1/2010 70.31041664
1/1/2011 70.53933561
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 14px Helvetica;
font-weight: lighter;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.y.axis path {
display: none;
}
.line {
fill: none;
stroke: darkmagenta;
stroke-width: 2px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 20, right: 20, 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 xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.lifeExpectancy); });
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) {
data.forEach(function(d) {
d.year = parseDate(d.year);
d.lifeExpectancy = +d.lifeExpectancy;
});
x.domain(d3.extent(data, function(d) { return d.year; }));
y.domain([50, d3.max(data, function(d) { return d.lifeExpectancy; })]);
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("years");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment