[英]Generate (multilevel) flare.json data format from flat json
I have a flat json file structure like: 我有一个扁平的json文件结构,如:
[
{ "name" : "ABC", "parent":"DEF", "relation": "ghi", "depth": 1 },
{ "name" : "DEF", "parent":"null", "relation": "null", "depth": 0 },
{ "name" : "new_name", "parent":"ABC", "relation": "rel", "depth": 2 }
....
....
]
And what I want is a nested file structure like: 我想要的是一个嵌套的文件结构,如:
[
{
"name": "DEF",
"parent": "null",
"relation": "null",
"children": [
{ "name": "ABC",
"parent": "DEF",
"relation": "ghi",
"children": [
"name": "new_name",
...
"children": []
]
}
]
}
]
There is no limit on how many levels deep it should go. 它的深度应该没有限制。 The current max I have is 30. There is no limit on the number of children a node can have.
我当前的最大值是30.节点可以拥有的子节点数没有限制。 Eg.
例如。 The root node has all the remaining as its children.
根节点将所有剩余的节点作为其子节点。
What I have tried till now? 我到现在为止尝试过什么?
Read about d3.nest() and how it is able to nest but not perfectly. 阅读有关d3.nest()及其如何嵌套但不完美的信息。 https://groups.google.com/forum/?fromgroups=#!topic/d3-js/L3UeeUnNHO8/discussion
https://groups.google.com/forum/?fromgroups=#!topic/d3-js/L3UeeUnNHO8/discussion
Wrote a python script for this, but it gets stuck with the null values and also since the data is not bounded (It increases everyday in double figures) so it is very very slow. 为此写了一个python脚本,但是它被空值所困,并且由于数据没有限制(它每天以两位数增加)所以它非常慢。
I tried the force directed layout and it worked very well but I want to add another layout that makes the visualization easy. 我尝试了强制定向布局并且它工作得很好,但我想添加另一种布局,使可视化变得容易。
I could go with some other python scripts posted but they do not seem to carry forward any other information than "name" and "children". 我可以使用其他一些python脚本发布,但它们似乎没有提供除“名称”和“孩子”之外的任何其他信息。
I read this: http://blog.pixelingene.com/2011/07/building-a-tree-diagram-in-d3-js/ but they too have the right format data in the first place. 我读到了这个: http : //blog.pixelingene.com/2011/07/building-a-tree-diagram-in-d3-js/但他们也拥有正确的格式数据。 What I intend to create is http://bl.ocks.org/mbostock/4339083 .
我打算创建的是http://bl.ocks.org/mbostock/4339083 。
The source of the data is MS SQL Server database which I am fetching and parsing through python. 数据源是我正在通过python获取和解析的MS SQL Server数据库。 Kindly help!
请帮忙! i have been stuck at this for the past 2 weeks.
过去两周我一直被困在这里。
Thanks 谢谢
Here's one implementation, in Javascript: http://jsfiddle.net/9FqKS/ 这是Javascript中的一个实现: http : //jsfiddle.net/9FqKS/
You start by creating a name-based map for easy lookup. 首先,创建一个基于名称的地图,以便于查找。 There are a few different ways to do this - in this case, I use a
.reduce
method, which starts with an empty object and iterates over the data
array, adding an entry for each node: 有几种不同的方法可以做到这一点 - 在这种情况下,我使用
.reduce
方法,该方法以空对象开始并遍历data
数组,为每个节点添加一个条目:
// create a {name: node} map
var dataMap = data.reduce(function(map, node) {
map[node.name] = node;
return map;
}, {});
This is equivalent to: 这相当于:
var dataMap = {};
data.forEach(function(node) {
dataMap[node.name] = node;
});
(I sometimes think the reduce is more elegant.) Then iteratively add each child to its parents, or to the root array if no parent is found: (我有时认为reduce更优雅。)然后迭代地将每个子节点添加到其父节点,或者如果没有找到父节点则添加到根数组:
// create the tree array
var tree = [];
data.forEach(function(node) {
// find parent
var parent = dataMap[node.parent];
if (parent) {
// create child array if it doesn't exist
(parent.children || (parent.children = []))
// add node to parent's child array
.push(node);
} else {
// parent is null or missing
tree.push(node);
}
});
Unless your tree is enormous, I don't think this should be too expensive, so you ought to be able to do it on the client side (if you can't, you might have too much data to easily display in any case). 除非你的树很大,我认为这不应该太昂贵,所以你应该能够在客户端做到这一点(如果你不能,你可能有太多的数据,无论如何都很容易显示) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.