xxxxxxxxxx
<meta charset="utf-8">
<style> /* set the CSS */
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node rect {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 10px 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>
<!-- load the d3.js library -->
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
function textFlow(myText,textToAppend,maxWidth,x,ddy,justified) {
//extract and add line breaks for start
var dashArray = new Array();
var dashFound = true;
var indexPos = 0;
var cumulY = 0;
while (dashFound == true) {
var result = myText.indexOf("-",indexPos);
if (result == -1) {
//could not find a dash
dashFound = false;
}
else {
dashArray.push(result);
indexPos = result + 1;
}
}
//split the text at all spaces and dashes
var words = myText.split(/[\s-]/);
var line = "";
var dy = 0;
var curNumChars = 0;
var computedTextLength = 0;
var myTextNode;
var tspanEl;
var lastLineBreak = 0;
for (i=0;i<words.length;i++) {
var word = words[i];
curNumChars += word.length + 1;
if (computedTextLength > maxWidth || i == 0) {
if (computedTextLength > maxWidth) {
var tempText = tspanEl.firstChild.nodeValue;
tempText = tempText.slice(0,(tempText.length - words[i-1].length - 2)); //the -2 is because we also strip off white space
tspanEl.firstChild.nodeValue = tempText;
if (justified) {
//determine the number of words in this line
var nrWords = tempText.split(/\s/).length;
computedTextLength = tspanEl.getComputedTextLength();
var additionalWordSpacing = (maxWidth - computedTextLength) / (nrWords - 1);
tspanEl.setAttributeNS(null,"word-spacing",additionalWordSpacing);
//alternatively one could use textLength and lengthAdjust, however, currently this is not too well supported in SVG UA's
}
}
tspanEl = document.createElementNS(svgNS,"tspan");
tspanEl.setAttributeNS(null,"x",x);
tspanEl.setAttributeNS(null,"dy",dy);
myTextNode = document.createTextNode(line);
tspanEl.appendChild(myTextNode);
textToAppend.appendChild(tspanEl);
if(checkDashPosition(dashArray,curNumChars-1)) {
line = word + "-";
}
else {
line = word + " ";
}
if (i != 0) {
line = words[i-1] + " " + line;
}
dy = ddy;
cumulY += dy;
}
else {
if(checkDashPosition(dashArray,curNumChars-1)) {
line += word + "-";
}
else {
line += word + " ";
}
}
tspanEl.firstChild.nodeValue = line;
computedTextLength = tspanEl.getComputedTextLength();
if (i == words.length - 1) {
if (computedTextLength > maxWidth) {
var tempText = tspanEl.firstChild.nodeValue;
tspanEl.firstChild.nodeValue = tempText.slice(0,(tempText.length - words[i].length - 1));
tspanEl = document.createElementNS(svgNS,"tspan");
tspanEl.setAttributeNS(null,"x",x);
tspanEl.setAttributeNS(null,"dy",dy);
myTextNode = document.createTextNode(words[i]);
tspanEl.appendChild(myTextNode);
textToAppend.appendChild(tspanEl);
}
}
}
return cumulY;
}
//this function checks if there should be a dash at the given position, instead of a blank
function checkDashPosition(dashArray,pos) {
var result = false;
for (var i=0;i<dashArray.length;i++) {
if (dashArray[i] == pos) {
result = true;
}
}
return result;
}
console.clear();
// the flat data
var flatData = [
{"name": "Top Level", "parent": null},
{"name": "Level 2: A", "parent": "Top Level" },
{"name": "Level 2: B", "parent": "Top Level" },
{"name": "Son of A", "parent": "Level 2: A" },
{"name": "Daughter of A", "parent": "Level 2: A" }
];
var data = [
{par: null, id: 'ESTATES' , description: 'Estates Department' , level: 40},
{par: 'ESTATES', id: 'ESTFACMGMT' , description: 'Estates Facilities Management' , level: 50},
{par: 'ESTATES', id: 'ESTCAPPROJ' , description: 'Estates Capital Projects' , level: 50},
{par: 'ESTATES', id: 'ESTPLNPROP' , description: 'Estates Planning and Property' , level: 50},
{par: 'ESTATES', id: 'ESTMAINT' , description: 'Estates Maintenance' , level: 50},
{par: 'ESTATES', id: 'ESTTELADMN' , description: 'Estates Administration and Telecoms' , level: 50},
{par: 'ESTATES', id: 'ESTFINANCE' , description: 'Estates Finance and Corporate Services' , level: 50},
{par: 'ESTATES', id: 'ESTENGSUST' , description: 'Estates Energy and Sustainability' , level: 50},
{par: 'ESTFACMGMT', id: 'ESTSECURIT' , description: 'Estates Campus Security' , level: 60},
{par: 'ESTFACMGMT', id: 'ESTTRANSPO' , description: 'Estates Transport and Car Parking' , level: 60},
{par: 'ESTFACMGMT', id: 'ESTPOST' , description: 'Estates Postal Services' , level: 60},
{par: 'ESTFACMGMT', id: 'ESTCLEAN' , description: 'Estates Cleaning' , level: 60},
{par: 'ESTFACMGMT', id: 'ESTGENERAL' , description: 'Estates General Services' , level: 60},
{par: 'ESTFACMGMT', id: 'ESTINTSERV' , description: 'Estates Interior Services' , level: 60},
{par: 'ESTFACMGMT', id: 'ESTGROUND' , description: 'Estates Grounds' , level: 60},
{par: 'ESTTELADMN', id: 'ESTTELECOM' , description: 'Estates Telecoms' , level: 60},
{par: 'ESTTELADMN', id: 'ESTADMIN' , description: 'Estates Administration' , level: 60},
{par: 'ESTTELADMN', id: 'ESTHLPDESK' , description: 'Estates Helpdesk' , level: 60},
{par: 'ESTFINANCE', id: 'ESTDES&PRI' , description: 'Estates Design and Print Centre' , level: 60}
] ;
// convert the flat data into a hierarchy
var treeData = d3.stratify(data)
.id(function(d) { return d.id; })
.parentId(function(d) { return d.par; })
(data);
//console.log(treeData);
// assign the name to each node
treeData.each(function(d) {
d.name = d.id;
d.desc = d.data.description;
// console.log(d.data.description)
});
// set the dimensions and margins of the diagram
var margin = {top: 20, right: 200, bottom: 30, left: 150},
width = 800 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// 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.y + "," + d.x
+ "C" + (d.y + d.parent.y) / 2 + "," + d.x
+ " " + (d.y + d.parent.y) / 2 + "," + d.parent.x
+ " " + d.parent.y + "," + d.parent.x;
});
// 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) {
return "translate(" + d.y + "," + d.x + ")"; });
// adds the circle to the node
node.append("rect")
.attr("width", 40)
.attr("height", 20);
// .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) { console.log(d.data.desc); return d.data.desc; });
</script>
</body>
https://d3js.org/d3.v4.min.js