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 iterate over nested data
- How to structure nested nodes that need updating in d3 v4 force layout?
- How can I add starting positions for D3 Force?
- How can I use my data from a JSON string to populate dimple d3.js line chart?
- reading in csv with date parsing
- Cities displayed in topojson visualization, but not in file itself?
- D3 Placeholder for bar graph
- D3.js v5 not displaying any data using d3.tsv()
- D3js working with unknown: headers/number of columns/number of rows
- D3 force layout graph with nodes positioned in a grid
- Have D3 force directed graph form a specific shape?
- d3.js v4, how do I have a line follow the mouse on hover, but also have a circle follow the path?
- Linking 3 simple squares to data which includes required color
- How to output PHP result in plottable format
- Data loading issue in Table using D3 version 4
- Align svg text inline with d3.js legend
- d3.js simple area chart example ... getting "undefined" error; I think I need a return statement?
- Angular 2 & d3: How to call this when function this exists
- D3 pie chart legends with horizontal pagination
- D3 treemaps with images
- d3js v5 Normalized Stacked Bar Chart
- d3 js markers don't display on map
- D3 Circle-Packing Clear Labeling Solution
- Initiate D3.js data map with new data
- D3.js adding/removing data pie chart slices
- Prevent node overlap in force directed graph
- D3.js advanced node connections (find the path)
- Setting a min & max on the axis
- aframe vive controller Uncaught TypeError: hand.getAttribute is not a function
- How to update bound data in d3.js?