|
<!DOCTYPE html> |
|
<style> |
|
|
|
.axis .domain { |
|
display: none; |
|
} |
|
|
|
</style> |
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
|
|
<body> |
|
<button onclick="render('WestCoast')">West Coast</button> |
|
<button onclick="render(1)">The Rest</button><br> |
|
<svg width="700" height="400"></svg> |
|
|
|
<script> |
|
|
|
function render(region) { |
|
|
|
var svg = d3.selectAll("svg"), |
|
margin = { top: 20, right: 20, bottom: 30, left: 40 }, |
|
width = +svg.attr("width") - margin.left - margin.right, |
|
height = +svg.attr("height") - margin.top - margin.bottom, |
|
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); |
|
|
|
var removeAxis = d3.selectAll("g.axis").remove() |
|
|
|
var removeBars = d3.selectAll("rect.bars").remove() |
|
|
|
var removeMoreBard = d3.selectAll("g.bars").remove() |
|
|
|
var removeLegend = d3.selectAll("g.legend").remove() |
|
|
|
|
|
var x = d3.scaleBand() |
|
.rangeRound([0, width]) |
|
.paddingInner(0.05) |
|
.align(0.1); |
|
|
|
var y = d3.scaleLinear() |
|
.rangeRound([height, 0]); |
|
|
|
var z = d3.scaleOrdinal() |
|
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); |
|
|
|
d3.csv("data.csv", function (d, i, columns) { |
|
for (i = 3, t = 0; i < 6; ++i) t += d[columns[i]] = +d[columns[i]]; |
|
d.total = t; |
|
return d; |
|
}, function (error, data) { |
|
if (error) throw error; |
|
|
|
var keys = data.columns.slice(3, 6); |
|
|
|
data = data.filter(function (d) { return d.Region == region }) |
|
|
|
data.sort(function (a, b) { return b.total - a.total; }); |
|
x.domain(data.map(function (d) { return d.State; })); |
|
y.domain([0, d3.max(data, function (d) { return d.total; })]).nice(); |
|
z.domain(keys); |
|
|
|
var bars = g.append("g") |
|
.selectAll("g").exit().remove() |
|
.data(d3.stack().keys(keys)(data)) |
|
.enter().append("g") |
|
.attr("class","bars") |
|
.attr("fill", function (d) { return z(d.key); }) |
|
.selectAll("rect") |
|
.data(function (d) { return d; }) |
|
|
|
|
|
var newBars = bars.enter() |
|
.append("rect") |
|
.attr("height", 0) |
|
.attr("width", 0) |
|
.attr("class","bars") |
|
|
|
newBars.merge(bars) |
|
.transition() |
|
.attr("x", function (d) { return x(d.data.State); }) |
|
.attr("y", function (d) { return y(d[1]); }) |
|
.attr("height", function (d) { return y(d[0]) - y(d[1]); }) |
|
.attr("width", x.bandwidth()) |
|
.attr("class","bars"); |
|
|
|
|
|
var x_axis = |
|
g.append("g") |
|
.attr("class", "axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(x)); |
|
|
|
var y_axis = g.append("g") |
|
.attr("class", "axis") |
|
.call(d3.axisLeft(y).ticks(null, "s")) |
|
.append("text") |
|
.attr("x", 2) |
|
.attr("y", y(y.ticks().pop()) + 0.5) |
|
.attr("dy", "0.32em") |
|
.attr("fill", "#000") |
|
.attr("font-weight", "bold") |
|
.attr("text-anchor", "start") |
|
.text("Potential"); |
|
|
|
|
|
var legend = g.append("g") |
|
.attr("class", "legend") |
|
.attr("font-family", "sans-serif") |
|
.attr("font-size", 10) |
|
.attr("text-anchor", "end") |
|
.selectAll("g") |
|
.data(keys.slice().reverse()) |
|
.enter().append("g") |
|
.attr("transform", function (d, i) { return "translate(0," + i * 20 + ")"; }); |
|
|
|
legend.append("rect") |
|
.attr("x", width - 19) |
|
.attr("width", 19) |
|
.attr("height", 19) |
|
.attr("fill", z); |
|
|
|
legend.append("text") |
|
.attr("x", width - 24) |
|
.attr("y", 9.5) |
|
.attr("dy", "0.32em") |
|
.text(function (d) { return d; }); |
|
}); |
|
} |
|
render('WestCoast') |
|
|
|
</script> |
|
|
|
</body> |