OrgChart(组织机构图) - Flex
2013-06-26 08:33
148 查看
前段时间,用Flex写了个OrgChart,截图如下
iLog有个很不错的,可是收钱,799美刀。你可以看这里
http://visudemos.ilog.com/webdemos/orgchart/orgchart.html
。
俺农村来的,比较穷,最终还是自己“创造”了一个。
其实算法还是很就简单地,核心就是递归算法。
我写的那个是只能接受XML的。
核心代码:
Java代码
/**
* create nodes,
* calculate depth.
* */
private function _createSubNodes(data:ICollectionView, parentNode:IOrgChartNode):void{
for(var cursor:IViewCursor = data.createCursor(); !cursor.afterLast; cursor.moveNext()){
var node:IOrgChartNode = _createNode(cursor.current, parentNode);
if(_treeDataDesciptor.isBranch(cursor.current, data)
&& _treeDataDesciptor.getChildren(cursor.current, data).length != 0){
var __tmp:ICollectionView = _treeDataDesciptor.getChildren(cursor.current, data);
_createSubNodes(__tmp, node);
}
}
}
private var _maxX:Number=0;
/**
* Wrap data with IOrgChartNode
* */
private function _createNode(data:Object, parentNode:IOrgChartNode):IOrgChartNode{
var node:IOrgChartNode = new DefaultOrgChartNode();
node.addEventListener(MouseEvent.CLICK, nodeClick);
node.addEventListener(MouseEvent.MOUSE_OVER, nodeMouseOver);
node.addEventListener(MouseEvent.ROLL_OVER, nodeRollOver);
node.addEventListener(MouseEvent.MOUSE_OUT, nodeMouseOut);
node.parentNode = parentNode;
node.data = data;
node.width = hItemWidth;
node.height = hItemHeight;
//起始时,根节点在最左边
if(parentNode == null){
node.x = 0;
node.y = 0;
//_maxX = node.width + _horizonalSpacing;
}else{
if(node.previousSibling == null){
//与父节点在同一中轴线上
//trace(node);
node.x = parentNode.x + (parentNode.width - node.width)/2;
_maxX = node.x + node.width;
}else{
node.x = _maxX + _horizonalSpacing;
_maxX = node.x + node.width;
}
//移动父节点
updateParentNodePosition(node.parentNode );
node.y = parentNode.y + parentNode.height + _verticalSpacing;
}
_nodes.addItem(node);
return node;
}
/**
* 递归移动所有父节点的位置。
* */
private function updateParentNodePosition(node:IOrgChartNode):void{
if(node != null){
var subs:ArrayCollection = node.subNodes;
var lastChild:IOrgChartNode = subs.getItemAt(subs.length - 1 ) as IOrgChartNode;
var firstChild:IOrgChartNode = subs.getItemAt(0) as IOrgChartNode;
node.x = firstChild.x + ( lastChild.x - firstChild.x + lastChild.width - node.width) / 2;
//递归更新直到根节点
updateParentNodePosition(node.parentNode);
}
}
基本就是一个递归右移父节点的算法。
然后把所有的节点统一的Render就OK了。
iLog有个很不错的,可是收钱,799美刀。你可以看这里
http://visudemos.ilog.com/webdemos/orgchart/orgchart.html
。
俺农村来的,比较穷,最终还是自己“创造”了一个。
其实算法还是很就简单地,核心就是递归算法。
我写的那个是只能接受XML的。
核心代码:
Java代码
/**
* create nodes,
* calculate depth.
* */
private function _createSubNodes(data:ICollectionView, parentNode:IOrgChartNode):void{
for(var cursor:IViewCursor = data.createCursor(); !cursor.afterLast; cursor.moveNext()){
var node:IOrgChartNode = _createNode(cursor.current, parentNode);
if(_treeDataDesciptor.isBranch(cursor.current, data)
&& _treeDataDesciptor.getChildren(cursor.current, data).length != 0){
var __tmp:ICollectionView = _treeDataDesciptor.getChildren(cursor.current, data);
_createSubNodes(__tmp, node);
}
}
}
private var _maxX:Number=0;
/**
* Wrap data with IOrgChartNode
* */
private function _createNode(data:Object, parentNode:IOrgChartNode):IOrgChartNode{
var node:IOrgChartNode = new DefaultOrgChartNode();
node.addEventListener(MouseEvent.CLICK, nodeClick);
node.addEventListener(MouseEvent.MOUSE_OVER, nodeMouseOver);
node.addEventListener(MouseEvent.ROLL_OVER, nodeRollOver);
node.addEventListener(MouseEvent.MOUSE_OUT, nodeMouseOut);
node.parentNode = parentNode;
node.data = data;
node.width = hItemWidth;
node.height = hItemHeight;
//起始时,根节点在最左边
if(parentNode == null){
node.x = 0;
node.y = 0;
//_maxX = node.width + _horizonalSpacing;
}else{
if(node.previousSibling == null){
//与父节点在同一中轴线上
//trace(node);
node.x = parentNode.x + (parentNode.width - node.width)/2;
_maxX = node.x + node.width;
}else{
node.x = _maxX + _horizonalSpacing;
_maxX = node.x + node.width;
}
//移动父节点
updateParentNodePosition(node.parentNode );
node.y = parentNode.y + parentNode.height + _verticalSpacing;
}
_nodes.addItem(node);
return node;
}
/**
* 递归移动所有父节点的位置。
* */
private function updateParentNodePosition(node:IOrgChartNode):void{
if(node != null){
var subs:ArrayCollection = node.subNodes;
var lastChild:IOrgChartNode = subs.getItemAt(subs.length - 1 ) as IOrgChartNode;
var firstChild:IOrgChartNode = subs.getItemAt(0) as IOrgChartNode;
node.x = firstChild.x + ( lastChild.x - firstChild.x + lastChild.width - node.width) / 2;
//递归更新直到根节点
updateParentNodePosition(node.parentNode);
}
}
基本就是一个递归右移父节点的算法。
然后把所有的节点统一的Render就OK了。
相关文章推荐
- 通用权限管理系统组件 (GPM - General Permissions Manager) 中实现按部门组织机构设置权限
- 组织机构管理里只有“内部组织”会出现在即时通讯的目录树里(可供C#.NET源码下载学习)
- 利用AJAX+J2EE开发组织机构管理系统
- RDIFramework.NET 框架之组织机构权限设置
- 第4章 组织机构(部门)管理
- 基于DSL的组织机构模型 之一:预告
- MIS通用管理组件_部门(组织机构)
- JS验证身份证、营业执照、组织机构代码等
- 易协软件:组织机构模型之浅见
- 组织机构DSL之二:参考资料(ruby hacking guide )的阅读笔记
- Shiro基于组织机构的登录验证
- 身份证、组织机构代码及营业执照号校验函数【来自CSDN论坛】
- 实现全国组织机构代码校验
- 组织机构中校验码的计算方法
- 校验组织机构代码 合法性
- Xianfeng轻量级Java中间件平台:流水号管理、组织机构管理
- 组织机构代码校验码程序源代码
- 组织机构代码校验码生成算法(C#版)
- java 递归查询组织机构树
- 组织机构权限系统的实现(工作流)