Created
March 10, 2017 01:03
-
-
Save huembelin/3d8f37a9fec14c4f7df18d25dd73599a to your computer and use it in GitHub Desktop.
Waterfall - 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name | value | |
---|---|---|
gini pre-tax | 0.413 | |
federal income tax | -0.008 | |
cantonal income tax | -0.006 | |
cantonal wealth tax | -0.001 | |
com income tax | -0.006 | |
com wealth tax | -0.001 | |
church tax | -0.001 | |
social | 0.002 | |
work related | 0.001 | |
real etatet | 0.014 | |
financial | 0.003 | |
transfers | 0.001 | |
others | 0.00001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.bar.total rect { | |
fill: steelblue; | |
} | |
.bar.positive rect { | |
fill: darkolivegreen; | |
} | |
.bar.negative rect { | |
fill: crimson; | |
} | |
.bar line.connector { | |
stroke: grey; | |
stroke-dasharray: 3; | |
} | |
.bar text { | |
fill: black; | |
font: 12px sans-serif; | |
text-anchor: middle; | |
} | |
.axis text { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<h1> Income inequality impact of taxes and deducations </h1> | |
<svg class="chart"></svg> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 30, bottom: 70, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom, | |
padding = 0.3; | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], padding); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
var chart = d3.select(".chart") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
// clip-path | |
chart.append("clipPath") | |
.attr("id","clip") | |
.append("rect") | |
.attr("width",width) | |
.attr("height",height); | |
var formatNumber=d3.format(".2n") | |
d3.csv("giniData3.csv", type, function(error, data) { | |
console.log(data) | |
// Transform data (i.e., finding cumulative values and total) for easier charting | |
var cumulative = 0; | |
for (var i = 0; i < data.length; i++) { | |
data[i].start = cumulative; | |
cumulative += data[i].value; | |
data[i].end = cumulative; | |
data[i].class = ( data[i].value >= 0 ) ? 'positive' : 'negative' | |
} | |
data.push({ | |
name: 'Gini post-tax', | |
end: cumulative, | |
start: 0, | |
class: 'total' | |
}); | |
x.domain(data.map(function(d) { return d.name; })); | |
y.domain([0.35, 0.45]); | |
chart.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis) | |
.selectAll(".tick text") | |
.call(wrap, x.rangeBand()); | |
chart.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
var bar = chart.selectAll(".bar") | |
.data(data) | |
.enter().append("g") | |
.attr("class", function(d) { return "bar " + d.class }) | |
.attr("transform", function(d) { return "translate(" + x(d.name) + ",0)"; }); | |
bar.append("rect") | |
.attr("y", function(d) { return y( Math.max(d.start, d.end) ); }) | |
.attr("height", function(d) { return Math.abs( y(d.start) - y(d.end) ); }) | |
.attr("width", x.rangeBand()) | |
.attr("clip-path", "url(#clip)") // clip the rectangle | |
bar.append("text") | |
.attr("x", x.rangeBand() / 2) | |
.attr("y", function(d) { return y(d.end) + 5; }) | |
.attr("dy", function(d) { return ((d.class=='negative') ? '-' : '') + "1.5em" }) | |
.text(function(d) { return formatNumber(d.end - d.start);}); | |
bar.filter(function(d) { return d.class != "total" }).append("line") | |
.attr("class", "connector") | |
.attr("x1", x.rangeBand() + 5 ) | |
.attr("y1", function(d) { return y(d.end) } ) | |
.attr("x2", x.rangeBand() / ( 1 - padding) - 5 ) | |
.attr("y2", function(d) { return y(d.end) } ) | |
chart.append("text") | |
.attr("class","ylegend") | |
.attr("x",0) | |
.attr("y",15) | |
.style("text-anchor", "end") | |
.style("font-size",13) | |
.attr("transform", "rotate(-90)") | |
.text("Gini-Coefficient"); | |
}); | |
function wrap(text, width) { | |
text.each(function() { | |
var text = d3.select(this), | |
words = text.text().split(/\s+/).reverse(), | |
word, | |
line = [], | |
lineNumber = 0, | |
lineHeight = 1.1, // ems | |
y = text.attr("y"), | |
dy = parseFloat(text.attr("dy")), | |
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em"); | |
while (word = words.pop()) { | |
line.push(word); | |
tspan.text(line.join(" ")); | |
if (tspan.node().getComputedTextLength() > width) { | |
line.pop(); | |
tspan.text(line.join(" ")); | |
line = [word]; | |
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word); | |
} | |
} | |
}); | |
} | |
function type(d) { | |
d.value = +d.value; | |
return d; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment