score:15
Accepted answer
you can't use the nest operator here because nesting produces a fixed hierarchy: the number of levels in the output hierarchy is the same as the number of key functions you specify.
that said, you can write your own function which produces a tree. assuming that the root node is the first node in the input array, you can create a map from id to node, and then construct the tree lazily.
function tree(nodes) {
var nodebyid = {};
// index the nodes by id, in case they come out of order.
nodes.foreach(function(d) {
nodebyid[d.id] = d;
});
// lazily compute children.
nodes.foreach(function(d) {
if ("manager" in d) {
var manager = nodebyid[d.manager];
if (manager.children) manager.children.push(d);
else manager.children = [d];
}
});
return nodes[0];
}
if you know that the nodes are listed in order such that managers appear before their reports, you can simplify the code to iterate only once.
Source: stackoverflow.com
Related Query
- D3: use nest function to turn flat data with parent key into a hierarchy
- How to use the quantize function with my json data file
- Use d3 stratify to transform flat data to hierarchy data
- D3.js nest function, cannot get data into hierarchy
- How to use d3.js Hierarchy with custom data structure?
- d3.js Map (<svg>) Auto Fit into Parent Container and Resize with Window
- d3js: make new parent data descend into child nodes
- Combining Parent and Nested Data with d3.js
- How to make data() key function to work with TypeScript?
- How to replace a d3js nest function with group and rollup
- Nesting or mapping data from key lower in json hierarchy
- Use d3 stack layout with data organized per points, instead of per layers
- How do I turn a piece of text into a parent children JSON file?
- How to use a value in d3 data key function?
- D3 with AngularJs, Multiple charts on same page with different data based on common key
- Key function in data binding: objects vs strings
- Stratify flat data without directly specifying parent
- D3 forceRadial() with nested data centered around parent
- Bootstrap doesn't let me use data with other libraries
- Data Join with Custom Key does not work as expected
- When creating an HTML table with D3, use certain data for a row class rather than cell
- Enter() statement with a function from one data array to multiple arrays? (Adding multiple Gradient Paths)
- How can i bind an json data with a key and Name for display
- d3.js replacing random() data function with asynchronous function
- D3.js dynamical style() function with geoJSON data
- How D3 key function in data binding works
- d3 Grouping data on multiple values in combination with key value pair
- Automatically place text from two data variables into an svg with d3
- d3.js: how to use data from the parent node? (to show the grouped bar name at the individual bar tooltip)
- D3.js .each() with function that passes data
More Query from same tag
- D3 pie chart disapears from map
- Why are tipsy and qtip not picking my svg elements?
- d3 parsing and drawing nested csv data
- D3.js tree custom node overlapping
- Javascript module pattern in D3
- Creating axis with custom (range) values
- d3 node-tooltip values multi-line
- How can i show the labels on the x axis when there is data overlap in d3js?
- Using multiple datasets for a given D3 force layout
- In D3.js I am confused by behavior of exit()
- Attribute cy returning expected length error in D3 V4
- D3.js Tree pass node.name on click to R Shiny
- d3 table append data according to different data types
- Add text before array elements
- Understanding nvd3 x-axis date format
- Transition on path in d3.js is not working
- Circle chart with repulsive circles
- How to add multiple images to svg in D3 (quantity based on value in dataset)
- I want to zoom to a circle directly in d3js without clicking or double clicking it but programmatically
- How to center & scale map in d3js (use projection.center/translate/rotate?)
- D3 albersUsa projection funtion return null
- d3 bar chart y axis ticks and grid lines above max value
- How to transform added circles correctly?
- Customize axis points in d3 chart with respect to bar width
- D3 Animate One Path Series At a Time
- How to get the x and y axis on click Angular JS Charts?
- D3.js: Trying to build flat calendar
- angular directive d3 stuck when hidden on mobile
- Differentiating between bars in tooltip for stacked bar chart
- Right to Left tree direction in D3