图形流使用“始终开启”的自动布局。

huangapple go评论81阅读模式
英文:

graphstream using "always-on" autolayout

问题

我正在尝试使用GraphStream和我正在编写的小型编译器玩耍,以创建类似于以下内容的抽象语法树可视化:

     // ASTNode是AST树的根节点。鉴于该节点,这将在屏幕上显示AST。
     public static void visualize(ASTNode ast) throws IOException, URISyntaxException {
        Path file = Path.of(VisualizeAbstractSyntaxTree.class.getResource("graph.css").toURI());
        String css = Files.readString(file);
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        Graph graph = new SingleGraph("AST");
        graph.addAttribute("ui.stylesheet", css);
        construct(ast, "0", graph);  // 从AST根节点构造树。
        Viewer viewer = graph.display();
    }

运行该程序会显示自动定位效果。然而,当鼠标拖动一个节点时,其他节点保持静止。有没有一种简单的方法可以使其他节点也对鼠标拖动的节点做出反应(一起移动)?

理论上应该支持这一点,但我似乎找不到任何示例或API参考来实现这一点。

英文:

I was playing around with graphstream with a small compiler I'm writing to create an Abstract-Syntax-Tree visualization, like this:

     // ASTNode is the root to to the AST tree. Given that node, this just displays
     // the AST on-screen.
     public static void visualize(ASTNode ast) throws IOException, URISyntaxException {
        Path file = Path.of(VisualizeAbstractSyntaxTree.class.getResource("graph.css").toURI());
        String css = Files.readString(file);
        System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer");
        Graph graph = new SingleGraph("AST");
        graph.addAttribute("ui.stylesheet", css);
        construct(ast, "0", graph);  // construct the tree from the AST root node.
        Viewer viewer = graph.display();
    }

Running the program shows the auto positioning magic. However, when a node is dragged by the mouse the other nodes remain stationary. Is there an easy way to get the other nodes to react (also pulled) if a node is dragged with the mouse?

This must be supported, but I cannot seem to find any examples or API references to this?

答案1

得分: 3

我不知道您的函数背后的代码,但通常情况下,默认查看器会自动处理。您可以尝试通过以下方式强制启用自动布局:

viewer.enableAutoLayout();

您可以在网站上找到一些文档。

如果自动布局起初起作用,但突然停止,可能是布局算法的参数不适合您。布局算法被编写成在达到稳定点时停止,但您可以进行更改。

您只需定义一个您喜欢的新布局算法实例并使用它:

SpringBox l = new SpringBox();

然后,您可以定义参数,如力量或稳定点。约定是,值为0意味着控制布局的过程不会停止布局(因此不会考虑稳定性限制)。换句话说,布局将无限计算:

l.setStabilizationLimit(0);

但请记住,如果您想使用自己的布局算法实例,您需要在显示之前创建查看器。这意味着您需要构建自己的用户界面。这里是一个简单的示例,您可以在官方GitHub上找到更多示例:

SpringBox l = new SpringBox(); // 布局算法
l.setStabilizationLimit(0);

Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
viewer.enableAutoLayout(l); // 将布局算法添加到查看器

// 构建您的用户界面
add(viewer.addDefaultView(false), BorderLayout.CENTER); // 您的类应该扩展JFrame
setSize(800, 600);
setVisible(true);
英文:

I don't know the code behind your function but usually it's automatic with the default viewer. You can try to force the activation of the autolayout with :

viewer.enableAutoLayout();

You can find some documentation in the website.

If the autolayout works but just stop suddenly, it's perhaps the parameter of the layout algorithm that doesn't suit you. Layout algorithms are written to stop when they reach their stabilization point, but you can change this.

You just have to define a new instance of the layout algorithm that you prefer and use it :

SpringBox l = new SpringBox();

Then you can define the parameter, like the force or the stabilization point. The convention is that the value 0 means that the process controlling the layout will not stop the layout (will therefore not consider the stabilization limit). In other words the layout will compute endlessly. :

l.setStabilizationLimit(0);

But please keep in mind that if you want to use your instance of layout algorithm, you are going to create your viewer before the display. And that implies to build your own ui. Here an easy example, you can find more on the official github :

SpringBox l = new SpringBox(); // The layout algorithm
l.setStabilizationLimit(0);

Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_GUI_THREAD);
viewer.enableAutoLayout(l); // Add the layout algorithm to the viewer

// Build your UI
add(viewer.addDefaultView(false), BorderLayout.CENTER); // Your class should extends JFrame
setSize(800, 600);
setVisible(true);

huangapple
  • 本文由 发表于 2020年8月21日 03:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63511761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定