前言
Gephi是一款開源免費(fèi)跨平臺基于JVM的復(fù)雜網(wǎng)絡(luò)分析軟件, 其主要用于各種網(wǎng)絡(luò)和復(fù)雜系統(tǒng), 特別是在處理網(wǎng)絡(luò)關(guān)系數(shù)據(jù)這方面很有優(yōu)勢,下面是兩個不錯的例子
那么,我們拿到原始數(shù)據(jù)后, 怎么才能畫出這樣的圖表呢蝌焚?
布局文件生成
通過上面兩個例子可以分析出,這類圖表可以通過 sigma.js 畫出來,但是插件本身并不提供預(yù)處理數(shù)據(jù)&&布局功能,所以在繪制圖表的時候需要有一份數(shù)據(jù)文件來詳細(xì)的表明節(jié)點(diǎn)名稱,顏色,大小,橫坐標(biāo), 縱坐標(biāo),邊的起始節(jié)點(diǎn)
,這類數(shù)據(jù)一般用 gexf(xml格式) 或者 json來表示.
生成gexf需要用到布局算法, 常見的有 Force-directed_graph_drawing 力導(dǎo)向算法, 算法的核心思想是節(jié)點(diǎn)之間產(chǎn)生斥力,邊給兩個節(jié)點(diǎn)提供拉力,通過多次迭代最后維持一個穩(wěn)定狀態(tài)
,手動實(shí)現(xiàn)布局算法還是有一些復(fù)雜度的,好在gephi-tookit組件提供了API來處理數(shù)據(jù), 首先在maven項(xiàng)目中加入gephi的倉庫和依賴
<repositories>
<repository>
<id>gephi-snapshots</id>
<name>Gephi Snapshots</name>
<url>http://nexus.gephi.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>gephi-releases</id>
<name>Gephi Releases</name>
<url>http://nexus.gephi.org/nexus/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.gephi</groupId>
<artifactId>gephi-toolkit</artifactId>
<version>0.8.2</version>
</dependency>
</dependencies>
添加依賴完成之后,參考這個 slide, 根據(jù)需求構(gòu)造一個有向圖,并調(diào)用布局算法, 最后導(dǎo)出成gexf和pdf文件
ProjectController pc = Lookup.getDefault().lookup(ProjectController.class);
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();
//Generate a new random graph into a container
Container container = Lookup.getDefault().lookup(ContainerFactory.class).newContainer();
GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel();
DirectedGraph graph = graphModel.getDirectedGraph();
Node n0 = graphModel.factory().newNode("n0");
n0.getNodeData().setLabel("n0");
Node n1 = graphModel.factory().newNode("n1");
n1.getNodeData().setLabel("n1");
Edge edge = graphModel.factory().newEdge(n0, n1, 1f, true);
graph.addNode(n0);
graph.addNode(n1);
graph.addEdge(edge);
for(int i = 0 ; i < 100; i++) {
Node ntmp = graphModel.factory().newNode("tmp" + i);
Edge edgetmp = graphModel.factory().newEdge(n0, ntmp, 1f, true);
graph.addNode(ntmp);
graph.addEdge(edgetmp);
}
System.out.println("Nodes: " + graph.getNodeCount());
System.out.println("Edges: " + graph.getEdgeCount());
//Layout for 15 seconds
AutoLayout autoLayout = new AutoLayout(20, TimeUnit.SECONDS);
autoLayout.setGraphModel(graphModel);
YifanHuLayout firstLayout = new YifanHuLayout(null, new StepDisplacement(1f));
ForceAtlasLayout secondLayout = 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.9f);
autoLayout.addLayout(secondLayout, 0.1f, new AutoLayout.DynamicProperty[]{adjustBySizeProperty, repulsionProperty});
autoLayout.execute();
//Export pdf & gexf
ExportController ec = Lookup.getDefault().lookup(ExportController.class);
try {
File pdfFile = new File("/tmp/data.pdf");
File gexfFile = new File("/tmp/data.gexf");
pdfFile.getParentFile().mkdirs();
gexfFile.getParentFile().mkdirs();
ec.exportFile(pdfFile);
ec.exportFile(gexfFile);
} catch (IOException ex) {
ex.printStackTrace();
}
圖表繪制
在得到數(shù)據(jù)文件后可以參考這個 Online Demo 來繪制圖表岸夯。
參考資料
- http://gephi.github.io/
- http://www.slideshare.net/gephi/gephi-toolkit-tutorialtoolkit
- https://github.com/gephi/gephi/wiki/How-to-code-with-the-Toolkit
THE END