A variation of the bar chart with negative values example using a custom axis to flip the tick orientation for the negative bars.
forked from mbostock's block: Bar Chart with Negative Values II
license: gpl-3.0 |
A variation of the bar chart with negative values example using a custom axis to flip the tick orientation for the negative bars.
forked from mbostock's block: Bar Chart with Negative Values II
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.bar--positive { | |
fill: steelblue; | |
} | |
.bar--negative { | |
fill: brown; | |
} | |
.axis text { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
</style> | |
<body> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 20, right: 30, bottom: 40, left: 30}, | |
width = 960 - margin.left - margin.right, | |
height = 1900 - margin.top - margin.bottom; | |
var x = d3.scale.linear() | |
.range([0, width]); | |
var y = d3.scale.ordinal() | |
.rangeRoundBands([0, height], 0.1); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.tickSize(6, 0); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.csv("marginsfl.csv", type, function(error, data) { | |
x.domain(d3.extent(data, function(d) { return d.marginClinton; })).nice(); | |
y.domain(data.map(function(d) { return d.district; })); | |
svg.selectAll(".bar") | |
.data(data) | |
.enter().append("rect") | |
.attr("class", function(d) { return "bar bar--" + (d.marginClinton < 0 ? "negative" : "positive"); }) | |
.attr("x", function(d) { return x(Math.min(0, d.marginClinton)); }) | |
.attr("y", function(d) { return y(d.district); }) | |
.attr("width", function(d) { return Math.abs(x(d.marginClinton) - x(0)); }) | |
.attr("height", y.rangeBand()); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
var tickNegative = svg.append("g") | |
.attr("class", "y axis") | |
.attr("transform", "translate(" + x(0) + ",0)") | |
.call(yAxis) | |
.selectAll(".tick") | |
.filter(function(d, i) { return data[i].marginClinton < 0; }); | |
tickNegative.select("line") | |
.attr("x2", 6); | |
tickNegative.select("text") | |
.attr("x", 9) | |
.style("text-anchor", "start"); | |
}); | |
function type(d) { | |
d.marginClinton = +d.marginClinton; | |
return d; | |
} | |
</script> |
district | marginClinton | |
---|---|---|
FL HD 3 | -56.39 | |
FL HD 5 | -54.5 | |
FL HD 10 | -48.68 | |
FL HD 19 | -45.75 | |
FL HD 6 | -44.82 | |
FL HD 4 | -44.78 | |
FL HD 18 | -41.9 | |
FL HD 34 | -39.58 | |
FL HD 33 | -39.3 | |
FL HD 11 | -38.77 | |
FL HD 7 | -38.27 | |
FL HD 55 | -34.53 | |
FL HD 17 | -33.36 | |
FL HD 23 | -33.36 | |
FL HD 106 | -29.87 | |
FL HD 1 | -28.36 | |
FL HD 35 | -27.95 | |
FL HD 75 | -27.78 | |
FL HD 22 | -27.19 | |
FL HD 37 | -26.98 | |
FL HD 76 | -26.65 | |
FL HD 56 | -26.33 | |
FL HD 73 | -25.32 | |
FL HD 24 | -25.21 | |
FL HD 77 | -24.8 | |
FL HD 80 | -23.53 | |
FL HD 82 | -23.3 | |
FL HD 54 | -23.1 | |
FL HD 74 | -23.1 | |
FL HD 52 | -22.56 | |
FL HD 31 | -22.38 | |
FL HD 25 | -22.24 | |
FL HD 51 | -21.21 | |
FL HD 36 | -20.2 | |
FL HD 39 | -19.03 | |
FL HD 16 | -18.93 | |
FL HD 79 | -17.12 | |
FL HD 38 | -16.42 | |
FL HD 12 | -15.99 | |
FL HD 2 | -15.68 | |
FL HD 32 | -15.43 | |
FL HD 66 | -14.22 | |
FL HD 65 | -13.9 | |
FL HD 40 | -13.14 | |
FL HD 57 | -12.36 | |
FL HD 27 | -11.53 | |
FL HD 71 | -11.17 | |
FL HD 53 | -10.92 | |
FL HD 78 | -10.89 | |
FL HD 58 | -10.14 | |
FL HD 83 | -9.72 | |
FL HD 64 | -9.29 | |
FL HD 15 | -8.7 | |
FL HD 85 | -7.96 | |
FL HD 41 | -5.48 | |
FL HD 72 | -4.68 | |
FL HD 67 | -4.19 | |
FL HD 29 | -4.19 | |
FL HD 50 | -3.69 | |
FL HD 28 | -3.57 | |
FL HD 42 | -3.16 | |
FL HD 69 | -2.96 | |
FL HD 84 | -1.63 | |
FL HD 60 | -1.33 | |
FL HD 93 | -1.11 | |
FL HD 59 | -0.41 | |
FL HD 21 | 0.05 | |
FL HD 89 | 0.52 | |
FL HD 26 | 1.51 | |
FL HD 120 | 1.7 | |
FL HD 116 | 4.48 | |
FL HD 44 | 6.16 | |
FL HD 111 | 6.5 | |
FL HD 68 | 6.83 | |
FL HD 110 | 7.57 | |
FL HD 30 | 8.32 | |
FL HD 9 | 10.07 | |
FL HD 63 | 10.31 | |
FL HD 115 | 10.71 | |
FL HD 118 | 12.33 | |
FL HD 47 | 12.45 | |
FL HD 119 | 12.92 | |
FL HD 114 | 14.16 | |
FL HD 105 | 14.94 | |
FL HD 86 | 15.16 | |
FL HD 100 | 18.29 | |
FL HD 103 | 19.49 | |
FL HD 91 | 19.94 | |
FL HD 81 | 19.94 | |
FL HD 90 | 21.18 | |
FL HD 99 | 21.4 | |
FL HD 96 | 23.28 | |
FL HD 98 | 24.44 | |
FL HD 112 | 25.92 | |
FL HD 104 | 26.93 | |
FL HD 49 | 27.13 | |
FL HD 62 | 29.74 | |
FL HD 20 | 30.8 | |
FL HD 13 | 31.05 | |
FL HD 14 | 31.75 | |
FL HD 97 | 32.86 | |
FL HD 87 | 33.06 | |
FL HD 113 | 37.16 | |
FL HD 45 | 38.43 | |
FL HD 92 | 44.83 | |
FL HD 48 | 46.66 | |
FL HD 43 | 49.1 | |
FL HD 70 | 50.25 | |
FL HD 8 | 51.97 | |
FL HD 101 | 55.27 | |
FL HD 88 | 58.52 | |
FL HD 117 | 58.79 | |
FL HD 61 | 63.21 | |
FL HD 94 | 63.8 | |
FL HD 46 | 68.01 | |
FL HD 102 | 68.13 | |
FL HD 107 | 69.02 | |
FL HD 95 | 71.63 | |
FL HD 109 | 74.24 | |
FL HD 108 | 75.88 |