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 retweetGraph = TweetFileToGraph.getRetweetNetwork(tweetFile); /* * Converts a node to its string representation */ Transformer stringer = new Transformer(){ public String transform(UserNode n){ return n.toString(); } }; /* * Calculate the centrality */ //calculate the betweenness centrality // final InDegreeScorer centralityScore = new InDegreeScorer(retweetGraph); // final BetweennessCentrality centralityScore = new BetweennessCentrality(retweetGraph); // final PageRank centralityScore = new PageRank(retweetGraph, 0.85); final EigenvectorCentrality centralityScore = new EigenvectorCentrality(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 shaper = new Transformer(){ 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 layout = new KKLayout(retweetGraph); layout.setSize(new Dimension(500, 500)); BasicVisualizationServer vv = new BasicVisualizationServer(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); } }