var margin = {top: 20, right: 185, bottom: 130, left: 70}; var width = 1000 - margin.left - margin.right; var height = 550 - margin.top - margin.bottom; var xScale = d3.scale.ordinal() .rangeRoundBands([0, width], .3); var yScale = d3.scale.linear() .rangeRound([height, 0]); /*var color = d3.scale.category20();*/ var color = d3.scale.ordinal().range(["#8C703A", "#ABACDB", "#B574B5", "#DBABC4", "#DBBFAB", "#ACD7E3", "#E6E687", "#FA9D91", "#D6C789", "#72A886", "#8A5F76", "#92A147", "#E3E2AF", "#A5BBD1"]); var tooltip = d3.select("#graph1") .append("div") .attr("class", "tooltip"); var percentClicked = false; var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .innerTickSize([0]); var yAxis = d3.svg.axis() .scale(yScale) .orient("left"); /*.innerTickSize(d3.format(".1"))*/; var stack = d3.layout .stack(); var svg1 = d3.select("#graph1").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("diarrhoeal.csv", function(error, data) { if (error) { console.log(error); } my2013 = []; data.forEach(function (d) { if (d.year === "2013" && d.region === "Sub-Saharan Africa" /*|| d.region === "Middle East & North Africa" || d.region === "East Asia & Pacific"*/) { my2013.push(d); } }); console.log(my2013); data.sort(function(a,b) { return b.country - b.country; }); var illnesses = ["Diarrhoeal diseases","HIV","Pertussis","Measles","Meningitis and encephalitis","Malaria","Respiratory infections","Prematurity","Birth asphixia","Sepsis and infections","Perinatal and nutritional", "Congenital anomalies","Injuries","Other"]; var stacked = stack(makeData(illnesses, my2013)); console.log(stacked); xScale.domain(my2013.map(function(d) { return d.country; })); svg1.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .selectAll("text") .attr("dy", ".5em") .attr("transform", "rotate(-45)") .style("text-anchor", "end"); svg1.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("class", "y label") .attr("transform", "rotate(-90)") .attr("y", -60) .attr("dy", ".5em") .attr("dx", 5) .style("text-anchor", "end") .text("Mortality rate under 5"); var country = svg1.selectAll(".country") .data(stacked) .enter().append("g") .attr("class", "country") .style("fill", function(d,i) { return color(i); }); var rectangles = country.selectAll("rect") .data(function(d) { console.log("array for a rectangle", d); return d; }) .enter().append("rect") .attr("width", xScale.rangeBand()); transitionCount(); drawLegend(); d3.selectAll("input").on("change", handleFormClick); function handleFormClick() { if (this.value === "bypercent") { percentClicked = true; transitionPercent(); } else { percentClicked = false; transitionCount(); } } function makeData(illnesses, data) { return illnesses.map(function(illness) { return data.map(function(d) { return { x: d["country"], y: +d[illness], illness: illness }; }) }); } function transitionPercent () { yAxis.tickFormat(d3.format("%")); stack.offset("expand"); // use this to get it to be relative/normalized! var stacked = stack(makeData(illnesses, my2013)); transitionRects(stacked); } function transitionCount() { yAxis.tickFormat(d3.format("s")); // for the stacked totals version stack.offset("zero"); var stacked = stack(makeData(illnesses, my2013)); transitionRects(stacked); } function transitionRects(stacked) { yScale.domain([0, d3.max(stacked[stacked.length -1], function(d) { return d.y0 + d.y; })]); var country = svg1.selectAll(".country") .data(stacked); country.selectAll("rect") .data(function(d) { return d; }) svg1.selectAll("g.country rect") .transition() .duration(350) .attr("x", function(d) { return xScale(d.x); }) .attr("y", function(d) { return yScale(d.y0 + d.y); }) .attr("height", function(d) { return yScale(d.y0) - yScale(d.y0 + d.y); //height is base - tallness }); svg1.selectAll(".y.axis") .transition() .call(yAxis) .selectAll("text") .style("text-anchor", "end"); } function drawLegend() { var legend = svg1.selectAll(".legend") .data(color.domain().slice()) .enter().append("g") .attr("class", "legend") .attr("transform", function(d,i){ return "translate(0," + i * 20 + ")"; }); legend.append("rect") .attr("x", width + 15) .attr("width", 15) .attr("height", 15) .style("fill", color); legend.append("text") .attr("x", width + 44) .attr("y", 6) .attr("dy", ".5em") .style("text-anchor", "start") .text(function(d, i) { return illnesses[i]; }); } //tooltip and interaction rectangles .on("mouseover", mouseoverFunc) .on("mousemove", mousemoveFunc) .on("mouseout", mouseoutFunc); function mouseoverFunc(d) { console.log("moused over", d.x); if(percentClicked) { tooltip .style("display", null) .html("

" + d.x + "
"+ d.illness + ": " + d3.format(",%")(d.y) + "

"); } else { console.log("illness", d.illness, "percent", d.y); tooltip .style("display", null) .html("

" + d.x + "
"+ d.illness + ": " + (d.y) + "

"); } } function mousemoveFunc(d) { tooltip .style("top", (d3.event.pageY - 5) + "px") .style("left", (d3.event.pageX + 10) + "px"); } function mouseoutFunc(d) { return tooltip.style("display", "none"); // this sets it to invisible! } });