gephi java教程_gephi生成图(java版)

该博客展示了如何使用Java代码通过Gephi库进行图的生成和布局,特别是利用Yifan Hu算法进行百万级节点的布局。通过导入图形文件,处理并应用不同的布局算法,如ForceAtlas和YifanHuLayout,然后导出结果为GEXF文件。
摘要由CSDN通过智能技术生成

不说啥了,直接代码 ,对于图的生成,推荐使用组合算法,yifan hu 算法点生成图是百万级的

/*

Copyright 2008-2010 Gephi

Authors : Mathieu Bastian

Website : http://www.gephi.org

This file is part of Gephi.

Gephi is free software: you can redistribute it and/or modify

it under the terms of the GNU Affero General Public License as

published by the Free Software Foundation, either version 3 of the

License, or (at your option) any later version.

Gephi is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License

along with Gephi. If not, see .

*/

package org.gephi.toolkit.demos;

import java.io.File;

import java.io.IOException;

import java.util.concurrent.TimeUnit;

import org.gephi.graph.api.DirectedGraph;

import org.gephi.graph.api.GraphController;

import org.gephi.graph.api.GraphModel;

import org.gephi.io.exporter.api.ExportController;

import org.gephi.io.importer.api.Container;

import org.gephi.io.importer.api.EdgeDefault;

import org.gephi.io.importer.api.ImportController;

import org.gephi.io.processor.plugin.DefaultProcessor;

import org.gephi.layout.plugin.AutoLayout;

import org.gephi.layout.plugin.force.StepDisplacement;

import org.gephi.layout.plugin.force.yifanHu.YifanHuLayout;

import org.gephi.layout.plugin.forceAtlas.ForceAtlasLayout;

import org.gephi.project.api.ProjectController;

import org.gephi.project.api.Workspace;

import org.openide.util.Lookup;

/**

* This demo shows how to use the AutoLayout class to run layout

* programmatically.

*

* You can set a layout duration, and an execution ratio for several layout. For

* instance you set 0.8 for a Yifan Hu algorithm and 0.2 for Label Adjust. If

* execution time is 100 seconds, the first algorithm run for 80 seconds and the

* second for 20 seconds. It also allows to change property values dynamically

* (at a certain ratio or interpolated if values are numerical).

*

* @author Mathieu Bastian

*/

public class WithAutoLayout {

public void script() {

//Init a project - and therefore a workspace

//��ʼ��һ����Ŀ - ��һ��������

ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);

pc.newProject();

Workspace workspace = pc.getCurrentWorkspace();

//Append container to graph structure

//��������ͼ�νṹ

ImportController importController = Lookup.getDefault().lookup(ImportController.class);

//Import file

//�����ļ�

Container container;

try {

File file = new File(getClass().getResource("/org/gephi/toolkit/demos/resources/lesmiserables3582908092477373.gml").toURI());

container = importController.importFile(file);

container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED); //Force DIRECTED

container.setAllowAutoNode(false); //Don't create missing nodes

} catch (Exception ex) {

ex.printStackTrace();

return;

}

importController.process(container, new DefaultProcessor(), workspace);

//See if graph is well imported

//���ͼ���

GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();

DirectedGraph graph = graphModel.getDirectedGraph();

for (int i = 1;i <= graph.getNodeCount();i++) {

//System.out.println("Nodes: " + graph.getNode(i).getNodeData());

}

System.out.println("Edges: " + graph.getEdgeCount());

//Layout for 1 minute

//����1����

//^^

long time = 0;

if (graph.getEdgeCount() > 20000) {

time = (long) (((double) graph.getEdgeCount() / 80));

//System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);

}else

if (graph.getEdgeCount() > 12000) {

time = (long) (((double) graph.getEdgeCount() / 85));

//System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);

} else if (graph.getEdgeCount() > 8000) {

time = (long) (((double) graph.getEdgeCount() / 90));

//System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);

} else if (graph.getEdgeCount() > 5000) {

time = (long) (((double) graph.getEdgeCount() / 95));

//System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);

} else if (graph.getEdgeCount() > 100) {

time = (long) (((double) graph.getEdgeCount() / 100));

//System.out.println("log:huangzheng:"+DateUtil.time2str((int)System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss")+"当前生成图像所花费的时间:"+time);

} else {

time = 1;

}

AutoLayout autoLayout = new AutoLayout(time, TimeUnit.SECONDS);

autoLayout.setGraphModel(graphModel);

YifanHuLayout secondLayout = new YifanHuLayout(null, new StepDisplacement(1f));

ForceAtlasLayout firstLayout = new ForceAtlasLayout(null);

AutoLayout.DynamicProperty adjustBySizeProperty = AutoLayout.createDynamicProperty("forceAtlas.adjustSizes.name", Boolean.TRUE, 0.1f);//True after 10% of layout time

AutoLayout.DynamicProperty repulsionProperty = AutoLayout.createDynamicProperty("forceAtlas.repulsionStrength.name", new Double(500.), 0f);//500 for the complete period

autoLayout.addLayout(firstLayout , 0.5f, new AutoLayout.DynamicProperty[]{adjustBySizeProperty, repulsionProperty});

autoLayout.addLayout(secondLayout, 0.5f);

autoLayout.execute();

//^^

//Export

ExportController ec = Lookup.getDefault().lookup(ExportController.class);

try {

ec.exportFile(new File("WebRoot/WEB-INF/weiboGexf/io_gexf2.gexf"));

} catch (IOException ex) {

ex.printStackTrace();

}

}

public static void main(String[] args) {

WithAutoLayout autoLayout = new WithAutoLayout();

autoLayout.script();

// System.out.println();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值