Comments (11)
Yes, you have to specify all parent nodes in the tabular data. There’s no way for d3.stratify to infer the implied parent nodes in the example you gave because it doesn’t understand the semantics of the id: the identifier is opaque. So, this is flat:
id,value
flare.analytics.cluster.AgglomerativeCluster,3938
flare.analytics.cluster.CommunityStructure,3812
flare.analytics.cluster.HierarchicalCluster,6714
flare.analytics.cluster.MergeEdge,743
Just like this:
id,value
foo,3938
bar,3812
baz,6714
qux,743
I couldn’t think of an elegant way to create the implied parent nodes. But maybe d3.stratify could have a mode where it creates implied parent nodes automatically if you opt-in… but you’d need another accessor function to either specify all parents of a particular node (flare.analytics.cluster.AgglomerativeCluster ↦ [flare, flare.analytics, flare.analytics.cluster]), or equivalently an accessor function to compute the parent of an implied parent (flare.analytics.cluster ↦ flare.analytics, flare.analytics ↦ flare, flare ↦ null).
At any rate, you wouldn’t want to use d3.stratify with d3.nest because d3.nest already returns a hierarchical data structure; you want to use d3.hierarchy and pass a children accessor. I’ll cook up an example for that.
from d3-hierarchy.
Take a look at the burrow
function in this example. The advantage over nest
is that each entry can be at an arbitrary depth. The advantage over stratify
as that parent nodes need not be specified in advance.
In the tsv file that is the output of du
, the parent nodes appear after the children, but that still works just fine. A burrow
-like function seems too similar to stratify
to be included in d3-hierarchy, but it's a convenient and forgiving way to generate hierarchies from flat files.
The particular use case I came across was a collection of organisms with partial taxonomy per organism. These are a few sample values from that column (a single column with comma-delimited strings in a TSV):
taxonomy
Proteobacteria, Bacteria
Burkholderiales, Betaproteobacteria, Proteobacteria, Bacteria
Microbacterium testaceum, Microbacterium, Micrococcales, Actinobacteria, Actinobacteria, Bacteria
Novosphingobium sp. MD-1, Novosphingobium, Sphingomonadales, Alphaproteobacteria, Proteobacteria, Bacteria
from d3-hierarchy.
An alternate basic example that solves the problem with d3.nest
or other method would work too.
from d3-hierarchy.
Here’s an example using d3.treemap and d3.nest:
from d3-hierarchy.
Looking at #34, it seems like creating implied parent nodes could be problematic with stratify if parents were to appear later in the dataset (like the output of du
I posted)
from d3-hierarchy.
This part in your example:
data.forEach(function(row) {
row.taxonomy = row.file.split("/");
});
Is functionally equivalent to what I was suggesting here with d3.stratify:
you’d need another accessor function to either specify all parents of a particular node (flare.analytics.cluster.AgglomerativeCluster ↦ [flare, flare.analytics, flare.analytics.cluster]), or equivalently an accessor function to compute the parent of an implied parent (flare.analytics.cluster ↦ flare.analytics, flare.analytics ↦ flare, flare ↦ null).
from d3-hierarchy.
Ordering shouldn’t be a problem. After a full pass of the input data, you’d have a set of missing parents. You’d then create nodes for each missing parent, and determine the next set of missing parents. When that set is empty, you’d be done.
Three options:
-
If we required a datum.id, rather than having an arbitrary id accessor function (say as with the proposed d3.stratifyDot in #75), then we can generate an implied parent node of the form
{id: …, children: […]}
, and pass that to the parentId accessor function. But I don’t see an elegant way of opting-in to this functionality, other than maybe stratify.parentImplied(true) and then throwing an error if stratify.id is set to anything on than the default. -
If we added a stratify.ancestorIds method, it could return the identifiers of every ancestor, and then we could create the missing ancestors as needed. This would be exclusive with stratify.parentId: setting one would clear the other.
-
Alternatively, a stratify.ancestorId method, which takes an identifier rather than a datum, and this is called repeatedly to determine the implied ancestors. This would also be exclusive with stratify.parentId: setting one would clear the other. It’s arguably confusing that this method has a different signature than stratify.parentId, but it seems reasonable…
from d3-hierarchy.
If stratify.ancestorId is used, it’d be nice if we could automatically set a node.name, too. For example, if the id is flare.analytics.cluster.AgglomerativeCluster
and the ancestor of this is flare.analytics.cluster
, the implied local name should be AgglomerativeCluster
. But, I don’t see a general way of inferring the name given an id and an ancestor id—I suppose there’s no requirement that the ancestor id is a prefix of the id, and there’s certainly no requirement that a single character is used as a separator.
Maybe I’m trying too hard to build this into d3.stratify, rather than creating something like d3.stratify that requires that the hierarchy structure be derived solely from delimiter-separated strings. And in that case, we can compute the implied parents and local names automatically.
from d3-hierarchy.
Would something like this hastily assembled flattree
be useful here? @syntagmatic, this example demonstrates how parent levels are inferred when not explicitly provided. In this example, parents are explicitly specified.
I do understand though that the expected input structure is not sparse, and for this reason flattree
might be better as a standalone entity.
from d3-hierarchy.
Eagerly looking forward to this solution.
from d3-hierarchy.
Maybe I’m trying to hard to build this into d3.stratify, rather than creating something like d3.stratify that requires that the hierarchy structure be derived solely from delimiter-separated strings. And in that case, we can compute the implied parents and local names automatically.
I think it can go both ways: provide a general stratify.ancestorIds
method and then a custom method for working with character-separated prefixed identifiers.
from d3-hierarchy.
Related Issues (20)
- Treemap leaf nodes with rect height = 0 HOT 1
- How can I handle the ts error "Type 'number | undefined' is not assignable to type 'number'." ? HOT 1
- IE11 issue - Const must be initialized in each.js HOT 1
- the json result convert to d3-hierarchy can not generate the correct format without children HOT 1
- tree layout: Support transparent intermediary nodes to keep the tree tidy HOT 3
- Undesirable padding behaviour with disparately sized children HOT 3
- node.sum/count for non-numeric data
- New nodesAtDepth(depth) function HOT 3
- d3 partition: ability to choose different "value" size variable
- document d3.Node
- Seemingly non-weird ball collection throws an error in d3.packEnclose() HOT 1
- Suggestion: pack() layouts with deterministic outputs HOT 5
- Inner and outer padding for partition layout
- How can you add new attributes to d3.heirarchy() object? HOT 1
- why non-negative for node.sum
- Using @ant-design/plots and this error is popping up
- d3.stratify().path() doesn't bind data HOT 1
- Stratify nodes that are both a leaf (with value) and a parent
- d3__WEBPACK_IMPORTED_MODULE_0__.hierarchy is not a function HOT 3
- d3__WEBPACK_IMPORTED_MODULE_0__.hierarchy is not a function HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from d3-hierarchy.