使用Gephi生成網(wǎng)絡(luò)圖

前言

Gephi是一款開源免費(fèi)跨平臺基于JVM的復(fù)雜網(wǎng)絡(luò)分析軟件, 其主要用于各種網(wǎng)絡(luò)和復(fù)雜系統(tǒng), 特別是在處理網(wǎng)絡(luò)關(guān)系數(shù)據(jù)這方面很有優(yōu)勢,下面是兩個(gè)不錯(cuò)的例子

那么,我們拿到原始數(shù)據(jù)后, 怎么才能畫出這樣的圖表呢?

布局文件生成

通過上面兩個(gè)例子可以分析出,這類圖表可以通過 sigma.js 畫出來,但是插件本身并不提供預(yù)處理數(shù)據(jù)&&布局功能,所以在繪制圖表的時(shí)候需要有一份數(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)生斥力,邊給兩個(gè)節(jié)點(diǎn)提供拉力,通過多次迭代最后維持一個(gè)穩(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>

添加依賴完成之后,參考這個(gè) slide, 根據(jù)需求構(gòu)造一個(gè)有向圖,并調(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ù)文件后可以參考這個(gè) Online Demo 來繪制圖表。

參考資料

  1. http://gephi.github.io/
  2. http://www.slideshare.net/gephi/gephi-toolkit-tutorialtoolkit
  3. https://github.com/gephi/gephi/wiki/How-to-code-with-the-Toolkit

THE END

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容