从平面json生成(多级)flare.json数据格式 [英] Generate (multilevel) flare.json data format from flat json
问题描述
我有一个平面的json文件结构,如:
I have a flat json file structure like:
[
{ "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 }
....
....
]
嵌套文件结构如:
[
{
"name": "DEF",
"parent": "null",
"relation": "null",
"children": [
{ "name": "ABC",
"parent": "DEF",
"relation": "ghi",
"children": [
"name": "new_name",
...
"children": []
]
}
]
}
]
深度应该有多少级别没有限制。当前的最大值为30.
节点可以拥有的子节点数没有限制。例如。根节点具有所有剩余的子节点。
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. Eg. The root node has all the remaining as its children.
我到目前为止所做的是什么?
What I have tried till now?
-
阅读关于d3.nest()以及它如何嵌套,但不完美。
https://groups.google.com/forum/? fromgroups =#!topic / d3-js / L3UeeUnNHO8 / discussion
写了一个python脚本,
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.
我尝试了强制导向的布局,它的工作方式非常非常慢。非常好,但我想添加另一个布局,使可视化很容易。
I tried the force directed layout and it worked very well but I want to add another layout that makes the visualization easy.
我可以使用一些其他python脚本,但他们似乎没有转发任何其他信息比名称和儿童。
I could go with some other python scripts posted but they do not seem to carry forward any other information than "name" and "children".
我看到这个: http://blog.pixelingene.com/2011/07/building-a-tree-diagram-in-d3-js/
,但他们也有正确的格式数据在第一名。我打算创建的是 http://bl.ocks.org/mbostock/4339083 。
数据源是MS SQL Server数据库,我通过python提取和解析。请帮忙!
The source of the data is MS SQL Server database which I am fetching and parsing through python. Kindly help! i have been stuck at this for the past 2 weeks.
感谢
推荐答案
以下是Javascript中的一种实现: http://jsfiddle.net/9FqKS/
Here's one implementation, in Javascript: http://jsfiddle.net/9FqKS/
您首先创建一个基于名称的地图,以便于查找。有几种不同的方法可以做到这一点 - 在这种情况下,我使用一个 .reduce
方法,从一个空对象开始,并遍历数据
数组,为每个节点添加一个条目:
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:
// create a {name: node} map
var dataMap = data.reduce(function(map, node) {
map[node.name] = node;
return map;
}, {});
这相当于:
var dataMap = {};
data.forEach(function(node) {
dataMap[node.name] = node;
});
(我有时认为reduce更优雅。)然后迭代地添加每个孩子到其父
(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:
// 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).
这篇关于从平面json生成(多级)flare.json数据格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!