如何使Radial Tidy Tree处理来自networkx的node_link_data(G[, attrs])
或tree_data(G, root[, attrs])
的JSON树数据?
如何使D3 Cluster Dendrogram处理来自networkx的node_link_data(G[, attrs])
或tree_data(G, root[, attrs])
的JSON树数据?
如何使D3 Tidy Tree处理来自networkx的node_link_data(G[, attrs])
或tree_data(G, root[, attrs])
的JSON树数据?
示例不使用JSON格式,而是CSV风格的输入。
嗨,
我正在使用Python和networkx来生成图形数据(通常是树状图)。我可以使用their page上提到的networkx函数将数据导出到JSON:
node_link_data(G[, attrs])
:以适用于JSON序列化并在Javascript文档中使用的节点链接格式返回数据。adjacency_data(G[, attrs])
:以适合JSON序列化并在Javascript文档中使用的邻接格式返回数据。tree_data(G, root[, attrs])
:以树格式返回适合JSON序列化并在Javascript文档中使用的数据。networkx的文档明确提到:
中的树
但是,linked example似乎并没有使用JSON树格式,而是使用了csv
type of input。由于我的javascript-skilz本质上为零,因此我不了解如何使Mike Bostock的示例与我拥有的JSON数据一起使用。你能帮忙吗?
{
"issueid": "3295658",
"issuetype": "Portfolio-Epic",
"status": "In Progress",
"pirank": 24,
"id": "ATROB-2523",
"children": [
{
"issueid": "3288189",
"issuetype": "Epic",
"status": "Implementation",
"id": "ATTDATA-233",
"children": [
{
"issueid": "3305730",
"issuetype": "Task",
"status": "Implementation",
"id": "ATTDATA-363"
},
{
"issueid": "3305723",
"issuetype": "Task",
"status": "Open",
"id": "ATTDATA-361"
},
{
"issueid": "3301728",
"issuetype": "Task",
"status": "Open",
"id": "ATTDATA-336"
},
{
"issueid": "3297381",
"issuetype": "Task",
"status": "Closed",
"id": "ATTDATA-300"
},
{
"issueid": "3295913",
"issuetype": "Task",
"status": "Review",
"id": "ATTDATA-290"
},
{
"issueid": "3295893",
"issuetype": "Task",
"status": "Open",
"id": "ATTDATA-289"
},
{
"issueid": "3291658",
"issuetype": "Task",
"status": "Closed",
"id": "ATTDATA-256"
},
{
"issueid": "3291653",
"issuetype": "Task",
"status": "Closed",
"id": "ATTDATA-255"
},
{
"issueid": "3291530",
"issuetype": "Task",
"status": "Open",
"id": "ATTDATA-253"
},
{
"issueid": "3290232",
"issuetype": "Task",
"status": "Open",
"id": "ATTDATA-247"
},
{
"issueid": "3287061",
"issuetype": "Task",
"status": "Resolved",
"id": "ATTDATA-226"
}
]
},
{
"issueid": "3300899",
"issuetype": "Request",
"status": "REJECTED",
"id": "ATI-1478"
}
]
}
答案 0 :(得分:2)
Mike Bostock的链接示例使用d3.stratify将CSV数据转换为分层形式:
var stratify = d3.stratify()
.parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
[... later in the code...]
var root = tree(stratify(root));
在您的情况下,您的数据已经是分层形式的,因此您可以通过d3.hierarchy来运行它,以将其转换为d3型分层结构,然后使用它来创建图表:
var root = tree(d3.hierarchy(data));
您还需要将提取数据的功能从d3.csv
更改为d3.json
:
// assume your data is in `data.json`
d3.json("data.json", function(error, data) {
所以脚本的顶部现在看起来像这样:
<script type="text/javascript">
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(" + (width / 2 + 40) + "," + (height / 2 + 90) + ")");
var tree = d3.tree()
.size([2 * Math.PI, 300])
.separation(function(a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
d3.json("data.json", function(error, data) {
if (error) throw error;
var root = tree(d3.hierarchy(data));
Here's a .block和上面提供的JSON数据中的树。