|
<!DOCTYPE html> |
|
<style> |
|
|
|
.axis .domain { |
|
display: none; |
|
} |
|
|
|
h2 { |
|
margin: 0.83em 0 0 0; |
|
} |
|
|
|
</style> |
|
<h1>Change of Water Levels over the Weekend</h1> |
|
<h2>North and West</h2> |
|
<svg id="zona1" width="960" height="500"></svg> |
|
|
|
<h2>Downtown Northwest</h2> |
|
<svg id="zona2" width="960" height="500"></svg> |
|
|
|
<h2>Northeast</h2> |
|
<svg id="zona3" width="960" height="500"></svg> |
|
|
|
<h2>Downtown South</h2> |
|
<svg id="zona4" width="960" height="500"></svg> |
|
|
|
<h2>South</h2> |
|
<svg id="zona5" width="960" height="500"></svg> |
|
|
|
<script src="https://d3js.org/d3.v4.min.js"></script> |
|
<script> |
|
|
|
function graficar(svgzona, csvdatos, csvwwd) { |
|
var svg = d3.select(svgzona), |
|
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 x = d3.scaleBand() |
|
.rangeRound([0, width]) |
|
.paddingInner(0.05) |
|
.align(0.1); |
|
|
|
var xn = 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"]); |
|
|
|
var color0 = "rgb(167,200,210)"; |
|
var color1 = "rgb(238,100,92)"; |
|
|
|
d3.csv(csvwwd, |
|
function(d) {return d;}, |
|
function(errorwwd,datawwd) { |
|
if (errorwwd) throw errorwwd; |
|
|
|
//console.log(datawwd[0]["NW755"]); |
|
|
|
d3.csv(csvdatos, function(d, i, columns) { |
|
for (i = 1, t = 0; i < columns.length; ++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(1); |
|
|
|
//No ordenar |
|
//data.sort(function(a, b) { return b.total - a.total; }); |
|
x.domain(data.map(function(d) { return d.time; })); |
|
|
|
xn.domain(data.map(function(d) { |
|
var cad = (d.time.substring(1, 3)) - 0; |
|
return cad; |
|
})); |
|
|
|
y.domain([0, d3.max(data, function(d) { return d.total; })]).nice(); |
|
z.domain(keys); |
|
|
|
g.append("g") |
|
.selectAll("g") |
|
.data(d3.stack().keys(keys)(data)) |
|
.enter() |
|
.selectAll("rect") |
|
.data(function(d) { |
|
|
|
for(var k = 0; k < data.length; k++) |
|
d[k].fila = d.key; |
|
|
|
//console.log(data.length); |
|
return d; |
|
}) |
|
.enter() |
|
.append("g") |
|
.attr("fill", function(d, i, c) { |
|
|
|
var sal = color0; |
|
|
|
//console.log(datawwd[0][d.fila]); |
|
//console.log(d[1] - d[0]); |
|
if ((d[1] - d[0]) >= datawwd[0][d.fila]) { |
|
sal = color1; |
|
} |
|
|
|
//PRUEBA |
|
/*return "rgb(" + |
|
Math.floor(Math.random() * 255) |
|
+ "," + Math.floor(Math.random() * 255) |
|
+ "," + Math.floor(Math.random() * 255) |
|
+ ")"; */ |
|
|
|
return sal; |
|
|
|
}) |
|
.append("rect") |
|
.attr("x", function(d) { |
|
|
|
//console.log(z(d.data.time)); |
|
//console.log(d[0] + " " + d[1]); |
|
//console.log(d); |
|
|
|
return x(d.data.time); }) |
|
.attr("y", function(d) { return y(d[1]); }) |
|
.attr("height", function(d) { return y(d[0]) - y(d[1]); }) |
|
.attr("width", x.bandwidth()); |
|
|
|
var formatDay = function(d) { |
|
var sal = ""; |
|
|
|
|
|
if ((d % 8) == 0) { |
|
var num = 25 + (d/8); |
|
sal = ((num <= 31) ? "Aug " + num : "Sept " + (num - 31)); |
|
} |
|
|
|
return sal; |
|
} |
|
|
|
g.append("g") |
|
.attr("class", "axis") |
|
.attr("transform", "translate(0," + height + ")") |
|
.call(d3.axisBottom(xn).tickFormat(formatDay)); |
|
|
|
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("Accumulated gauge level"); |
|
|
|
/*var legend = g.append("g") |
|
.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; });*/ |
|
});//data.csv |
|
|
|
});//dataWWD.csv |
|
|
|
var legend = svg.select("g") |
|
.append("g") |
|
.attr("font-family", "sans-serif") |
|
.attr("font-size", 10) |
|
.attr("text-anchor", "end"); |
|
; |
|
|
|
legend.append("rect") |
|
.attr("x", width - 19) |
|
.attr("y", 0) |
|
.attr("width", 19) |
|
.attr("height", 19) |
|
.attr("fill", color0); |
|
|
|
legend.append("rect") |
|
.attr("x", width - 19) |
|
.attr("y", 20) |
|
.attr("width", 19) |
|
.attr("height", 19) |
|
.attr("fill", color1); |
|
|
|
legend.append("text") |
|
.attr("x", width - 24) |
|
.attr("y", 9.5) |
|
.attr("dy", "0.32em") |
|
.text("Normal level"); |
|
|
|
legend.append("text") |
|
.attr("x", width - 24) |
|
.attr("y", 9.5 + 20) |
|
.attr("dy", "0.32em") |
|
.text("Flooded gauge"); |
|
}//function graficar |
|
|
|
graficar("#zona1", "data_NW.csv", "datawwd_NW.csv"); |
|
graficar("#zona2", "data_DN.csv", "datawwd_DN.csv"); |
|
graficar("#zona3", "data_NE.csv", "datawwd_NE.csv"); |
|
graficar("#zona4", "data_DS.csv", "datawwd_DS.csv"); |
|
graficar("#zona5", "data_S.csv", "datawwd_S.csv"); |
|
</script> |