Skip to content

Instantly share code, notes, and snippets.

@wmerrow
Created May 10, 2015 22:39
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 wmerrow/592f07f78fc4cc3bb0a2 to your computer and use it in GitHub Desktop.
Save wmerrow/592f07f78fc4cc3bb0a2 to your computer and use it in GitHub Desktop.
Will's module 6 exercise
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Will's mod6 exercise</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: sans-serif;
}
h1 {
font-size: 30px;
margin: 0;
}
p {
font-size: 18px;
margin: 10px 0 15px 0;
color: black;
}
svg {
background-color: whitesmoke;
}
.axis path,
.axis line {
fill: none;
stroke: gray;
shape-rendering: crispEdges;
}
.y2.axis line {
fill: none;
stroke: gray;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
}
.x.axis text {font-size: 14px;}
.x2.axis text {opacity: 0;}
.y.axis text {font-size: 14px;}
.y2.axis text {opacity: 0;}
</style>
</head>
<body>
<h1>The Great Divergence</h1>
<p>Total contributions by PACs representing <strong style= "color: #00688B">corporations</strong> and <strong style= "color: #009ACD">labor unions</strong> per two-year election period (2014 dollars).</p>
<script type="text/javascript">
var w = 750;
var h = 500;
var padding = [ 40, 60, 50, 60 ]; //Top, right, bottom, left
var dateFormat = d3.time.format("%Y");
var xScale = d3.time.scale()
.range([ padding[3], w - padding[1] ]);
var yScale = d3.scale.linear()
.range([ padding[0], h - padding[2] ]);
//Configure axis generators
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickFormat(function(d) {return dateFormat(d);})
.ticks(d3.time.year, 4)
.tickSize(8)
;
var xAxis2 = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.tickFormat(function(d) {return dateFormat(d);})
.ticks(d3.time.year, 2)
;
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5)
.tickFormat(function(d){return "$"+d+"m";})
;
var yAxis2 = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5)
.tickSize(0 - w + padding[1] + padding[3])
;
//Configure line generator
var line = d3.svg.line()
.x(function(d) {
return xScale(dateFormat.parse(d.ElectionYear));
})
.y(function(d) {
return yScale(d.SpendingInf);
});
//Create SVG
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Load PAC data
d3.csv("PACdata-Corporations-Inf.csv", function(corpData) {
//Load Labor data
d3.csv("PACdata-Labor-Inf.csv", function(laborData) {
//Create merged data
var mergedData = corpData.concat(laborData);
xScale.domain([
d3.min(mergedData, function(d) {
return dateFormat.parse(d.ElectionYear);
}),
d3.max(mergedData, function(d) {
return dateFormat.parse(d.ElectionYear);
})
]);
yScale.domain([
d3.max(mergedData, function(d) {
return +d.SpendingInf;
}) + 16.6,
0
]);
//Draw axes
svg.append("g")
.attr("class", "x2 axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis2);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y2 axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis2);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + (padding[3]) + ",0)")
.call(yAxis);
//Draw lines
svg.data([ corpData ])
.append("path")
.attr("class", "line corp")
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "#00688B")
.attr("stroke-width", 3);
svg.data([ laborData ])
.append("path")
.attr("class", "line labor")
.attr("d", line)
.attr("fill", "none")
.attr("stroke", "#009ACD")
.attr("stroke-width", 3);
});
//End labor data load function
});
//End corp data load function
</script>
</body>
</html>
ElectionYear SpendingInf
1978 34.485
1980 55.104
1982 67.375
1984 80.94
1986 99.792
1988 100.8
1990 96.835
1992 108.667
1994 102.56
1996 105.247
1998 103.095
2000 115.354
2002 120.912
2004 130.375
2006 150.228
2008 157.96
2010 167.533
2012 172.113
2014 183.4
ElectionYear SpendingInf
1978 35.937
1980 37.884
1982 49.735
1984 56.544
1986 64.584
1988 67.8
1990 60.816
1992 67.093
1994 65.12
1996 70.215
1998 62.93
2000 68.774
2002 68.508
2004 62.875
2006 66.573
2008 67.1
2010 67.144
2012 54.281
2014 50.9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment