Skip to content

Instantly share code, notes, and snippets.

@erikhazzard
Created October 2, 2013 00:03
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 erikhazzard/6787127 to your computer and use it in GitHub Desktop.
Save erikhazzard/6787127 to your computer and use it in GitHub Desktop.
inflation index
{"description":"inflation index","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"raises.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/q2jDcGh.png"}
var raises = tributary.raises.map(function(d) {
return {
year: d.Year,
raise: +d['raise-index'],
union: +d['union-raise-index'],
inflation: +d['inflation-index'],
SF: +d['case-shiller-SF'],
comp: +d['case-shiller-comp-20'],
CA: +d['CA-wage-inflation']
}
})
var currentYear = 2013;
// --------------------------------------
// Viz Setup
// --------------------------------------
var margin = {top: 20, right: 20, bottom: 30, left: 80},
width = 750 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Setup scales
// --------------------------------------
var xScale = d3.scale.linear()
.range([0, width])
.domain([1997, 2017]);
var yScale = d3.scale.linear()
.range([height, 0]);
// extent will update based on data
var yExtent = [91,195];
var yExtentHousing = [91, 227];
yScale.domain(yExtent)
var color = d3.scale.category20c();
// Setup axes
// --------------------------------------
var xAxis = d3.svg.axis()
.scale(xScale)
.ticks(raises.length)
.tickFormat(function(d){ return d + ''; })
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(yScale)
.tickSize(-width - (margin.left + margin.right))
/*.tickFormat(function(d){
return d3.format('$,')(d);
})*/
.orient("left");
// Setup SVG
// --------------------------------------
var svg = d3.select('svg')
.attr({
width: width + margin.left + margin.right,
height: height + margin.top + margin.bottom
})
.append("g")
.attr({
transform: "translate(" + margin.left + "," + margin.top + ")"
});
// Setup groups
// --------------------------------------
// Draw initial axes
var axesGroup = svg.append('g').attr({ 'class': 'axes' });
var xAxisGroup = axesGroup.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
var yAxisGroup = axesGroup.append("g")
.attr("class", "y axis");
yAxisGroup.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
//.text("Income ($)");
// Income lines
var incomeLines = svg.append('svg:g').attr({ 'class': 'incomeLines' });
// Projected info
// --------------------------------------
// Draw a line representing projected info
svg.append('line')
.attr({
x1: xScale(currentYear),
x2: xScale(currentYear),
y1: 0,
y2: height
}).classed("current-year", true);
// --------------------------------------
// Income Paths
// --------------------------------------
var drawIndexLine = function(field, befaft){
var slice = 14; //this should look up current year
if(befaft === "before") {
var data = raises.slice(0, slice)
} else if(befaft === "after") {
var data = raises.slice(slice-1)
} else {
var data = raises;
}
// Udpate y scale
var extent = d3.extent(data, function(d) { return d[field]; });
//if(extent[0] < yExtent[0]){ yExtent[0] = extent[0]; }
//if(extent[1] > yExtent[1]){ yExtent[1] = extent[1]; }
//yScale.domain(yExtent);
// Setup line func
var line = d3.svg.line()
.x(function(d) { return xScale(d.year); })
.y(function(d) { return yScale(d[field]); })
.defined(function(d) { return d[field]; })
.interpolate("cardinal")
// Draw a new path for the income line
var incomeLine = incomeLines.append("path")
.datum(data)
.attr({
'class': "line inflation " + field + " " + befaft,
d: line
})
.style({
//stroke: color(Math.random() * 100 | 0) ,
opacity: 1
});
};
var updateIndexLines = function(){
incomeLines.selectAll("path").remove()
drawLines();
// Update y scale
yAxisGroup.transition().call(yAxis);
};
//sue me..
function drawLines() {
drawIndexLine("inflation", "before")
drawIndexLine("inflation", "after")
drawIndexLine("raise", "before")
drawIndexLine("raise", "after")
drawIndexLine("union", "after")
drawIndexLine("comp", "all")
drawIndexLine("SF", "all")
drawIndexLine("CA", "all")
}
var ldata = [
{name: "Union proposal", "class": "union after"},
{name: "BART proposal", "class": "raise after"},
{name: "Raises", "class": "raise before"},
{name: "Inflation", "class": "inflation before"},
{name: "Comp", "class": "comp all"},
{name: "SF Housing", "class": "SF all"}
]
var legendWidth = 29;
var legend = svg.append("g").classed("legend", true)
.attr("transform", "translate(" + [width - legendWidth, 0] + ")")
var legs = legend.selectAll("g")
.data(ldata)
.enter()
.append("g")
.attr("transform", function(d,i) { return "translate(" + [10, 22 + i * 24 ] + ")"})
legs.append("text")
.text(function(d) { return d.name })
.attr("class", function(d) { return d['class'] })
legs.append("line")
.attr({
x1: 10,
x2: 30,
y1: 0,
y2: 0
}).attr("class", function(d) { return d['class'] + " line" });
var housing = false;
function toggleHousing() {
housing = !housing;
if(housing) {
yScale.domain(yExtentHousing);
} else {
yScale.domain(yExtent);
}
updateIndexLines();
d3.selectAll(".all").classed("hidden", !housing);
}
toggleHousing();
//toggleHousing();
Year raise-index union-raise-index inflation-index case-shiller-SF case-shiller-comp-20 bls-transport-wage
2000 100 103.4 101.45 100.59 100
2001 106 106.3 133.04 113.05 103.5842294
2002 112.36 108 126.84 121.36 108.6021505
2003 119.1016 110.48 143.73 136.47 112.3058542
2004 126.247696 113.46 157.84 152.62 115.2927121
2005 128.4570307 117.32 191.6 177.54 117.8016726
2006 130.7050287 121.07 217.36 203.75 119.9522103
2007 132.9923667 124.46 214.56 203.69 122.8195938
2008 135.3197331 129.19 186.53 182.03 126.1648746
2009 135.3197331 129.14 126.49 147.54 128.3154122
2010 135.3197331 131.21 138.13 146.62 130.2270012
2011 135.3197331 135.41 136.01 142.14 134.4086022
2012 135.3197331 138.25 128.03 136.59 138.2317802
2013 135.3197331 135.3197331 140.42 147.45 146.14 142.7718041
2014 138.0261278 145.0176473 143.93
2015 140.7866504 155.4105787 147.53
2016 143.6023834 166.5483369 151.21
2017 146.474431 154.99
.line {
stroke-width: 3px;
}
.inflation {
stroke: #ff0000;
}
.union {
stroke: #029202;
}
.raise {
stroke: #0000ff;
}
.after {
stroke-dasharray: 5px 2px;
}
.comp {
stroke: #39A8BD;
stroke-width: 1px;
}
.SF {
stroke: #39BD88;
stroke-width: 1px;
}
.current-year {
stroke-dasharray: 10px 4px;
}
.y.axis path {
stroke: none;
}
path, line {
fill: none;
stroke: #000;
}
.legend text {
text-anchor: end;
font-size: 12px;
alignment-baseline: middle;
stroke: none;
}
.projected {
stroke-dasharray: 2 2;
}
.y.axis .tick line {
stroke: #d0d0d0;
opacity: 0.5;
}
.axes .x.axis text {
font-size: 10px;
}
.hidden {
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment