Skip to content

Instantly share code, notes, and snippets.

@solenoid
Last active October 12, 2018 01:44
Show Gist options
  • Save solenoid/95ee09f690e4eb1688a852f30caec437 to your computer and use it in GitHub Desktop.
Save solenoid/95ee09f690e4eb1688a852f30caec437 to your computer and use it in GitHub Desktop.
GDP Growth and Presidential Party
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
2.29999999999977 6.09999999999999 4.40000000000002 5.80000000000014 6.39999999999984 6.50000000000024 2.50000000000003 4.79999999999974 3.10000000000004 3.20680725745444 3.29547672836176 5.26326277619982 5.64312484755203 -0.51715456222523 -0.197678536519447 5.38609005075547 4.60859740653179 5.5616849289446 3.17569075012061 -0.244596225208085 2.59447038823151 -1.91089106804856 4.63245718120484 7.25908695936059 4.23873752083914 3.5116144990922 3.46174769185008 4.20397197941298 3.68052403304713 1.91937029742549 -0.0740845307123976 3.55539614766758 2.74585671892275 4.03764342486481 2.71897578878193 3.79588122942587 4.48702649316731 4.44991096328404 4.68519960839866 4.09217644881066 0.975981833932124 1.78612768745552 2.80677595648093 3.78574284969444 3.34521606334877 2.666625826122 1.77857023965289 -0.291621458693953 -2.77552957416808 2.53192061616315 1.60145467247139 2.32108445977606 2.21930802533575 2.38819999999969
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script>
let margin = { top: 10, right: 40, bottom: 40, left: 10 };
let width = 900 - margin.left - margin.right;
let height = 250 - margin.top - margin.bottom;
let axisOffset = 5;
let parseTime = d3.timeParse('%B %d %Y');
let x = d3.scaleTime()
.range([0, width]);
let y = d3.scaleLinear()
.range([height, 0]);
let line = d3.line()
.x(d => x(d.year))
.y(d => y(d.growth));
let 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.csv(
'presidents.csv',
row => {
row.start = parseTime(row.start);
row.until = parseTime(row.until);
return row;
},
(error, presidentRows) => {
if (error) throw error;
x.domain([new Date(1961, 0, 1), new Date(2014, 0, 1)]);
svg.selectAll('rect')
.data(presidentRows)
.enter()
.append('rect')
.attr('x', d => x(d.start))
.attr('y', 0)
.attr('width', d => x(d.until) - x(d.start))
.attr('height', height)
.style('stroke', '#888')
.style('fill', d => d.party === 'Democrat' ? '#88f' : '#f88')
svg.append('g')
.attr('transform', `translate(0,${height + axisOffset})`)
.call(d3.axisBottom(x));
d3.csv('gdp.csv', (error, gdpRawRows) => {
if (error) throw error;
let gdpRows = gdpRawRows.columns.map(year => {
return {
year: new Date(+year, 0, 1),
growth: gdpRawRows[0][year] / 100
};
});
y.domain(d3.extent(gdpRows.map(d => d.growth)));
svg.append('g')
.attr('transform', `translate(${width +axisOffset},0)`)
.call(d3.axisRight(y).ticks(10, '%'));
svg.append('path')
.datum(gdpRows)
.style('fill', 'none')
.style('stroke', '#444')
.style('stroke-width', 2)
.style('stroke-linecap', 'round')
.style('stroke-linejoin', 'round')
.attr('d', line);
})
}
);
</script>
</body>
start until name party
January 20 1961 November 22 1963 John F. Kennedy Democrat
November 22 1963 January 20 1969 Lyndon B. Johnson Democrat
January 20 1969 August 9 1974 Richard M. Nixon Republican
August 9 1974 January 20 1977 Gerald R. Ford Republican
January 20 1977 January 20 1981 James E. Carter Democrat
January 20 1981 January 20 1989 Ronald W. Reagan Republican
January 20 1989 January 20 1993 George H. W. Bush Republican
January 20 1993 January 20 2001 William J. Clinton Democrat
January 20 2001 January 20 2009 George W. Bush Republican
January 20 2009 January 1 2014 Barack Obama Democrat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment