javascript - d3 - unable to add select/option drop down to svg-g element -
i intending add circle pack, radio , drop down menu svg , toggle between these (on same svg).
i wasn't able add select/option dropdown menu svg created width/height of 600/600. in element console, able see g
-element , drop down select option. on screen, g#menu
has 0x0 size? why so? pointers/inputs?
note: here trying add dropdown menu dynamically using d3.js instead of hardcoding selectio/option or div in body element. enable toggling between different views.
var svg1 = d3.select("body") .append("svg") .attr("width", 600) .attr("height", 600) .append("g") .attr("transform", "translate(0,0)")); var buttonnames = ["button 1", "button 2", "button 3", "button 4"]; var menug = svg1.append("g") .attr("id","menug") .attr("transform","translate(60,60)"); var menu = d3.select("#menug") .append("select") .attr("id", "menu"); menu.selectall("option") .data(buttonnames) .enter().append("option") .text(function(d){return d;});
try out
var buttonnames = ["button 1", "button 2", "button 3", "button 4"]; var l=4; for(i=0;i<buttonnames.length;i++){if(l<buttonnames[i].length)l=buttonnames[i].length}; var width = 400, height = 300; l=l*10; var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("class","dropdown"); select = svg.append("g") .attr("class","select"); select.append("rect") .attr("x", 10) .attr("y", 10 ) .attr("width", l) .attr("height", 30); select.append("text") .attr("x", 15) .attr("y",30 ) .attr("id","mydropdown") .text( buttonnames[0]); var options = svg.selectall(".mybars") .data(buttonnames) .enter() .append("g"); options.attr("class", "option").on("click", function() { document.getelementbyid("mydropdown").innerhtml=this.getelementsbytagname("text")[0].innerhtml; d3.event.stoppropagation(); }); options.append("rect").attr("x", 10) .attr("y", function(d,i){ return 40 + i*30}) .attr("width", l) .attr("height", 30); options.append("text").attr("x", function(d){ return 15}) .attr("y", function(d,i){ return 60 + i*30}) .text(function(d){ return d});
rect { fill: teal; } .dropdown .option { display:none; } .dropdown rect{ stroke-width:0.5; stroke:rgb(0,0,0) } .dropdown:hover .option { display:unset; } .dropdown { cursor:pointer; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
Comments
Post a Comment