Gephi生成图代码

来源:互联网 发布:青鸟软件开发培训 编辑:程序博客网 时间:2024/05/22 20:39
<BLOCKQUOTE>
003    
004    
005   
006    
007   
008    
009    
010    
011    
012不说啥了,直接代码  ,对于图的生成,推荐使用组合算法,yifan hu 算法点生成图是百万级的
013  
014   
015   
016   
017   
018  
019   
020  
021   
022 </BLOCKQUOTE>
023/*
024Copyright 2008-2010 Gephi
025Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
026Website : http://www.gephi.org
027  
028This file is part of Gephi.
029  
030Gephi is free software: you can redistribute it and/or modify
031it under the terms of the GNU Affero General Public License as
032published by the Free Software Foundation, either version 3 of the
033License, or (at your option) any later version.
034  
035Gephi is distributed in the hope that it will be useful,
036but WITHOUT ANY WARRANTY; without even the implied warranty of
037MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
038GNU Affero General Public License for more details.
039  
040You should have received a copy of the GNU Affero General Public License
041along with Gephi.  If not, see <http://www.gnu.org/licenses/>.
042*/
043package org.gephi.toolkit.demos;
044  
045import java.io.File;
046import java.io.IOException;
047import java.util.concurrent.TimeUnit;
048  
049import org.gephi.graph.api.DirectedGraph;
050import org.gephi.graph.api.GraphController;
051import org.gephi.graph.api.GraphModel;
052import org.gephi.io.exporter.api.ExportController;
053import org.gephi.io.importer.api.Container;
054import org.gephi.io.importer.api.EdgeDefault;
055import org.gephi.io.importer.api.ImportController;
056import org.gephi.io.processor.plugin.DefaultProcessor;
057import org.gephi.layout.plugin.AutoLayout;
058import org.gephi.layout.plugin.force.StepDisplacement;
059import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout;
060import org.gephi.layout.plugin.forceAtlas.ForceAtlasLayout;
061import org.gephi.project.api.ProjectController;
062import org.gephi.project.api.Workspace;
063import org.openide.util.Lookup;
064  
065/**
066 * This demo shows how to use the <code>AutoLayout</code> class to run layout
067 * programmatically.
068 * <p>
069 * You can set a layout duration, and an execution ratio for several layout. For
070 * instance you set 0.8 for a Yifan Hu algorithm and 0.2 for Label Adjust. If
071 * execution time is 100 seconds, the first algorithm run for 80 seconds and the
072 * second for 20 seconds. It also allows to change property values dynamically
073 * (at a certain ratio or interpolated if values are numerical).
074 *
075 * @author Mathieu Bastian
076 */
077public class WithAutoLayout {
078  
079    publicvoid script() {
080        //Init a project - and therefore a workspace
081        //��ʼ��һ����Ŀ - ��һ��������
082        ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
083        pc.newProject();
084        Workspace workspace = pc.getCurrentWorkspace();
085  
086        //Append container to graph structure
087        //��������ͼ�νṹ
088        ImportController importController = Lookup.getDefault().lookup(ImportController.class);
089        //Import file
090        //�����ļ�
091        Container container;
092        try{
093            File file =new File(getClass().getResource("/org/gephi/toolkit/demos/resources/lesmiserables3582908092477373.gml").toURI());
094            container = importController.importFile(file);
095            container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);  //Force DIRECTED
096            container.setAllowAutoNode(false); //Don't create missing nodes
097        }catch (Exception ex) {
098            ex.printStackTrace();
099            return;
100        }
101        importController.process(container,new DefaultProcessor(), workspace);
102  
103        //See if graph is well imported
104        //���ͼ���
105        GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
106        DirectedGraph graph = graphModel.getDirectedGraph();
107        for(int i = 1;i <= graph.getNodeCount();i++) {
108//          System.out.println("Nodes: " + graph.getNode(i).getNodeData());
109        }
110        System.out.println("Edges: "+ graph.getEdgeCount());
111          
112        //Layout for 1 minute
113        //����1����
114          
115          
116        //^^
117        longtime = 0;
118        if(graph.getEdgeCount() > 20000) {
119            time = (long) (((double) graph.getEdgeCount() / 80));
120//          System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);
121        }else
122        if(graph.getEdgeCount() > 12000) {
123            time = (long) (((double) graph.getEdgeCount() / 85));
124//          System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);
125        }else if(graph.getEdgeCount() > 8000) {
126            time = (long) (((double) graph.getEdgeCount() / 90));
127//          System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);
128        }else if(graph.getEdgeCount() > 5000) {
129            time = (long) (((double) graph.getEdgeCount() / 95));
130//          System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);
131        }else if(graph.getEdgeCount() > 100) {
132            time = (long) (((double) graph.getEdgeCount() / 100));
133//          System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);
134        }else {
135            time =1;
136        }
137        AutoLayout autoLayout =new AutoLayout(time, TimeUnit.SECONDS);
138        autoLayout.setGraphModel(graphModel);
139        YifanHuLayout secondLayout  =new YifanHuLayout(null,new StepDisplacement(1f));
140        ForceAtlasLayout firstLayout =new ForceAtlasLayout(null);
141        AutoLayout.DynamicProperty adjustBySizeProperty = AutoLayout.createDynamicProperty("forceAtlas.adjustSizes.name", Boolean.TRUE, 0.1f);//True after 10% of layout time
142        AutoLayout.DynamicProperty repulsionProperty = AutoLayout.createDynamicProperty("forceAtlas.repulsionStrength.name",new Double(500.), 0f);//500 for the complete period
143        autoLayout.addLayout(firstLayout ,0.5f, newAutoLayout.DynamicProperty[]{adjustBySizeProperty, repulsionProperty});
144        autoLayout.addLayout(secondLayout,0.5f);
145  
146         
147          
148          
149        autoLayout.execute();
150         //^^
151          
152          
153          
154        //Export
155        ExportController ec = Lookup.getDefault().lookup(ExportController.class);
156        try{
157            ec.exportFile(newFile("WebRoot/WEB-INF/weiboGexf/io_gexf2.gexf"));
158        }catch (IOException ex) {
159            ex.printStackTrace();
160        }
161    }
162      
163    publicstatic void main(String[] args) {
164        WithAutoLayout autoLayout =new WithAutoLayout();
165        autoLayout.script();
166//      System.out.println();
167    }
168}
0 0
原创粉丝点击