score:46
So when you set the "size" of the tree layout all you are doing is setting the value the tree layout will interpolate the x and y values to. So there is no reason you can't compute the width or height you want and change it in the layout on each update call.
I copied the example you gave to a jsFiddle and added the following to the update function:
// compute the new height
var levelWidth = [1];
var childCount = function(level, n) {
if(n.children && n.children.length > 0) {
if(levelWidth.length <= level + 1) levelWidth.push(0);
levelWidth[level+1] += n.children.length;
n.children.forEach(function(d) {
childCount(level + 1, d);
});
}
};
childCount(0, root);
var newHeight = d3.max(levelWidth) * 20; // 20 pixels per line
tree = tree.size([newHeight, w]);
Note that this is a pretty rough calculation and does not factor in where the children are w.r.t their parents or anything - but you get the idea.
As for manipulating the x values of the nodes the easiest is probably to simply modify the nodes after the layout has done it's thing. In fact you can see that this is already done in the example with the y coordinate:
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
He does this so that even if children are hidden a given level of nodes stays at the same y position, otherwise if you collapsed all the children the remaining levels would stretch to take up the whole width (try commenting that line out and see what happens as you toggle whole levels invisible).
As for going top down I think you can pretty much just flip everywhere you see x and y (and x0 and y0). Don't forget to do the same to the diagonal projection to make sure your lines flip too.
Source: stackoverflow.com
Related Query
- Dynamically resize the d3 tree layout based on number of childnodes
- How can I dynamically resize an SVG rect based on text width?
- D3 Tree Layout - Custom Vertical Layout when children exceed more than a certain number
- How to append the arrow marker dynamically in force layout d3.js
- How to change the tree layout to v5?
- Select all the paths and parent nodes of a child node in tree layout
- Dynamically set the axisBottom number of ticks to the highest integer in an array in D3v4
- How to dynamically adjust the size of a D3 rect based on the foreignObject's height
- Open a d3 tree layout to show the node a user has searched for by typing request in a search box
- Dynamically changing the Javascript D3 layout simulation
- D3.js Tree layout canvas resize
- D3 v4 (Tree Diagram) - Dynamically calculate the height of the SVG given dynamic number of nodes
- d3.js tree layout is bunching everything up at the top
- D3 force layout with clustering to the top based on size
- Dynamically set the width and height of SVG based on data
- How to rearrange the DOM order of svg elements based on their data values in a dynamically changing d3 vis?
- access data of child node when clicked on the link in d3 tree layout
- Dynamically changing the number of ticks in D3
- Change the layout of a tree in D3JS
- How to specify height and width of a d3 tree dynamically to allow it to take up all the space it needs
- Add nodes dynamically to a D3 Tree layout request (on toggle)
- How to change the data in a force layout graph dynamically ? D3
- add nodes dynamically to leaf node in D3.js using Tree layout v4 (version 4)
- How to vary the size of piechart in d3 with tree layout
- D3 Collapsible Force Layout adding nodes to the layout dynamically
- Using d3js, how do I use the tree layout to visually place child nodes within their parent?
- Updating the coordinates in d3js for a tree layout
- Pre determined x and y values of the tree layout
- Resize SVG contents based on the result of rendering
- Placing label in the middle of a link in d3 js tree layout
More Query from same tag
- D3 filtering data points
- How to change the names of provinces on the map?
- Putting a function in a return statement
- When will this design pattern break?
- D3.js: Trying to build flat calendar
- How to update d3 graphics if I load from external data?
- D3 js bar chart pan
- Text and Rectangle not align
- unable to plot polygons in d3.js "format" over leaflet.js
- Select box in a ForeignObject within an svg within an svg doesn't repaint/update if it has a dropshadow in d3.js
- D3.js, Need click event in d3.js
- d3 js - update transition with multiple lines
- How to use an array (inside a javascript script) to create a SVG Polygon chart with D3.js?
- D3 Radar Chart Colors
- d3.js not able to parse json data
- Type errors for d3js in Angular ( Latest )
- How to prevent a d3.geo map to pan, but keep zoom
- How to load a local JSON object via D3 library in a Rails3 app?
- bootstrap form-inline not rendering properly with d3
- Adding a filter in dc.js / Crossfilter not updating the chart
- No Data value with D3
- Understanding D3 domain and ranges
- Keep changed position of NVD3 legend after click
- Tooltip in worldmap created via d3.js
- Date ticks for lineplusbar nvd3
- Move legend outside the chart area in C3.js
- programmatically trigger click event in svg rect element
- Preview d3 bar chart such that it sorted by values for each x value
- D3 V5 : sync initial zoom with zoom event
- D3 circle object has no properties