'adding a additional text under nodes name d3.js
I am pretty new to d3.js and have created a node tree in javascript using a JSON within a javaScript file. I added an extra field for my last children called classNum I want this to display under my last node's name but is only displaying to the side I tried using '\n ' not sure if it is fixed in a certain area.
This is the code I am trying to get to return my text correctly
nodeEnter.append("text")
.attr("y", function (d) {
return d.children || d._children ? -18 : 18;
})
.attr("dy", "0em")
.attr("text-anchor", "middle")
.text(function (d) {
if(d.classNum == null)
{
return d.name;
}
else
{
return d.name + '\n' +d.classNum ;
}
})
.style("fill-opacity", 1);
(sorry if the code is ugly I am new to this and my code is a collect of ways I saw others do it on the internet)
this is the file creating my tree
// ************** Generate the tree diagram *****************
var margin = {
top: 50,
right: 50,
bottom: 50,
left: 50
},
width = window.innerWidth// Use the window's width
, height = window.innerHeight// Use the window's height
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree().size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function (d) {
return [d.x, d.y];
});
var svg = d3.select("body").append("svg")
.attr("viewBox", "-80 -30 1300 3000")
.classed("svg-content-responsive", true);
var aspect = width / height,
chart = d3.select('#chart');
d3.select(window)
.on("resize", function () {
var targetWidth = chart.node().getBoundingClientRect().width;
chart.attr("width", targetWidth);
chart.attr("height", targetWidth / aspect);
});
root = treeSoftDEv[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameElement).style("height", "500px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth y.
nodes.forEach(function (d) {
d.y = d.depth * 120;
});
// Update the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function (d) {
return d.id || (d.id = ++i);
});
// Enter any new nodes at the parent's previous position. added
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + source.x0 + "," + source.y0 + ")";
})
.on("click", click);
nodeEnter.append("circle")
.attr("r", function (d) {
return d.value;
})
.style("stroke", function (d) {
return d.type;
})
.style("fill", function (d) {
return d.level;
});
nodeEnter.append("text")
.attr("y", function (d) {
return d.children || d._children ? -18 : 18;
})
.attr("dy", "0em")
.attr("text-anchor", "middle")
.text(function (d) {
if(d.classNum == null)
{
return d.name;
}
else
{
return d.name + '\n' +d.classNum ;
}
})
.style("fill-opacity", 1);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
});
nodeUpdate.select("circle")
.attr("r", function (d) {
return d.value;
})
.style("stroke", function (d) {
return d.type;
})
.style("fill", function (d) {
return d.level;
});
nodeUpdate.select("text")
.style("fill-opacity", 1);
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + source.x + "," + source.y + ")";
})
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update 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")
.style("stroke", function (d) {
return d.target.level;
})
.attr("d", diagonal);
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function (d) {
var o = {y: source.y, x: source.x};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function (d) {
d.y0 = d.y;
d.x0 = d.x;
});
}
function click(d) {
var index;
for(var i=0;i<d.parent.children.length;i++){//length of current label
if(d.parent.children[i].name===d.name)
index = i;
};
for(var i=0;i<d.parent.children.length;i++){
if(typeof d.parent.children[i].children!=="undefined" && i!=index){//if child is expnd then make null
d.parent.children[i]._children=d.parent.children[i].children;
d.parent.children[i].children= null;
}
}
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
this is my file creating my json file
var treeSoftDEv = [
{
"name": "BACHELOR OF SCIENCE",
"value": 14,
"type": "black",
"level": "red",
"children": [
{
"name": "INFORMATION TECHNOLOGY",
"parent": "BACHELOR OF SCIENCE",
"value": 12,
"type": "grey",
"level": "pink",
"children": [
{
"name": "Software Development",
"parent": "INFORMATION TECHNOLOGY",
"value": 10,
"type": "steelblue",
"level": "green",
"children": [
{
"name": "Junior/Entry Software Developers",
"parent": "Software Development",
"value": 9,
"type": "darkblue",
"level": "purple",
"children": [
{
"name": "Java",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Adv Programming",
"classNum" : "ITEC 3150",
"parent": "Java",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "SQL",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Adv Database",
"classNum" : "ITEC 3150",
"parent": "SQL",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": ".Net",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "tbh",
"classNum" : "ITEC 3150",
"parent": ".Net",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "CSS",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Web Dev",
"classNum" : "ITEC 3150",
"parent": "CSS",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "JavaScript",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Web Dev",
"classNum" : "ITEC 3150",
"parent": "JavaScript",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "Server",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "tbh",
"classNum" : "ITEC 3150",
"parent": "Server",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
}
, {
"name": "MVC",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "tbh",
"classNum" : "ITEC 3150",
"parent": "MVC",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "Rest API",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Software Projects",
"classNum" : "ITEC 3150",
"parent": "Rest API",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "Microsoft",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Intro to Computing",
"classNum" : "ITEC 3150",
"parent": "Microsoft",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
}
, {
"name": "PHP",
"parent": "Junior/Entry Software Developers",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "Web Dev",
"classNum" : "ITEC 3150",
"parent": "SQL",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
}]
},
{
"name": "Job2",
"parent": "Data Science",
"value": 9,
"type": "darkblue",
"level": "purple",
"children": [
{
"name": "skill1",
"parent": "Job",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "course",
"parent": "skill1",
"value": 7,
"type": "lightgreen",
"level": "black"
},
{
"name": "course2",
"parent": "skill1",
"value": 7,
"type": "lightgreen",
"level": "black"
},
{
"name": "course3",
"parent": "skill1",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "skill2",
"parent": "Job",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "course",
"parent": "skill2",
"value": 7,
"type": "lightgreen",
"level": "black"
},
{
"name": "course2",
"parent": "skill2",
"value": 7,
"type": "lightgreen",
"level": "black"
},
{
"name": "course3",
"parent": "skill2",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
},
{
"name": "skill3",
"parent": "Job",
"value": 8,
"type": "lightblue",
"level": "orange",
"children": [
{
"name": "course",
"parent": "skill3",
"value": 7,
"type": "lightgreen",
"level": "black"
},
{
"name": "course2",
"parent": "skill3",
"value": 7,
"type": "lightgreen",
"level": "black"
},
{
"name": "course3",
"parent": "skill3",
"value": 7,
"type": "lightgreen",
"level": "black"
}]
}
]
}]
}]
}]
}]
console.log(JSON.parse(treeSoftDEv))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
