D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lmelgar
Full window
Github gist
Finished Bar Homework
<!DOCTYPE html> <!-- Modified example from enjalot: https://bl.ocks.org/enjalot/1429426 --> <html> <head> <title>Top Causes of Deaths Bar Chart</title> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script> <style type="text/css"> body { padding: 50px; font-family: sans-serif; font-size: 12pt; } button { margin: 5px; font-size: 15pt; padding: 3px; cursor: pointer; } input { margin: 5px; font-size: 15pt; padding: 3px; } p { width: 500px; padding-left: 50px; } #chart { padding-left: 75px; } #menu { padding: 20px 75px; } #menu select { background: #ccc; width: 220px; padding: 5px; font-size: 14px; line-height: 1; border: 0; border-radius: 0; height: 34px; } .labels { fill: white; font-size: 10px; } </style> </head> <body> <h2>Top 20 Contributors to Death of Under 4 Year Olds By Country</h2> <p>For each country, these causes contributed what percent of the total deaths of under four year olds? Sorted to show the top 20 countries per disease cost. (Need real source here.) <p>Blue bars were present in your previous selection. <p>(Code Based on <a href="https://bl.ocks.org/enjalot/1429426">an example from enjalot</a>.) <div id="menu"> <select> <option value="Asphyxia_trauma">Asphyxia</option> <option value="Diarrhoeal_diseases">Diarrhoea</option> <option value="Malaria">Malaria</option> <option value="Measles">Measles</option> <option value="Meningitis_encephalitis">Meningitis and encephalitis</option> <option value="Pertussis">Pertussis</option> <option value="Respiratory_infections">Respiratory infections</option> <option value="Sepsis">Sepsis</option> //TODO: fill in the others here. What do you use for the values? </select> </div> <div id="chart"> </div> <script type="text/javascript"> var width = 400; var height = 500; var format = d3.format(".1%"); //setup the svg var vis = d3.select("#chart").append("svg"); var svg = vis .attr("width", width+100) .attr("height", height+100); // adding some random padding svg.append("rect") .attr("width", "100%") .attr("height", "100%") .attr("fill", "none"); d3.csv("deaths_04yearsold_2013percent.csv", function(error, data) { var column = d3.select("#menu select").property("value"); var dataset = top20_by_column(data, column); // you need to finish this function below. console.log(column, dataset); redraw(dataset, column); //setup our ui -- requires access to data variable, so inside csv d3.select("#menu select") .on("change", function() { column = d3.select("#menu select").property("value"); //TODO: How do you get the current value of the select menu? dataset = top20_by_column(data, column); //TODO: How do you get the current filter/storted data? //console.log(column, dataset); redraw(dataset, column); }); }) // end csv //make the bars for the first data set. They will be red at first. function top20_by_column(data, column) { return data.sort(function(a, b) { return b[column] - a[column]; // descending order, biggest at the top }).slice(0, 20); // cut off the top 10!// TODO: fill in this function. The answer direction is in the wiki page for week8. // You want to sort the data by the column, descending order, and then slice. } // This function is used to draw and update the data. It takes different data each time. function redraw(data, column) { var max = d3.max(data, function(d) {return +d[column];}); xScale = d3.scale.linear() .domain([0, max]) .range([0, width]); yScale = d3.scale.ordinal() .domain(d3.range(data.length)) .rangeBands([0, height], .2); var bars = vis.selectAll("rect.bar") .data(data, function (d) { return d.Country; });//TODO: what is your key value?}); // key function! //update -- existing bars get blue when we "redraw". We don't change labels. bars .attr("fill", "steelblue"); //enter - new bars get set to darkorange when we "redraw." bars.enter() .append("rect") .attr("class", "bar") .attr("fill", "darkorange"); //exit -- remove ones that aren't in the index set bars.exit() .transition() .duration(300) .ease("exp") .attr("width", 0) .remove(); //TODO: what goes here at the end of exit? // transition -- move the bars to proper widths and location bars .transition() .duration(300) .ease("quad") .attr("width", function(d) { return xScale(+d[column]);//TODO: what goes here?); }) .attr("height", yScale.rangeBand())//TODO: In an ordinal scale bar chart, what goes here?) .attr("transform", function(d,i) { return "translate(" + [0, yScale(i)] + ")" }); // We are attaching the labels separately, not in a group with the bars... var labels = svg.selectAll("text.labels") .data(data, function (d) { return d.Country });//TODO: what is your key here? same as above.}); // key function! // everything gets a class and a text field. But we assign attributes in the transition. labels.enter() .append("text") .attr("class", "labels"); labels.exit() .remove(); labels.transition() .duration(1000) //TODO: How long do you want this to last?) .text(function(d) { return d.Country + " " + format(+d[column]); //TODO: what goes here?); }) .attr("transform", function(d,i) { return "translate(" + xScale(+d[column]) + "," + yScale(i) + ")" }) .attr("dy", "1.2em") .attr("dx", "-3px") .attr("text-anchor", "end"); } // end of draw function </script> </body> </html>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js