zoukankan      html  css  js  c++  java
  • 第归方法创建树形图

    1)TreeGraph.js文件内容如下:

    function treeMenu(a){
        //列表map形式
        this.tree=a||[];
        this.groups={};
    	//存放id与对应的name映射
    	this.nameMap={}
    	//得到每个点对应的层次,为了后期进行布局
    	this.levelMap={}
    	//样式设计
    	this.style={"symbolSize":[60,50,40,30,20],"value":[8,6,4,2,1]}
    };
    treeMenu.prototype={
        init:function(pid){
            this.group();
    		this.MapNamebyId();
    		this.setIdLevel(pid);
            return this.rescusive(pid);
        },
        group:function(){
            for(var i=0;i<this.tree.length;i++){
                //存在该grops则直接添加
                if(this.groups[this.tree[i].pId]){
                    this.groups[this.tree[i].pId].push(this.tree[i]);
                }else{
                    this.groups[this.tree[i].pId]=[];
                    this.groups[this.tree[i].pId].push(this.tree[i]);
                }
            }
        },
    	//得到每个点的层次
    	setIdLevel:function(pid){
    		var level=1;
    		this.levelMap[pid]=level;
    		var gs=this.groups[pid];
    		//str=JSON.stringify(gs)
    		//alert("json:"+str)
    		var temp=[]
    		while(gs){
    			level++;
    			if(gs==null||gs==undefined||gs.length==0)
    				break;
    			temp=[]
    			for(var i=0;i<gs.length;i++){
    				var myid=gs[i]["id"];
    				this.levelMap[myid]=level;
    				subgs=this.groups[myid];
    				if(subgs instanceof Array &&subgs!=null){
    				for(var j=0;j<subgs.length;j++){
    				temp.push(subgs[j]);
    				}
    				}
    			}
    			gs=temp;
    		}
    		
    	},
    	//根据所在层次设计不同大小的样式
    	getStyleById:function(id){
    		var level=this.levelMap[id]
    		if(level>=5)
    			level=5;
    		var symbolize=0
    		var value=0
    		symbolize=this.style['symbolSize'][level-1]
    		value=this.style['value'][level-1]
    		var styleValue={}
    		styleValue['symbolSize']=symbolize
    		styleValue['value']=value
    		return styleValue
    	},
        MapNamebyId:function(){
    		for(var i=0;i<this.tree.length;i++){
    			map=this.tree[i]
    			this.nameMap[map["id"]]=map["name"]
    		}
    	},
    	//设置节点属性
    	setNode:function(node,name,symbolize,value,children){
    		    node['name']=name;
    			node['symbolSize']=symbolize;
    			node['value']=value
    			node['children']=children
    			return node;
    	},
    	rescusive:function (number){//这里是构建数据源的重点
    		var data=[]
    		var node={}
    		var styleValue={}
    		//某个节点下的子节点
    		var a=this.groups[number];
    		var nodeName=this.nameMap[number];
    		if(a==null||a==undefined){
    			styleValue=this.getStyleById(number)
    			//设置节点
    			this.setNode(node,nodeName,styleValue['symbolSize'],styleValue['value'],[])
    			return node;
    		}
    		for(var i=0;i<a.length;i++){
    			children=this.rescusive(a[i].id);
    			data.push(children);
    		}
    		styleValue=this.getStyleById(number)
    		this.setNode(node,nodeName,styleValue['symbolSize'],styleValue['value'],data)
    		return node;
    	},
    	//创建组织结构图
    	createTreeVisual:function(myChart,title,data){
    		var option = {
            title : {
            text: title
           },
            tooltip : {
            trigger: 'item',
            formatter: "{b}"
            },
          toolbox: {
            show : true,
            feature : {
                saveAsImage : {show: true}
             }
          },
          calculable : false,
           series : [
             {
                name:'树图',
                type:'tree',
                orient: 'horizontal',  // vertical horizontal
                rootLocation: {x: 100, y: '60%'}, // 根节点位置  {x: 'center',y: 10}
                nodePadding: 20,
                symbol: 'circle',
                data:data
            }]//series
    	}	
         myChart.setOption(option);
    	}
    }
    //得到数据
    function getData(zNodes){
    	var mytree=new treeMenu(zNodes)
    	treeData=mytree.init(0)
    	data=[]
    	data.push(treeData)
    	return data;
    	//str=JSON.stringify(menu);
    	//alert("responsing json:"+str)
    }
    function createTreeV(mychart,title,znodes){
    	var mytree=new treeMenu(znodes)
    	treeData=mytree.init(1)
    	data=[]
    	data.push(treeData)
    	mytree.createTreeVisual(myChart,title,data)
    }


    ---------------------------------
    <html> <head> <meta charset="UTF-8">
    <title>社交网络</title> </style> <script src="echarts-2.x.js"></script> <script src="TreeGraph.js"></script> </head>
    <body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div style="padding:20px;100%;height:100%;"> <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM -->
    <div id="main" style=" 1104px;height:464px;"> </div> <div id='main_1' style="position: relative;height:15px; 100%;color:#A52A2A"></div> </div> <script type="text/javascript">
    //data //data=createDatabyHand()
    var zNodes=[ {id:1,pId:0,name:"董事长"}, {id:11,pId:1,name:"经理"}, {id:12,pId:1,name:"副总"}, {id:13,pId:1,name:"秘书"}, {id:16,pId:11,name:"财务经理"}, {id:27,pId:11,name:"人事经理"}, {id:18,pId:12,name:"HR"}, ]
    data=getData(zNodes) var myChart = echarts.init(document.getElementById('main'), 'macarons'); createTreeV(myChart,"人事架构图",zNodes) </script> </body> </html>

    --------------------- 作者:xqhadoop 来源:CSDN 原文:https://blog.csdn.net/xqhadoop/article/details/72518107?utm_source=copy 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    JS对象、包装类
    js刷题网站
    typeof 返回的数据类型
    一文讲懂什么是函数柯里化,柯里化的目的及其代码实现
    JS 中深拷贝的几种实现方法
    JavaScript 开发的45个经典技巧
    JavaScript中的delete操作符
    IE下iframe不能正常加载,显示空白
    使用Costura.Fody插件将自己写的程序打包成一个可以独立运行的EXE文件
    list获取所有上级
  • 原文地址:https://www.cnblogs.com/sdlzspl/p/9774204.html
Copyright © 2011-2022 走看看