Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------ BEGIN NEW CODE HERE ------
- let univMenu = d3.select("#univDropdown");
- univMenu
- .append("select")
- .selectAll("option")
- .data(nest)
- .enter()
- .append("option")
- .attr("value", function(d){return d.key;})
- .text(function(d){return d.key;})
- // Function to create the initial graph
- let initialGraph = function(univ){
- let selectUniv = nest.filter(function(d){return d.key == univ;})
- let selectUnivGroups = svg.selectAll(".univGroups")
- .data(selectUniv, function(d){return d ? d.key : this.key;})
- .enter()
- .append("g")
- .attr("class", "univGroups")
- let initialPath = selectUnivGroups.selectAll(".line")
- .data(function(d) { return d.values; })
- .enter()
- .append("path")
- initialPath
- .attr("d", function(d){return valueLine(d.values)})
- .attr("class", "line")}
- // Create initial graph
- initialGraph("USHE Average")
- // Update the data
- let updateGraph = function(univ){
- // Filter the data to include only fruit of interest
- let selectUniv = nest.filter(function(d){return d.key == univ;})
- // Select all of the grouped elements and update the data
- let selectUnivGroups = svg.selectAll(".univGroups")
- .data(selectUniv)
- // Select all the lines and transition to new positions
- selectUnivGroups.selectAll("path.line")
- .data(function(d){return (d.values);})
- .transition()
- .duration(1000)
- .attr("d", function(d){return valueLine(d.values)})}
- // Run update function when dropdown selection changes
- univMenu.on('change', function(){
- let selectedUniv = d3.select(this)
- .select("select")
- .property("value")
- updateGraph(selectedUniv)
- });
Add Comment
Please, Sign In to add comment