Skip to content

Instantly share code, notes, and snippets.

@ilanman
Last active January 16, 2020 10:12
Show Gist options
  • Save ilanman/9478259 to your computer and use it in GitHub Desktop.
Save ilanman/9478259 to your computer and use it in GitHub Desktop.
Congressional Spite

This graph looks at how the Senate and House Democrats and Republicans have voted with or against the President over the years. It's clear what the President's party is by Congress' voting patterns. More info here.
Main inspiration was this.

<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
body {
font: 8px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
fill: none;
size: 15px;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke-width: 5px;
}
.Body {opacity: 0.3;}
.Body:hover {opacity:1;}
</style>
<body>
<div>
<p id="menu"><b>Senate or House</b><br>Select Legislative Body:<select>
<option value="Senate">Senate</option>
<option value="House">House</option></select>
</div>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var margin = {top: 20, right: 70, bottom: 30, left: 60},
w = 650 - margin.left - margin.right,
h = 450 - margin.top - margin.bottom,
x = d3.time.scale().range([0, w]),
y = d3.scale.linear().domain([0,1]).range([h, 0]),
parseDate = d3.time.format("%Y").parse;
var color = d3.scale.ordinal().range(["#1f77b4", "#d62728"]); // to generate a different color for each path
var sections,
filtered,
transpose;
// where the line gets its properties, how it will be interpolated
var line = d3.svg.line()
.interpolate("basis")
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.stat); });
// add svg box where viz will go
var svg = d3.select("body").append("svg")
.attr("width", w + margin.left + margin.right+40)
.attr("height", h + margin.top + margin.bottom+20)
.append("g")
.attr("transform", "translate(" + (+20+margin.left) + "," + margin.top + ")");
// define the x axis and its class, append it to svg
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
svg.append("g")
.attr("class", "x axis");
yScale = d3.scale.linear().range([h, 0]);
// define the y axis and its class, append it to svg
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.tickFormat(d3.format(".0%"))
svg.append("g")
.attr("class", "y axis");
// force data to update when menu is changed
var menu = d3.select("#menu select")
.on("change", change);
// run redraw function that will refresh whenever a new data series is selected
d3.json("spite.json",function(json){
sections = json
redraw();
});
function change() {
d3.transition()
.duration(1500)
.each(redraw);
}
function redraw() {
var nested = d3.nest()
.key(function(d) { return d.Body; })
.map(sections)
// get value from menu selection
// the option values are set in HTML and correspond
// to the Body value we used to nest the data
var series = menu.property("value");
// only retrieve data from the selected series, using the nest we just created
var data = nested[series];
// the keyring variable contains only the names of the sections
var keyring = d3.keys(data[1]).filter(function(key) {
return (key !== "Body" && key !== "year" && key != "South" && key != "President");
});
// get the year and # of votes, map them to each group separately
var transpose = keyring.map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {year: parseDate(d.year), stat: +d[name]};
})
};
});
x.domain([
d3.min(transpose, function(c) { return d3.min(c.values, function(v) { return v.year; }); }),
d3.max(transpose, function(c) { return d3.max(c.values, function(v) { return v.year; }); })
]);
y.domain([
d3.min(transpose, function(c) { return d3.min(c.values, function(v) { return v.stat; }); }),
d3.max(transpose, function(c) { return d3.max(c.values, function(v) { return v.stat; }); })
]);
var Body = svg.selectAll(".Body")
.data(transpose);
var BodyEnter = Body.enter().append("g")
.attr("class", "Body")
.attr("id", function(d) { return d.name; });
BodyEnter.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); });
BodyEnter.append("text")
.attr("class", "names")
.style("font-family","sans-serif")
.datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; })
.attr("transform", function(d) { return "translate(" + x(d.value.year) + "," + y(d.value.stat) + ")"; })
.attr("x", 4)
.attr("dy", ".35em")
.text(function(d) { return d.name; });
var BodyUpdate = d3.transition(Body);
BodyUpdate.select("path")
.attr("d", function(d) { return line(d.values); });
BodyUpdate.select("text")
.style("font-family","sans-serif")
.attr("transform", function(d) { return "translate(" + x(d.values[d.values.length - 1].year) + "," +
y(d.values[d.values.length - 1].stat) + ")"; });
d3.transition(svg).select(".y.axis")
.call(yAxis);
Body.enter().append("text")
.attr("transform", "rotate(-90)")
.attr("dy", "1.2em")
.style("text-anchor", "end")
.style("stroke-width","2px")
.style("font-family","sans-serif")
.text("% Support");
d3.transition(svg).select(".x.axis")
.attr("transform", "translate(0," + h + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.style("font-family","sans-serif")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function(d) {
return "rotate(-45)"
});
}
</script>
</html>
[{"Body":"House","year":"1955","President":"Eisenhower","Democrats":"58","South":"0","Republicans":"67"},
{"Body":"House","year":"1956","President":"Eisenhower","Democrats":"58","South":"0","Republicans":"79"},
{"Body":"House","year":"1957","President":"Eisenhower","Democrats":"54","South":"0","Republicans":"60"},
{"Body":"House","year":"1958","President":"Eisenhower","Democrats":"63","South":"0","Republicans":"65"},
{"Body":"House","year":"1959","President":"Eisenhower","Democrats":"44","South":"0","Republicans":"76"},
{"Body":"House","year":"1960","President":"Eisenhower","Democrats":"49","South":"0","Republicans":"63"},
{"Body":"House","year":"1961","President":"Kennedy","Democrats":"81","South":"0","Republicans":"41"},
{"Body":"House","year":"1962","President":"Kennedy","Democrats":"83","South":"71","Republicans":"47"},
{"Body":"House","year":"1963","President":"Kennedy","Democrats":"84","South":"71","Republicans":"36"},
{"Body":"House","year":"1964","President":"Johnson","Democrats":"84","South":"70","Republicans":"42"},
{"Body":"House","year":"1965","President":"Johnson","Democrats":"83","South":"65","Republicans":"46"},
{"Body":"House","year":"1966","President":"Johnson","Democrats":"81","South":"64","Republicans":"45"},
{"Body":"House","year":"1967","President":"Johnson","Democrats":"80","South":"65","Republicans":"51"},
{"Body":"House","year":"1968","President":"Johnson","Democrats":"77","South":"63","Republicans":"59"},
{"Body":"House","year":"1969","President":"Nixon","Democrats":"56","South":"55","Republicans":"65"},
{"Body":"House","year":"1970","President":"Nixon","Democrats":"64","South":"64","Republicans":"79"},
{"Body":"House","year":"1971","President":"Nixon","Democrats":"53","South":"69","Republicans":"79"},
{"Body":"House","year":"1972","President":"Nixon","Democrats":"56","South":"59","Republicans":"74"},
{"Body":"House","year":"1973","President":"Nixon","Democrats":"39","South":"49","Republicans":"67"},
{"Body":"House","year":"1974","President":"Nixon","Democrats":"52","South":"64","Republicans":"71"},
{"Body":"House","year":"1974","President":"Ford","Democrats":"48","South":"52","Republicans":"59"},
{"Body":"House","year":"1975","President":"Ford","Democrats":"40","South":"48","Republicans":"67"},
{"Body":"House","year":"1976","President":"Ford","Democrats":"36","South":"52","Republicans":"70"},
{"Body":"House","year":"1977","President":"Carter","Democrats":"69","South":"58","Republicans":"46"},
{"Body":"House","year":"1978","President":"Carter","Democrats":"67","South":"54","Republicans":"40"},
{"Body":"House","year":"1979","President":"Carter","Democrats":"70","South":"58","Republicans":"37"},
{"Body":"House","year":"1980","President":"Carter","Democrats":"71","South":"63","Republicans":"44"},
{"Body":"House","year":"1981","President":"Reagan","Democrats":"46","South":"60","Republicans":"72"},
{"Body":"House","year":"1982","President":"Reagan","Democrats":"43","South":"55","Republicans":"70"},
{"Body":"House","year":"1983","President":"Reagan","Democrats":"30","South":"45","Republicans":"74"},
{"Body":"House","year":"1984","President":"Reagan","Democrats":"37","South":"47","Republicans":"64"},
{"Body":"House","year":"1985","President":"Reagan","Democrats":"31","South":"43","Republicans":"69"},
{"Body":"House","year":"1986","President":"Reagan","Democrats":"26","South":"37","Republicans":"69"},
{"Body":"House","year":"1987","President":"Reagan","Democrats":"26","South":"36","Republicans":"64"},
{"Body":"House","year":"1988","President":"Reagan","Democrats":"27","South":"34","Republicans":"61"},
{"Body":"House","year":"1989","President":"Bush","Democrats":"38","South":"49","Republicans":"72"},
{"Body":"House","year":"1990","President":"Bush","Democrats":"26","South":"35","Republicans":"65"},
{"Body":"House","year":"1991","President":"Bush","Democrats":"35","South":"43","Republicans":"74"},
{"Body":"House","year":"1992","President":"Bush","Democrats":"27","South":"38","Republicans":"75"},
{"Body":"House","year":"1993","President":"Clinton","Democrats":"80","South":"81","Republicans":"39"},
{"Body":"House","year":"1994","President":"Clinton","Democrats":"78","South":"68","Republicans":"49"},
{"Body":"House","year":"1995","President":"Clinton","Democrats":"75","South":"69","Republicans":"22"},
{"Body":"House","year":"1996","President":"Clinton","Democrats":"74","South":"70","Republicans":"38"},
{"Body":"House","year":"1997","President":"Clinton","Democrats":"73","South":"68","Republicans":"31"},
{"Body":"House","year":"1998","President":"Clinton","Democrats":"78","South":"72","Republicans":"27"},
{"Body":"House","year":"1999","President":"Clinton","Democrats":"75","South":"70","Republicans":"24"},
{"Body":"House","year":"2000","President":"Clinton","Democrats":"76","South":"68","Republicans":"28"},
{"Body":"House","year":"2001","President":"Bush","Democrats":"32","South":"38","Republicans":"92"},
{"Body":"House","year":"2002","President":"Bush","Democrats":"33","South":"40","Republicans":"85"},
{"Body":"House","year":"2003","President":"Bush","Democrats":"27","South":"36","Republicans":"92"},
{"Body":"House","year":"2004","President":"Bush","Democrats":"31","South":"42","Republicans":"83"},
{"Body":"House","year":"2005","President":"Bush","Democrats":"24","South":"33","Republicans":"81"},
{"Body":"House","year":"2006","President":"Bush","Democrats":"31","South":"45","Republicans":"85"},
{"Body":"House","year":"2007","President":"Bush","Democrats":"7","South":"0","Republicans":"72"},
{"Body":"House","year":"2008","President":"Bush","Democrats":"16","South":"0","Republicans":"64"},
{"Body":"House","year":"2009","President":"Obama","Democrats":"92","South":"0","Republicans":"27"},
{"Body":"House","year":"2010","President":"Obama","Democrats":"87","South":"0","Republicans":"30"},
{"Body":"House","year":"2011","President":"Obama","Democrats":"80","South":"0","Republicans":"22"},
{"Body":"House","year":"2012","President":"Obama","Democrats":"77","South":"0","Republicans":"17"},
{"Body":"Senate","year":"1954","President":"Eisenhower","Democrats":"45","South":"0","Republicans":"82"},
{"Body":"Senate","year":"1955","President":"Eisenhower","Democrats":"65","South":"0","Republicans":"85"},
{"Body":"Senate","year":"1956","President":"Eisenhower","Democrats":"44","South":"0","Republicans":"80"},
{"Body":"Senate","year":"1957","President":"Eisenhower","Democrats":"60","South":"0","Republicans":"80"},
{"Body":"Senate","year":"1958","President":"Eisenhower","Democrats":"51","South":"0","Republicans":"77"},
{"Body":"Senate","year":"1959","President":"Eisenhower","Democrats":"44","South":"0","Republicans":"80"},
{"Body":"Senate","year":"1960","President":"Eisenhower","Democrats":"52","South":"0","Republicans":"76"},
{"Body":"Senate","year":"1961","President":"Kennedy","Democrats":"73","South":"0","Republicans":"42"},
{"Body":"Senate","year":"1962","President":"Kennedy","Democrats":"76","South":"63","Republicans":"48"},
{"Body":"Senate","year":"1963","President":"Kennedy","Democrats":"77","South":"65","Republicans":"52"},
{"Body":"Senate","year":"1964","President":"Johnson","Democrats":"73","South":"63","Republicans":"52"},
{"Body":"Senate","year":"1965","President":"Johnson","Democrats":"75","South":"60","Republicans":"55"},
{"Body":"Senate","year":"1966","President":"Johnson","Democrats":"71","South":"59","Republicans":"53"},
{"Body":"Senate","year":"1967","President":"Johnson","Democrats":"73","South":"69","Republicans":"63"},
{"Body":"Senate","year":"1968","President":"Johnson","Democrats":"64","South":"50","Republicans":"57"},
{"Body":"Senate","year":"1969","President":"Nixon","Democrats":"55","South":"56","Republicans":"74"},
{"Body":"Senate","year":"1970","President":"Nixon","Democrats":"56","South":"62","Republicans":"74"},
{"Body":"Senate","year":"1971","President":"Nixon","Democrats":"48","South":"59","Republicans":"76"},
{"Body":"Senate","year":"1972","President":"Nixon","Democrats":"52","South":"71","Republicans":"77"},
{"Body":"Senate","year":"1973","President":"Nixon","Democrats":"42","South":"55","Republicans":"70"},
{"Body":"Senate","year":"1974","President":"Nixon","Democrats":"44","South":"60","Republicans":"65"},
{"Body":"Senate","year":"1974","President":"Ford","Democrats":"45","South":"55","Republicans":"67"},
{"Body":"Senate","year":"1975","President":"Ford","Democrats":"53","South":"67","Republicans":"76"},
{"Body":"Senate","year":"1976","President":"Ford","Democrats":"47","South":"61","Republicans":"73"},
{"Body":"Senate","year":"1977","President":"Carter","Democrats":"77","South":"71","Republicans":"58"},
{"Body":"Senate","year":"1978","President":"Carter","Democrats":"74","South":"61","Republicans":"47"},
{"Body":"Senate","year":"1979","President":"Carter","Democrats":"75","South":"66","Republicans":"51"},
{"Body":"Senate","year":"1980","President":"Carter","Democrats":"71","South":"69","Republicans":"50"},
{"Body":"Senate","year":"1981","President":"Reagan","Democrats":"52","South":"63","Republicans":"84"},
{"Body":"Senate","year":"1982","President":"Reagan","Democrats":"46","South":"57","Republicans":"77"},
{"Body":"Senate","year":"1983","President":"Reagan","Democrats":"45","South":"46","Republicans":"77"},
{"Body":"Senate","year":"1984","President":"Reagan","Democrats":"45","South":"58","Republicans":"81"},
{"Body":"Senate","year":"1985","President":"Reagan","Democrats":"36","South":"46","Republicans":"80"},
{"Body":"Senate","year":"1986","President":"Reagan","Democrats":"39","South":"56","Republicans":"90"},
{"Body":"Senate","year":"1987","President":"Reagan","Democrats":"38","South":"42","Republicans":"67"},
{"Body":"Senate","year":"1988","President":"Reagan","Democrats":"51","South":"58","Republicans":"73"},
{"Body":"Senate","year":"1989","President":"Bush","Democrats":"56","South":"66","Republicans":"84"},
{"Body":"Senate","year":"1990","President":"Bush","Democrats":"39","South":"49","Republicans":"72"},
{"Body":"Senate","year":"1991","President":"Bush","Democrats":"42","South":"53","Republicans":"83"},
{"Body":"Senate","year":"1992","President":"Bush","Democrats":"33","South":"41","Republicans":"75"},
{"Body":"Senate","year":"1993","President":"Clinton","Democrats":"87","South":"84","Republicans":"30"},
{"Body":"Senate","year":"1994","President":"Clinton","Democrats":"88","South":"88","Republicans":"44"},
{"Body":"Senate","year":"1995","President":"Clinton","Democrats":"81","South":"78","Republicans":"29"},
{"Body":"Senate","year":"1996","President":"Clinton","Democrats":"83","South":"75","Republicans":"37"},
{"Body":"Senate","year":"1997","President":"Clinton","Democrats":"87","South":"84","Republicans":"61"},
{"Body":"Senate","year":"1998","President":"Clinton","Democrats":"86","South":"84","Republicans":"42"},
{"Body":"Senate","year":"1999","President":"Clinton","Democrats":"86","South":"84","Republicans":"35"},
{"Body":"Senate","year":"2000","President":"Clinton","Democrats":"92","South":"88","Republicans":"47"},
{"Body":"Senate","year":"2001","President":"Bush","Democrats":"67","South":"71","Republicans":"96"},
{"Body":"Senate","year":"2002","President":"Bush","Democrats":"73","South":"81","Republicans":"95"},
{"Body":"Senate","year":"2003","President":"Bush","Democrats":"52","South":"60","Republicans":"96"},
{"Body":"Senate","year":"2004","President":"Bush","Democrats":"63","South":"70","Republicans":"93"},
{"Body":"Senate","year":"2005","President":"Bush","Democrats":"38","South":"54","Republicans":"86"},
{"Body":"Senate","year":"2006","President":"Bush","Democrats":"51","South":"62","Republicans":"85"},
{"Body":"Senate","year":"2007","President":"Bush","Democrats":"37","South":"0","Republicans":"78"},
{"Body":"Senate","year":"2008","President":"Bush","Democrats":"34","South":"0","Republicans":"70"},
{"Body":"Senate","year":"2009","President":"Obama","Democrats":"96","South":"0","Republicans":"51"},
{"Body":"Senate","year":"2010","President":"Obama","Democrats":"97","South":"0","Republicans":"43"},
{"Body":"Senate","year":"2011","President":"Obama","Democrats":"92","South":"0","Republicans":"53"},
{"Body":"Senate","year":"2012","President":"Obama","Democrats":"93","South":"0","Republicans":"47"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment