我正在使用GraphStream Java库。但是我在这里面临一个问题。
我试图从数据库查询具有2列的10,000条记录。该图为每个行创建2个节点和一个边。
但是,当我尝试运行该程序时,该图看起来非常复杂。见下图。
这是我的代码:
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;}");
}
}
谁能告诉我如何为该图提供更好的可视化。或我必须使用其他可用图形。我正在使用GraphStream的非常基本的example。
最佳答案
从ViewPanel
获取对Camera
的引用,并使用它来设置缩放百分比和中心。
viewPanel.getCamera().setViewPercent(0.25);
viewPanel.getCamera().setViewCenter(0, 0, 0);
一旦选择任何节点或
viewPanel
获得focus,viewPanel.requestFocusInWindow();
使用←,↑,→或↓键进行平移。
使用向上翻页和向下翻页键进行缩放。