gpt4 book ai didi

java - GraphStream 中更具可读性的图形 - JAVA

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:56:18 26 4
gpt4 key购买 nike

我正在使用 GraphStream 处理应用程序 Library .到目前为止我已经实现了Dijkstra's Shortest Path Algorithm . 我的图表工作正常,但图表的可读性不是我所期望的。这是图形的屏幕截图: enter image description here

如您所见,由于大量边缘交叉,图形不可读。有什么方法可以使我的图表更具可读性。我在学习Graph Stream Generators 但我不认为它们对我来说是最好的。所以我正在寻找我的问题的准确解决方案。在上图中,图表有 70% 的可读性,但可以有更多的节点和更多的边,在那种情况下图表是完全不可读的。到目前为止,这是我的代码,供你们了解我在做什么。

public class GraphTest {

Connection conn = null;
SingleGraph graph;
Statement stmt = null;
JLabel label;
JButton show_graph;
JTextField enter_numbers;
int i = 0;
double zoomLevel = 1.0;
String result, result2;
ImageIcon loading;

public static void main(String[] args) throws ClassNotFoundException, SQLException {
GraphTest graphTest = new GraphTest();
graphTest.createConnection();
}

public GraphTest() 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(1300, 600);
}
};
panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));

graph = new SingleGraph("Tutorial", false, true);
Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);

final ViewPanel viewPanel = viewer.addDefaultView(false);
viewer.enableAutoLayout();

graph.setAutoCreate(true);
graph.setStrict(false);
graph.addAttribute("ui.quality");
graph.addAttribute("ui.antialias");

label = new JLabel("Enter Numbers : ");
enter_numbers = new JTextField(15);
Font bigFont = enter_numbers.getFont().deriveFont(Font.PLAIN, 17f);
enter_numbers.setFont(bigFont);
show_graph = new JButton("SHOW GRAPH");

viewPanel.add(label);
viewPanel.add(enter_numbers);
viewPanel.add(show_graph);
show_graph.addActionListener(showGraphListener);

panel.add(viewPanel);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

viewPanel.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() == -1) {
zoomLevel = zoomLevel - 0.1;
if (zoomLevel < 0.1) {
zoomLevel = 0.1;
}
viewPanel.getCamera().setViewPercent(zoomLevel);
}
if (e.getWheelRotation() == 1) {
zoomLevel = zoomLevel + 0.1;
viewPanel.getCamera().setViewPercent(zoomLevel);
}
}
});
}

private Connection createConnection() throws ClassNotFoundException, SQLException {
Class.forName("org.h2.Driver");
conn = DriverManager.getConnection("jdbc:h2:file:G:/hs_data/h2_db/test", "sa", "sa");
return conn;
}

ActionListener showGraphListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user_input = enter_numbers.getText();
if (user_input == null || user_input.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter atleast two numbers with comma seperated");
} else {
ArrayList<String> items = new ArrayList<String>(Arrays.asList(user_input.split("\\s*,\\s*")));
try {
showGraph(items);
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
};

private void showGraph(ArrayList<String> items) throws SQLException {

stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ANUMBER,BNUMBER FROM CDR LIMIT 4500");
while (rs.next()) {
result = rs.getString("ANUMBER");
result2 = rs.getString("BNUMBER");
graph.addNode(result);
graph.addNode(result2);
i++;
graph.addEdge("String" + i, result, result2);
for (Node node : graph) {
node.addAttribute("ui.hide");
}
for (Edge edge : graph.getEachEdge()) {
edge.addAttribute("ui.hide");
}
}
conn.close();

Dijkstra dijkstra = new Dijkstra(Dijkstra.Element.EDGE, null, null);
dijkstra.init(graph);

ArrayList<String> rl = new ArrayList<String>();
ArrayList<String> lr = new ArrayList<String>();
rl = items;

lr.addAll(rl);
Collections.reverse(lr);
for (String anumber : rl) {
lr.remove(lr.size() - 1);
for (String bnumber : lr) {

dijkstra.setSource(graph.getNode(anumber));
dijkstra.compute();

for (Node node : dijkstra.getPathNodes(graph.getNode(bnumber))) {
node.addAttribute("ui.style", "fill-color: blue;");
node.addAttribute("ui.label", node.getId());
node.removeAttribute("ui.hide");
}

for (Edge edge : dijkstra.getPathEdges(graph.getNode(bnumber))) {
edge.addAttribute("ui.style", "fill-color: red;");
edge.removeAttribute("ui.hide");
}

graph.getNode(anumber).addAttribute("ui.style", "fill-color: green;");
graph.getNode(anumber).addAttribute("ui.style", "size: 16px;");
graph.getNode(bnumber).addAttribute("ui.style", "fill-color: green;");
graph.getNode(bnumber).addAttribute("ui.style", "size: 16px;");


}
}
dijkstra.clear();
}
}

最佳答案

您需要试验您的Viewer 使用的布局算法。您可以通过将具体的 Layout 传递给 enableAutoLayout() 来指定布局:SpringBox 是默认设置,但 LinLog可能值得一试。另见 list of attributes with a predefined meaning for the layout algorithms ,以及显示的示例 herehere .

关于java - GraphStream 中更具可读性的图形 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344610/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com