Skip to content

Instantly share code, notes, and snippets.

@dennisseah
Last active July 12, 2017 10:12
Show Gist options
  • Save dennisseah/8dbb7a23ef9c9c264249 to your computer and use it in GitHub Desktop.
Save dennisseah/8dbb7a23ef9c9c264249 to your computer and use it in GitHub Desktop.
SAPUI5: Organizational Chart with d3.js
<html>
<head>
<script src="http://d3js.org/d3.v3.min.js"></script>
<meta http-equiv='X-UA-Compatible' content='IE=edge' />
<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>
<style>
.dennisseah-orgchart .node {
cursor: pointer;
}
.dennisseah-orgchart .node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 1.5px;
}
.dennisseah-orgchart .node text {
font: 10px sans-serif;
}
.dennisseah-orgchart .link {
fill: none;
stroke: #ccc;
stroke-width: 1.5px;
}
.dennisseah-orgchart {
overflow: hidden;
}
</style>
<script id='sap-ui-bootstrap' type='text/javascript'
src='https://sapui5.hana.ondemand.com/resources/sap-ui-core.js'
data-sap-ui-theme='sap_bluecrystal'
data-sap-ui-libs='sap.ui.commons'></script>
<script>
$(function() {
sap.ui.core.Control.extend('dennisseah.OrgChart', {
metadata: {
properties: {
width: {type: 'int', defaultValue: 600},
height: {type: 'int', defaultValue: 500}
}
},
init : function() {
this.root = {};
},
setRoot : function(root) {
this.root = root;
},
renderer : function(oRm, oControl) {
oRm.write("<div");
oRm.writeControlData(oControl);
oRm.addClass("dennisseah-orgchart");
oRm.writeClasses();
oRm.write('>');
oRm.write("</div>");
},
onAfterRendering: function() {
var root = this.root;
var margin = {
top: 20,
right: 120,
bottom: 20,
left: 120
};
var width = this.getWidth() - margin.right - margin.left;
var height = this.getHeight() - margin.top - margin.bottom;
var i = 0,
duration = 750,
rectW = 60,
rectH = 30;
var tree = d3.layout.tree().nodeSize([70]);
var diagonal = d3.svg.diagonal()
.projection(function (d) {
return [d.x + rectW / 2, d.y + rectH / 2];
});
var svg = d3.select("#" + this.getId())
.append("svg")
.attr("width", this.getWidth())
.attr("height", this.getHeight())
.call(zm = d3.behavior.zoom().scaleExtent([1,3])
.on("zoom", redraw)).append("g")
.attr("transform", "translate(" + (this.getWidth() /2) + "," + 20 + ")");
zm.translate([50, 20]);
root.x0 = 0;
root.y0 = height / 2;
function collapse(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
root.children.forEach(collapse);
update(root);
d3.select("#" + this.getId()).style("height", "800px");
function update(source) {
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
nodes.forEach(function (d) {
d.y = d.depth * 180;
});
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.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function (d) {
return "translate(" + source.x0 + "," + source.y0 + ")";
})
.on("click", click);
nodeEnter.append("rect")
.attr("width", rectW)
.attr("height", rectH)
.attr("stroke", "black")
.attr("stroke-width", 1)
.style("fill", function (d) {
return d._children ? "lightsteelblue" : "#fff";
});
nodeEnter.append("text")
.attr("x", rectW / 2)
.attr("y", rectH / 2)
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function (d) {
return d.name;
});
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function (d) {
return "translate(" + d.x + "," + d.y + ")";
});
nodeUpdate.select("rect")
.attr("width", rectW)
.attr("height", rectH)
.attr("stroke", "black")
.attr("stroke-width", 1)
.style("fill", function (d) {
return d._children ? "lightsteelblue" : "#fff";
});
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("rect")
.attr("width", rectW)
.attr("height", rectH)
.attr("stroke", "black")
.attr("stroke-width", 1);
nodeExit.select("text");
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function (d) {
return d.target.id;
});
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("x", rectW / 2)
.attr("y", rectH / 2)
.attr("d", function (d) {
var o = {
x: source.x0,
y: source.y0
};
return diagonal({
source: o,
target: o
});
});
// 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 = {
x: source.x,
y: source.y
};
return diagonal({
source: o,
target: o
});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function (d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
//Redraw for zoom
function redraw() {
//console.log("here", d3.event.translate, d3.event.scale);
svg.attr("transform",
"translate(" + d3.event.translate + ")"
+ " scale(" + d3.event.scale + ")");
}
}
});
var org_chart = new dennisseah.OrgChart();
org_chart.setRoot({
name: 'Manager',
children: [
{name: 'Employee1'},
{name: 'Employee2'},
{name: 'Employee3'},
]
});
org_chart.placeAt('content');
});
</script>
</head>
<body class='sapUiBody'>
<div id='content'></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment