summaryrefslogtreecommitdiff
path: root/src/Chapter4/graph/visualization/SimpleGraphViewer.java
blob: 7cb46e4f9129a189c1c0f3167c25389c6397637d (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package chapter4.graph.visualization;

import Chapter4.util.TweetFileToGraph;
import java.awt.Dimension;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.io.File;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;
import GraphElements.RetweetEdge;
import GraphElements.UserNode;
import edu.uci.ics.jung.algorithms.layout.KKLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.scoring.EigenvectorCentrality;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;

public class SimpleGraphViewer {
	public static void main(String[] args){
		
		File tweetFile;
		
		if(args.length > 0){
			tweetFile = new File(args[0]);
		}
		else{
			tweetFile = new File("synthetic_retweet_network.json");
		}
		
		DirectedGraph<UserNode, RetweetEdge> retweetGraph = TweetFileToGraph.getRetweetNetwork(tweetFile);
		
		/*
		 * Converts a node to its string representation
		 */
		Transformer<UserNode, String> stringer = new Transformer<UserNode, String>(){
			public String transform(UserNode n){
				return n.toString();
			}
		};
		
		/*
		 * Calculate the centrality
		 */
		//calculate the betweenness centrality
//		final InDegreeScorer<UserNode> centralityScore = new InDegreeScorer<UserNode>(retweetGraph);
//		final BetweennessCentrality<UserNode, RetweetEdge> centralityScore = new BetweennessCentrality<UserNode, RetweetEdge>(retweetGraph);
//		final PageRank<UserNode, RetweetEdge> centralityScore = new PageRank<UserNode, RetweetEdge>(retweetGraph, 0.85);
		final EigenvectorCentrality<UserNode, RetweetEdge> centralityScore = new EigenvectorCentrality<UserNode, RetweetEdge>(retweetGraph);
		centralityScore.evaluate();
		
		double centralityMax = 0.0f;
		for(UserNode node : retweetGraph.getVertices()){
			centralityMax = Math.max(centralityMax, centralityScore.getVertexScore(node));
		}
		final double centralityMaxFinal = centralityMax;
		
		/*
		 * Sizes a node by some centrality measure
		 */
		Transformer<UserNode, Shape> shaper = new Transformer<UserNode, Shape>(){
			public Shape transform(UserNode n){
				System.out.println("User: " + n.getUsername() + " Cent: " + centralityScore.getVertexScore(n) + " Max: " + centralityMaxFinal);
				double radius = 50 * (centralityScore.getVertexScore(n)) / centralityMaxFinal;
				radius = Math.max(radius, 5.0f);
				float fRadius = (float) radius;
				return new Ellipse2D.Float(-fRadius/2, -fRadius/2, fRadius, fRadius);
			}
		};
		
		Layout<UserNode, RetweetEdge> layout = new KKLayout<UserNode, RetweetEdge>(retweetGraph);
		layout.setSize(new Dimension(500, 500));
		
		BasicVisualizationServer<UserNode, RetweetEdge> vv = new BasicVisualizationServer<UserNode, RetweetEdge>(layout);
		vv.setPreferredSize(new Dimension(550, 550));
		vv.getRenderContext().setVertexLabelTransformer(stringer);
		vv.getRenderContext().setVertexShapeTransformer(shaper);
		
		JFrame jframe = new JFrame("Simple Graph View");
		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jframe.getContentPane().add(vv);
		jframe.pack();
		jframe.setVisible(true);
	}
}