Я пытаюсь создать D3 Tree
и в некоторой степени выполнить задачу, но в моей ситуации может быть больше 1 родительский узел к одному узлу.Попытка добавить второй родительский узел к дочернему узлу с использованием библиотеки древовидных диаграмм d3
Но, как я узнал, мне все же нужно начать с одного корневого узла, чтобы построить дерево, и я сделал это и смог построить дерево, а теперь хочу добавить второй узел к узлу дерева ,
var data = [{
"children": [{
"children": [{
"children": [{
"children": [
],
"name": "Hierarchy Child 5",
"parent": "Hierarchy Sibling",
"relation": "Parent Of",
"rid": "a059000000VBecUAAT"
}],
"name": "Hierarchy Sibling",
"parent": "Hierarchy Parent",
"relation": "Parent Of",
"rid": "a059000000VBKeYAAX"
}, {
"children": [{
"children": [
],
"name": "Hierarchy Child 1",
"parent": "ACME",
"relation": "Parent Of",
"rid": "a059000000VBKf7AAH"
}, {
"children": [
],
"name": "Hierarchy Child 2",
"parent": "ACME",
"relation": "Parent Of",
"rid": "a059000000VBKfCAAX"
}, {
"children": [
],
"name": "Hierarchy Child 3",
"parent": "ACME",
"relation": "Parent Of",
"rid": "a059000000VBKfHAAX"
}, {
"children": [
],
"name": "Hierarchy Child 4",
"parent": "ACME",
"relation": "Parent Of",
"rid": "a059000000VBKfMAAX"
}],
"name": "ACME",
"parent": "Hierarchy Parent",
"relation": "Parent Of",
"rid": "a059000000VBKeJAAX"
}],
"name": "Hierarchy Parent",
"parent": "This is Test Second Parent Node",
"relation": "Parent Of",
"rid": "a059000000VBKeOAAX"
}, {
"children": [
],
"name": "Hierarchy Uncle",
"parent": "Hierarchy Grandparent",
"relation": "Parent Of",
"rid": "a059000000VBKedAAH"
}, {
"children": [
],
"name": "Hierarchy Great Uncle",
"parent": "Hierarchy Grandparent",
"relation": "Parent Of",
"rid": "a059000000VBKenAAH"
}],
"name": "Hierarchy Grandparent",
"parent": "null",
"relation": "Parent Of",
"rid": "a059000000VBKeTAAX"
}];
// *********** Convert flat data into a nice tree ***************
// create a name: node map
var dataMap = data.reduce(function(map, node) {
map[node.name] = node;
return map;
}, {});
// create the tree array
var treeData = [];
data.forEach(function(node) {
// add to parent
var parent = dataMap[node.parent];
if (parent) {
// create child array if it doesn't exist
(parent.children || (parent.children = []))
// add node to child array
.push(node);
} else {
// parent is null or missing
treeData.push(node);
}
});
// ************** Generate the tree diagram \t *****************
var margin = {
top: 20,
right: 120,
bottom: 20,
left: 120
},
width = 960 - margin.right - margin.left,
height = 500 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) {
return [d.y, d.x];
});
var svg = d3.select("div#chart-container").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root);
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) {
d.y = d.depth * 180;
});
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) {
return d.id || (d.id = ++i);
});
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.y + "," + d.x + ")";
});
nodeEnter.append("circle")
.attr("r", 10)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("x", function(d) {
return d.children || d._children ? -13 : 13;
})
.attr("dy", ".35em")
.attr("text-anchor", function(d) {
return d.children || d._children ? "end" : "start";
})
.text(function(d) {
return d.name;
})
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) {
return d.target.id;
});
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
}
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text {
font: 12px sans-serif;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chart-container">
</div>
Как мы можем видеть, код работает нормально, и дерево порождает, но мой массив JSON данных имеет второй родитель на узле с name
как Hierarchy Parent
имеет parent
определен как This is Test Second Parent Node
, но этот узел не отображается на дереве.
Это изображение является то, что я пытаюсь создать в качестве вывода:
Это многоуровневое дерево выглядит так, как мне нужно здесь, но я не могу понять 'connectionParent1' и' connectionChild1', который, похоже, мне нужно сделать в моем коде, чтобы работать с несколькими родителями :-( – VarunC