d3.js - D3 How to filter menu based on nested data -
i have following plunk https://plnkr.co/edit/gdbue99nnnmtyp9ipr1d?p=info , struggling dropdown menu working nested data. ive generated menu first level nest ('starting point'), shows list of nationalities generated second level nest ('nat'). want use menu load different 'nat' assosicated each 'starting point'.
previously created menu this, doesn't seem working 'nested_data'. d3.filter() wrong this?
list.on('change', function() { var selected = d3.select(this).select("select").property("value") var cd = nested_data.filter(function(d) { return (selected == d.key) }); updatenested_data(cd) }); ... var filter = nested_data.filter(function(d) { return ("berkner island" == d.key) }); updatenested_data(filter) i'm having problem exit() in update function, think because i'm binding data early? question i'd focus on how menu working.
full code
d3.csv("data.csv", function(csv) { var nested_data = d3.nest() .key(function(d) { return d['starting point']; }) .key(function(d) { return d.nat; }) .entries(csv); // create dropdown var list = d3.select("#opts") list.append("select").selectall("option") .data(nested_data) .enter().append("option") .attr("value", function(d) { return d.key; }) .text(function(d) { return d.key; }); // menu list.on('change', function() { var selected = d3.select(this).select("select").property("value") var cd = nested_data.filter(function(d) { return (selected == d.key) }); updatenested_data(cd) }); // config var canvas = d3.select('#explorers').data(nested_data) // bind, enter, remove function updatenested_data(nested_data) { var country = canvas.selectall(".country") var countryenter = country .data(function(d) { return d.values }) .enter().append('div') .attr('class', 'country') countryenter .append("p") .attr('class', 'label') .style('font-weight', 'bold') .text(function(d) { return d.key; }) country.exit().remove(); }; //filter var filter = nested_data.filter(function(d) { return ("berkner island" == d.key) }); // update updatenested_data(filter) });
there no problem anywhere except way entering , exiting:
function updatenested_data(cd) { //set data selection var country = canvas.selectall(".country").data(cd.values) var countryenter = country .enter().append('div') .attr('class', 'country') countryenter .append("p") .attr('class', 'label') .style('font-weight', 'bold') .text(function(d) { return d.key; }) //exit on selection country.exit().remove(); }; working code here
Comments
Post a Comment