Skip to content

Instantly share code, notes, and snippets.

@Pragyagarg
Last active October 23, 2018 05:33
Show Gist options
  • Save Pragyagarg/7db8e7f02521b28df98e0153cc95d4cb to your computer and use it in GitHub Desktop.
Save Pragyagarg/7db8e7f02521b28df98e0153cc95d4cb to your computer and use it in GitHub Desktop.
Tree diagram using flat data with v4
license: mit

This is a tree diagram that used data from a 'flat' format. It has been written with d3.js v4 and is based on the simple horizontal version here

This is designed to be used as part of documenting an update to the book D3 Tips and Tricks to version 4 of d3.js.

forked from d3noob's block: Tree diagram using flat data with v4

<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 12px sans-serif; }
.node--internal text {
text-shadow: 0 1px 0 #fff, 0 -1px 0 #fff, 1px 0 0 #fff, -1px 0 0 #fff;
}
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
<body>
<input type="text" id="myId" placeholder="Add mouseId&hellip;">
<input name="button" type="submit" value="View Hierarchy" onclick="search()">
<!-- load the d3.js library -->
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
function f1(data, id) {
var data1 = [];
var list = [];
list.push(id);
while(list.length > 0) {
Object.keys(data).forEach(function(key){
if(data[key]["parent"] == list[0]) {
list.push(data[key]["name"])
console.log("adding data: " + data[key]["name"])
data1.push(data[key])
}
});
list.shift();
}
data1.push( {"name": id, "parent": null})
return data1;
}
function search() {
id = document.getElementById("myId").value;
console.log("id: " + id)
d3.json("result2_parent_child.json", function(data) {
var result = f1(data, id);
Object.keys(result).forEach(function(key){
console.log("name: " + result[key]["name"])
console.log("parent: " + result[key]["parent"])
});
// convert the flat data into a hierarchy
var treeData = d3.stratify()
.id(function(d) {
console.log("name: " + d.name);
return d.name; })
.parentId(function(d) {
console.log("parent: " + d.parent);
return d.parent; })
(result);
console.log("len: " + treeData.length)
// assign the name to each node
treeData.each(function(d) {
d.name = d.id;
console.log("id: " + d.name);
});
// console.log("treeData: " + treeData)
console.log("here")
// set the dimensions and margins of the diagram
var margin = {top: 120, right: 240, bottom: 250, left: 90},
width = 330,
height = 230;
// declares a tree layout and assigns the size
var treemap = d3.tree()
.size([height, width]);
// assigns the data to a hierarchy using parent-child relationships
var nodes = d3.hierarchy(treeData, function(d) {
return d.children;
});
// maps the node data to the tree layout
nodes = treemap(nodes);
// append the svg object to the body of the page
// appends a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom),
g = svg.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// adds the links between the nodes
var link = g.selectAll(".link")
.data( nodes.descendants().slice(1))
.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
return "M" + d.x + "," + (300-d.y)
+ "C" + d.x + "," + (300-(d.y + d.parent.y) / 2)
+ " " + d.parent.x + "," + (300-(d.y + d.parent.y) / 2)
+ " " + d.parent.x + "," + (300-d.parent.y);
});
// adds each node as a group
var node = g.selectAll(".node")
.data(nodes.descendants())
.enter().append("g")
.attr("class", function(d) {
return "node" +
(d.children ? " node--internal" : " node--leaf"); })
.attr("transform", function(d) {
console.log("x: " + d.x)
console.log("y: " + d.y)
return "translate(" + d.x + "," + (300 - d.y) + ")"; });
// adds the circle to the node
node.append("circle")
.attr("r", 10);
// adds the text to the node
node.append("text")
.attr("dy", ".35em")
.attr("x", function(d) { return d.children ? -13 : 13; })
.style("text-anchor", function(d) {
return d.children ? "end" : "start"; })
.text(function(d) { return d.data.name; });
});
}
</script>
</body>
[{"name": "1", "parent": "19"}, {"name": "4", "parent": "19"}, {"name": "1", "parent": "17"}, {"name": "2", "parent": "17"}, {"name": "1", "parent": "18"}, {"name": "4", "parent": "18"}, {"name": "13", "parent": "33"}, {"name": "6", "parent": "33"}, {"name": "1", "parent": "15"}, {"name": "2", "parent": "15"}, {"name": "1", "parent": "16"}, {"name": "2", "parent": "16"}, {"name": "1", "parent": "13"}, {"name": "2", "parent": "13"}, {"name": "1", "parent": "14"}, {"name": "2", "parent": "14"}, {"name": "1", "parent": "11"}, {"name": "3", "parent": "11"}, {"name": "1", "parent": "12"}, {"name": "2", "parent": "12"}, {"name": "13", "parent": "21"}, {"name": "5", "parent": "21"}, {"name": "1", "parent": "20"}, {"name": "4", "parent": "20"}, {"name": "13", "parent": "22"}, {"name": "5", "parent": "22"}, {"name": "13", "parent": "23"}, {"name": "5", "parent": "23"}, {"name": "13", "parent": "24"}, {"name": "5", "parent": "24"}, {"name": "13", "parent": "25"}, {"name": "5", "parent": "25"}, {"name": "13", "parent": "26"}, {"name": "6", "parent": "26"}, {"name": "13", "parent": "27"}, {"name": "6", "parent": "27"}, {"name": "13", "parent": "28"}, {"name": "6", "parent": "28"}, {"name": "13", "parent": "29"}, {"name": "6", "parent": "29"}, {"name": "1", "parent": "10"}, {"name": "3", "parent": "10"}, {"name": "13", "parent": "30"}, {"name": "6", "parent": "30"}, {"name": "13", "parent": "32"}, {"name": "6", "parent": "32"}, {"name": "13", "parent": "31"}, {"name": "6", "parent": "31"}, {"name": "1", "parent": "9"}, {"name": "3", "parent": "9"}, {"name": "1", "parent": "8"}, {"name": "3", "parent": "8"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment