Gephi可視化(一)——使用Gephi Toolkit建立Gephi應用

  在Prefuse上摸打滾爬了一段時間,發現其和善可親,容易上手。可是往往在打開gephi,導入數據再運行時,總仍是在內心暗自讚歎gephi的絢爛之極,無與匹敵,固然,gephi也有本身的缺陷,可是gephi是一個在發展的、進步的、不斷壯大的開源軟件。縱使她曾經那般傷我,我還是對其依依不捨,好吧,我已經病入膏肓。。。。。html

 

  言歸正傳,Gephi是一款開源免費跨平臺基於JVM的複雜網絡分析軟件, 其主要用於各類網絡和複雜系統,動態和分層圖的交互可視化與探測開源工具。可用做:探索性數據分析,連接分析,社交網絡分析,生物網絡分析等。由於Gephi是開源軟件,因此能夠獲取其源碼,容許開發者去擴展和重複使用。java

Gephi開發平臺:Netbeans平臺(由於以前對於這句話理解不深,在eclipse環境下浪費了很多時間)node

開發語言:Javagit

可視化引擎:OpenGLgithub

Gephi官網:    http://gephi.github.io/算法

Gephi的視頻介紹:   http://v.youku.com/v_show/id_XMjU5MDUwMjg4.html編程

Gephi API幫助文檔:https://gephi.org/docs/api/api

Gephi Toolkit:   http://gephi.github.io/toolkit/微信

Gephi 論壇:     https://forum.gephi.org/網絡

 

  貼上gephi作的兩張圖:

                                                                           

 

  如今,咱們就深刻Gephi,好好了解下這個strong visual tool,此次咱們不走源碼,做爲過來人,我仍是想提醒下,若是java功底不是很紮實或是不太熟悉Netbeans開發平臺以及Swing編程知識的能夠走曲線,接近Gephi,不然會讓本身焦頭爛額,越陷越深,不可自拔^_^。下面經過一個實例,講解若是新建一個Gephi實例,進行可視化展現:

  (1)建立一個project、一個workspace,這是必作的工做,是進行後續操做的前提:

1
2
3
4
//Init a project - and therefore a workspace
ProjectController pc = Lookup.getDefault().lookup(ProjectController. class );
pc.newProject();
Workspace workspace = pc.getCurrentWorkspace();

  

  (2)獲得該空間的各個模型以及控制器,方便後面使用:

1
2
3
4
5
6
7
//Get models and controllers for this new workspace - will be useful later
AttributeModel attributeModel = Lookup.getDefault().lookup(AttributeController. class ).getModel();
GraphModel graphModel = Lookup.getDefault().lookup(GraphController. class ).getModel();
PreviewModel model = Lookup.getDefault().lookup(PreviewController. class ).getModel();
ImportController importController = Lookup.getDefault().lookup(ImportController. class );
FilterController filterController = Lookup.getDefault().lookup(FilterController. class );
RankingController rankingController = Lookup.getDefault().lookup(RankingController. class );

  

  (3)導入數據,使用Container接收,並將數據導入到空間中:

1
2
3
4
5
6
7
8
9
10
11
12
//Import file      
Container container;
try  {
     File file =  new  File(getClass().getResource( "/org/gephi/toolkit/demos/resources/polblogs.gml" ).toURI());
     container = importController.importFile(file);
     container.getLoader().setEdgeDefault(EdgeDefault.DIRECTED);    //Force DIRECTED
catch  (Exception ex) {
     ex.printStackTrace();
     return ;
}       
//Append imported data to GraphAPI
importController.process(container,  new  DefaultProcessor(), workspace);

  

  (4)經過打印信息,驗證圖形數據是否導入成功:

1
2
3
4
//See if graph is well imported
DirectedGraph graph = graphModel.getDirectedGraph();
System.out.println( "Nodes: "  + graph.getNodeCount());
System.out.println( "Edges: "  + graph.getEdgeCount());

 

  (5)對圖形數據進行過濾操做:

1
2
3
4
5
6
7
//Filter     
DegreeRangeFilter degreeFilter =  new  DegreeRangeFilter();
degreeFilter.init(graph);
degreeFilter.setRange( new  Range( 30 , Integer.MAX_VALUE));      //Remove nodes with degree < 30
Query query = filterController.createQuery(degreeFilter);
GraphView view = filterController.filter(query);
graphModel.setVisibleView(view);     //Set the filter result as the visible view

  

  (6)經過打印信息,驗證過濾操做是否起做用:

1
2
3
4
//See visible graph stats
UndirectedGraph graphVisible = graphModel.getUndirectedGraphVisible();
System.out.println( "Nodes: "  + graphVisible.getNodeCount());
System.out.println( "Edges: "  + graphVisible.getEdgeCount());

  

  (7)執行YifanHuLayout佈局算法:

1
2
3
4
5
6
7
8
9
10
11
//Run YifanHuLayout for 100 passes - The layout always takes the current visible view
YifanHuLayout layout =  new  YifanHuLayout( null new  StepDisplacement(1f));
layout.setGraphModel(graphModel);
layout.resetPropertiesValues();
layout.setOptimalDistance(200f);
layout.initAlgo();
  
for  ( int  i =  0 ; i <  100  && layout.canAlgo(); i++) {
     layout.goAlgo();
}
layout.endAlgo();

  

  (8)根據節點度值分配節點顏色:

1
2
3
4
5
//Rank color by Degree
Ranking degreeRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, Ranking.DEGREE_RANKING);
AbstractColorTransformer colorTransformer = (AbstractColorTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_COLOR);
colorTransformer.setColors( new  Color[]{ new  Color( 0xFEF0D9 ),  new  Color( 0xB30000 )});
rankingController.transform(degreeRanking,colorTransformer);

  

 

  (9)分配節點大小:

1
2
3
4
5
6
7
//Rank size by centrality
AttributeColumn centralityColumn = attributeModel.getNodeTable().getColumn(GraphDistance.BETWEENNESS);
Ranking centralityRanking = rankingController.getModel().getRanking(Ranking.NODE_ELEMENT, centralityColumn.getId());
AbstractSizeTransformer sizeTransformer = (AbstractSizeTransformer) rankingController.getModel().getTransformer(Ranking.NODE_ELEMENT, Transformer.RENDERABLE_SIZE);
sizeTransformer.setMinSize( 3 );
sizeTransformer.setMaxSize( 10 );
rankingController.transform(centralityRanking,sizeTransformer);

  

  (10)預覽效果配置,並呈現到Display上,添加到JFrame,進行界面話展現:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Preview
PreviewController previewController = Lookup.getDefault().lookup(PreviewController. class );
         model.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE);
model.getProperties().putValue(PreviewProperty.EDGE_COLOR,  new  EdgeColor(Color.GRAY));
model.getProperties().putValue(PreviewProperty.EDGE_THICKNESS,  new  Float( 0 .1f));
model.getProperties().putValue(PreviewProperty.NODE_LABEL_FONT, model.getProperties().getFontValue(PreviewProperty.NODE_LABEL_FONT).deriveFont( 8 ));
previewController.refreshPreview();
          
//New Processing target, get the PApplet
ProcessingTarget target = (ProcessingTarget) previewController.getRenderTarget(RenderTarget.PROCESSING_TARGET);
PApplet applet = target.getApplet();
applet.init();
//Refresh the preview and reset the zoom
previewController.render(target);
target.refresh();
target.resetZoom();
//Add the applet to a JFrame and display
JFrame frame =  new  JFrame( "Test Preview" );
frame.setLayout( new  BorderLayout());
         
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(applet, BorderLayout.CENTER);
         
frame.pack();
frame.setVisible( true );

 

  控制檯打印信息爲:

1
2
3
4
5
6
# Nodes loaded: 1490
# Edges loaded: 19025
Nodes: 1490
Edges: 19025
Nodes: 397
Edges: 10599

  執行的結果以下圖所示:

瞭解Prefuse的API以及建立一個Prefuse應用的思路詳見《漫談可視化Prefuse(二)---一分鐘學會Prefuse》,再回首看Gephi Toolkit建立gephi應用的思路也是一模一樣,後續有時間將繼續Gephi Toolkit之旅!

若是以爲有用,歡迎點贊^_^!也歡迎加入公告欄的可視化羣。

本文連接:《Gephi可視化(一)——使用Gephi Toolkit建立Gephi應用

 

 

友情贊助

 

若是你以爲博主的文章對你那麼一點小幫助,恰巧你又有想打賞博主的小衝動,那麼事不宜遲,趕忙掃一掃,小額地贊助下,攢個奶粉錢,也是讓博主有動力繼續努力,寫出更好的文章^^。

 

    1. 支付寶                          2. 微信

 

                      

相關文章
相關標籤/搜索