Better graph visualization in GraphStream - JAVA

2019-08-07 04:14发布

问题:

I am working on GraphStream Library for java. But I am facing a problem here. I tried to query 10,000 records having 2 columns from database. And the graph create 2 nodes and one edge for each row. But when I try to run the program, the graph looks very complicated. See the below image.

Here is my code:

public class GraphExplore {
    static Connection conn2;
    static String result, result2;
    static int totalRows, i;

    public static void main(String args[]) throws SQLException {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    showData();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static void showData() throws SQLException {

        JFrame frame = new JFrame("GRAPH");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        @SuppressWarnings("serial")
        JPanel panel = new JPanel(new GridLayout()) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));

        Graph graph = new SingleGraph("Tutorial", false, true);

        try {
            Class.forName("org.h2.Driver");
            conn2 = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa");

        } catch (Exception e) {
            e.printStackTrace();
        }
        Statement stmt2 = conn2.createStatement();
        ResultSet rs = stmt2.executeQuery("SELECT count(*) FROM cdr");
        while (rs.next()) {
            totalRows = rs.getInt(1);
        }
        ResultSet rs2 = stmt2.executeQuery("SELECT ANUMBER,BNUMBER FROM CDR LIMIT 1000");
        while (rs2.next()) {
            result = rs2.getString("ANUMBER");
            result2 = rs2.getString("BNUMBER");
            graph.addNode(result);
            graph.addNode(result2);
            for (i = 0; i < 1000; i++)
                graph.addEdge("string" + i, result, result2);
        }

        graph.setAutoCreate(true);
        graph.setStrict(false);
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
        ViewPanel viewPanel = viewer.addDefaultView(false);
        viewer.enableAutoLayout();
        panel.add(viewPanel);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        for (Node node : graph) {
            node.addAttribute("ui.label", node.getId());
        }

        // graph.addAttribute("ui.stylesheet", "graph { fill-color: red; }");text-mode:
        // hidden;
        graph.addAttribute("ui.stylesheet", "node {size:12px;fill-color:#ff0000;}");
        graph.addAttribute("ui.stylesheet", "edge { shape:angle ;fill-color:#222;}");

    }

}

Can anyone tell me, how to give better visualization to this graph. OR I have to use some other graphs available. I am using the very basic example of GraphStream.

回答1:

From the ViewPanel, get a reference to the Camera and use it to set the zoom percentage and center.

viewPanel.getCamera().setViewPercent(0.25);
viewPanel.getCamera().setViewCenter(0, 0, 0);

Once any node is selected or the viewPanel gains focus,

viewPanel.requestFocusInWindow();
  • Use the , , or keys to pan.

  • Use the page up and page down keys to zoom.