コード例 #1
0
  // method for flow graph visualization
  public static void visualizeGraph(int[][] matrix, int N, int k) {
    Graph<Integer, String> graph = new DirectedSparseGraph<Integer, String>();

    for (int i = 0; i < matrix.length; i++) {
      graph.addVertex((Integer) i);
    }

    for (int i = 0; i < matrix.length; i++) {
      for (int j = 0; j < matrix[i].length; j++) {
        // only select edge that has flow bigger than 0
        if (matrix[i][j] > 0) {
          graph.addEdge(i + "->" + j, i, j, EdgeType.DIRECTED);
        }
      }
    }

    Layout<Integer, String> layout = new CircleLayout<Integer, String>(graph);
    layout.setSize(new Dimension(800, 800));

    BasicVisualizationServer<Integer, String> vv =
        new BasicVisualizationServer<Integer, String>(layout);
    Transformer<Integer, Paint> vertexPaint =
        new Transformer<Integer, Paint>() {
          public Paint transform(Integer i) {
            return Color.YELLOW;
          }
        };

    vv.setPreferredSize(new Dimension(800, 800));
    vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Integer>());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);

    JFrame frame = new JFrame("Network Visualization - N = " + N + ", k = " + k);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(vv);
    frame.pack();
    frame.setVisible(true);
  }
コード例 #2
0
  public static void main(String[] args) {

    File tweetFile;

    if (args.length > 0) {
      tweetFile = new File(args[0]);
    } else {
      tweetFile =
          new File(
              "C:\\Users\\IBM_ADMIN\\Desktop\\Twitter\\TwitterDataAnalytics\\TwitterDataAnalytics\\owssad.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: "******" 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);
  }