我工作的GraphStream图书馆为Java。 但是我在这里面临的一个问题。 我试图查询10000条记录具有数据库2列。 和图形创建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的。