javascript - How do I make my D3 collapsible chart, collapsible? -


i've created hierarchical chart using d3 , want make collapsible. i'm prety new d3 i'm getting used understanding code. need width between nodes increase don't know how this. text overlaps eachother why need expand width between each node. can too?

d3 code:

var margin = {top: 40, right: 120, bottom: 20, left: 120},   width = 960 - margin.right - margin.left,   height = 500 - margin.top - margin.bottom;  var = 0;  var tree = d3.layout.tree()   .size([height, width]);  var diagonal = d3.svg.diagonal()   .projection(function(d) { return [d.x, d.y]; });  var svg = d3.select("body").append("svg")   .attr("width", width + margin.right + margin.left)   .attr("height", height + margin.top + margin.bottom)   .append("g")   .attr("transform", "translate(" + margin.left + "," + margin.top + ")");    root = treedata[0];    update(root);    function update(source) {      // compute new tree layout.     var nodes = tree.nodes(root).reverse(),         links = tree.links(nodes);      // length of link     nodes.foreach(function(d) { d.y = d.depth * 100; });      // declare nodes…     var node = svg.selectall("g.node")       .data(nodes, function(d) { return d.id || (d.id = ++i); });      // enter nodes.     var nodeenter = node.enter().append("g")       .attr("class", "node")       .attr("transform", function(d) {          return "translate(" + d.x + "," + d.y + ")";        });      nodeenter.append("circle")       .attr("r", 8)       .style("fill", "#fff");      nodeenter.append("text")       .attr("y", function(d) {         return d.children || d._children ? -18 : 18;        })       .attr("dy", ".35em")       .attr("text-anchor", "middle")       .text(function(d) { return d.name; })       .style("fill-opacity", 1);      // declare links…     var link = svg.selectall("path.link")       .data(links, function(d) { return d.target.id; });      // enter links.     link.enter().insert("path", "g")       .attr("class", "link")       .attr("d", diagonal);    } 

perhaps mike bostock's work on collapsible tree http://bl.ocks.org/mbostock/4339083

you can observe adding collapse function, , attaching mouse events nodes enables nodes collapsible.

you want add codes involving nodeexit , nodeupdate.

to fix problem of texts overlapping 1 - observe code part append text nodeenter.

nodeenter.append("text")   .attr("y", function(d) {     return d.children || d._children ? -18 : 18;    })   .attr("dy", ".35em")   .attr("text-anchor", "middle")   .text(function(d) { return d.name; })   .style("fill-opacity", 1); 

play around numbers -18 : 18. increasing numbers may texts not overlap 1 another, numbers adjust distance of texts , nodes. may want play around "text-anchor". try instead:

nodeenter.append("text")     .attr("x", function(d) {             return d.children || d._children ? -10 : 10;     })     .attr("dy", ".35em")     .attr('class', 'nodetext')     .attr("text-anchor", function(d) {         return d.children || d._children ? "end" : "start";     })     .text(function(d) {         return d.name;     })     .style("fill-opacity", 0); 

lastly, if want understand d3 tree fully, link phenomenal source http://www.d3noob.org/2014/01/tree-diagrams-in-d3js_11.html


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -