Organization Chart using Visualforce and Google Visualization API
1:42 PM
The below diagram is what we are going to build. This hierarchy is based upon the manager field in the user object. The below example is NOT based upon the role hierarchy. Anyhow, you can modify the code below to fit the role hierarchy.
Visualforce Page:
Visualforce Page:
<apex:page controller="orgchart" showHeader="false">
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['orgchart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('string', 'Manager');
data.addColumn('string', 'ToolTip');
data.addRows([
{!userdata}
]);
var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
chart.draw(data, {allowHtml:true});
}
</script>
</head>
<body>
<div id='chart_div' style="font-size:18px;"></div>
</body>
</html></apex:page>
Apex Class
public class orgchart {
Public String getuserdata()
{
List<User> allusers = [Select Name,Manager.Name from User where IsActive = TRUE and ManagerId != NULL];
String datastr = '';
for (integer i=0;i<allusers.size();i++)
{
datastr = datastr + '[\'';
datastr += allusers[i].Name;
datastr+= '\',\'';
datastr+= allusers[i].Manager.Name;
datastr+='\',\'\'],';
}
datastr = datastr + '';
return datastr;
}
}
9 comments
This is nice, how can we modify this so the chart fits the hierarchy ?
ReplyDeleteHo to do a multi-level hierarchy? How many levels are possible?
ReplyDeleteI think there is no limit on the level of hierarchy, you can just keep adding datarows and the hierarchy expands
ReplyDeleteThis will not work, until we need to remove the comma at the end. Include this line after the for loop ends.
ReplyDelete........
........
datastr = datastr.substring(0, datastr.length() -1);
datastr = datastr + '';
return datastr;
Thanks for pointing that out Viswa. Also, it looks like a single datastr variable can display a maximum of 50 separate hierarchy divisions..
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks,This is nice information about organizations.
ReplyDeleteA big thank you for your blog article.Organization charts definitely help to structure the employees and work flow environment.
I am getting this error System.StringException: Ending position out of bounds: -1
ReplyDeletecan any one why it is coming
I used the same code but i didnt got any output.can any one help
ReplyDelete