Skip to content

Instantly share code, notes, and snippets.

@wmerrow
Created April 27, 2015 00:07
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/9c78ac38b18190afb1d2 to your computer and use it in GitHub Desktop.
Save wmerrow/9c78ac38b18190afb1d2 to your computer and use it in GitHub Desktop.
Will's module 5 exercise
<!DOCTYPE html>
<head>
<html lang="en">
<meta charset="utf-8">
<title>Will's module 4 exercise</title>
<script type="text/javascript" src="http://d3js.org/d3.v3.js"></script>
<style type="text/css">
p1 {color: darkslategray; font-family: sans-serif; font-size: 35px; font-weight: bold;}
p2 {color: black; font-size: 18px;}
p3 {color: black; font-size: 12px;}
body {
background-color: whitesmoke; font-family: sans-serif;
}
svg {
background-color: lightgray;
}
circle:hover {fill: orange;}
.axis path,
.axis line {
fill: none; stroke: gray; shape-rendering: crispEdges; }
.y.axis path,
.y.axis text {font-size: 14px;}
.x.axis path,
.x.axis text {font-size: 14px;}
</style>
</head>
<body>
<p1>The Rise of PACs</p1>
<br>
<br>
<script type="text/javascript">
//these vars are the dimensions of the svg canvas (in pixels)
var w = 900;
var h = 400;
//PADDING:
//padding will leave blank space between the chart area and the svg edges
var padding = [35, 25, 75, 100];
//SCALES:
//creates an ordinal scale for the x axis (years), sets range bands (in pixels)
var xScale = d3.scale.linear().range([padding[3], w-padding[1]]);
//creates a linear scale for the y axis (# PACs), sets range (in pixels)
var yScale = d3.scale.linear().range([h-padding[2]-padding[0],0]);
//AXES:
//creates a var for an x axis at the bottom of the chart
var xAxis = d3.svg.axis().scale(xScale).orient("bottom").ticks(4).tickSize(-h+padding[0]+padding[2]).tickPadding(8);
//creates a var for a y axis at the left of the chart
var yAxis = d3.svg.axis().scale(yScale).orient("left").ticks(3,"$").tickSize(-w+padding[1]+padding[3]).tickPadding(6);
//creates a var for an SVG
var svg = d3.select("body").append("svg").attr("width",w).attr("height",h);
d3.csv("PACdata.csv", function(data) {
//sets x axis domain (ordinal scale)
xScale.domain([0, d3.max(data, function(d){return +d.TotalPACs + 336; }) ]);
//sets y axis domain from 0 to max number of super pacs (linear scale) - expressed as an array of two numbers, 0 to max.
yScale.domain([0, d3.max(data, function(d){return +d.TotalContributionsAdj + 144.2; }) ]);
//DRAW AXES:
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h - padding[2]) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding[3] + "," + padding[0] + ")")
.call(yAxis);
svg.append("text")
.attr("x",w/2-15)
.attr("y",h-padding[2]+50)
.text("Number of PACs");
svg.append("text")
.attr("text-anchor", "middle")
.attr("y", padding[3]/2-15)
.attr("x",-h/2+20)
.attr("transform", "rotate(-90)")
.text("Total PAC contributions (2014 dollars)");
//DRAW CIRCLES:
var circles =
svg.selectAll("circle")
.data(data)
.enter()
.append("circle");
circles
//sets circle center using x and y
.attr("cx",function(d){return xScale(d.TotalPACs);})
.attr("cy",function(d){return padding[0]+yScale(d.TotalContributionsAdj);})
//sets radius
.attr("r", 4)
.attr("fill","darkslategray")
;
} );
</script>
<br>
<p3>Source: Campaign Finance Institute, 2015</p3>
</body>
</html>
ElectionYear Corporate PAC Contributions Trade/Membership/Health PAC Contributions Labor PAC Contributions Nonconnected PAC Contributions Other PAC Contributions TotalContributions TotalContributionsAdj Corporate PACs Trade/Membership/Health PACs Labor PACs Nonconnected PACs Cooperative PACs Corporate (without stock) PACs TotalPACs
1978 9.5 11.2 9.9 2.5 1 34.1 98.1 704 122 215 400 11 22 1474
1980 19.2 15.9 13.2 4.9 2 55.2 134.55 1037 463 225 201 27 44 1997
1982 27.5 21.9 20.3 10.7 3.2 83.6 175.49 1317 407 293 204 46 78 2345
1984 35.5 26.7 24.8 14.5 3.8 105.3 205.36 1584 598 261 576 51 117 3187
1986 46.2 32.9 29.9 18.8 4.9 132.7 245.82 1584 598 261 576 51 117 3187
1988 50.4 38.9 33.9 19.2 5.4 147.8 257.95 1616 633 256 630 51 122 3308
1990 53.5 42.5 33.6 14.3 5.9 149.7 242.51 1540 609 233 511 51 114 3058
1992 64.3 51.4 39.7 17.5 6.6 179.4 275 1514 633 255 534 48 114 3098
1994 64.1 50.1 40.7 17.3 6.6 178.8 262.13 1468 633 255 509 50 112 3027
1996 69.7 56.2 46.5 22 6.8 201.2 283.7 1470 650 236 529 41 109 3035
1998 71.1 59 43.4 27.1 6.2 206.8 283.67 1425 664 232 560 41 102 3024
2000 84.2 68.3 50.2 35.6 7.1 245.4 324.51 1365 662 236 670 37 94 3064
2002 91.6 71.5 51.9 44.6 6.5 266.1 338.79 1359 697 215 700 36 86 3093
2004 104.3 78.2 50.3 49.8 6.5 289.1 351.24 1402 722 206 819 34 75 3258
2006 128.4 98.4 56.9 70.9 8.74 363.3 414.91 1463 734 202 870 35 89 3393
2008 143.6 106.1 61 63.8 11.4 385.9 421.1 1470 794 203 1023 39 84 3613
2010 153.7 126.2 61.6 53.6 11.7 406.8 435.21 1492 907 199 842 34 83 3557
2012 167.1 117.1 52.7 77.1 11.5 425.5 438.14 1223 700 137 902 29 55 3046
2014 183.4 136.5 50.9 72.8 12.2 455.8 455.8 1477 898 182 1003 37 67 3664
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment