<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 | /* |
024 | Copyright 2008-2010 Gephi |
025 | Authors : Mathieu Bastian <mathieu.bastian@gephi.org> |
026 | Website : http://www.gephi.org |
027 | |
028 | This file is part of Gephi. |
029 | |
030 | Gephi is free software: you can redistribute it and/or modify |
031 | it under the terms of the GNU Affero General Public License as |
032 | published by the Free Software Foundation, either version 3 of the |
033 | License, or (at your option) any later version. |
034 | |
035 | Gephi is distributed in the hope that it will be useful, |
036 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
037 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
038 | GNU Affero General Public License for more details. |
039 | |
040 | You should have received a copy of the GNU Affero General Public License |
041 | along with Gephi. If not, see <http://www.gnu.org/licenses/>. |
042 | */ |
043 | package org.gephi.toolkit.demos; |
044 | |
045 | import java.io.File; |
046 | import java.io.IOException; |
047 | import java.util.concurrent.TimeUnit; |
048 | |
049 | import org.gephi.graph.api.DirectedGraph; |
050 | import org.gephi.graph.api.GraphController; |
051 | import org.gephi.graph.api.GraphModel; |
052 | import org.gephi.io.exporter.api.ExportController; |
053 | import org.gephi.io.importer.api.Container; |
054 | import org.gephi.io.importer.api.EdgeDefault; |
055 | import org.gephi.io.importer.api.ImportController; |
056 | import org.gephi.io.processor.plugin.DefaultProcessor; |
057 | import org.gephi.layout.plugin.AutoLayout; |
058 | import org.gephi.layout.plugin.force.StepDisplacement; |
059 | import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout; |
060 | import org.gephi.layout.plugin.forceAtlas.ForceAtlasLayout; |
061 | import org.gephi.project.api.ProjectController; |
062 | import org.gephi.project.api.Workspace; |
063 | import 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 | */ |
077 | public class WithAutoLayout { |
078 | |
079 | public void 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 | long time = 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, new AutoLayout.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( new File( "WebRoot/WEB-INF/weiboGexf/io_gexf2.gexf" )); |
158 | } catch (IOException ex) { |
159 | ex.printStackTrace(); |
160 | } |
161 | } |
162 | |
163 | public static void main(String[] args) { |
164 | WithAutoLayout autoLayout = new WithAutoLayout(); |
165 | autoLayout.script(); |
166 | // System.out.println(); |
167 | } |
168 | } |