如何放大 GraphStream View



在GraphStream可视化中,图形可以是密集的。enableAutoLayout方法提供了图形的全局可视化,因此需要进行缩放。如何放大GraphStream View?

Graph go=...;
Viewer viewer = new Viewer(go, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
viewer.enableAutoLayout();
View view = viewer.addDefaultView(false); 
swingNode.setContent((JComponent) view);

我试图找到一种使用鼠标滚轮缩放到鼠标光标的方法,偶然发现了这个线程,希望找到答案。我想出了如何使用GraphStream最终缩放到鼠标:

在我的例子中,每次鼠标滚轮旋转的缩放系数为 1.25(缩小时为 0.8(。该代码根据图形的原始中心、图形中的单击点、缩放以及最终可以从相机检索的 Px 与 Gu 的比率来计算图形的新中心。

final Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
viewer.enableAutoLayout();
final View view = viewer.addDefaultView(false);
view.getCamera().setViewPercent(1);
((Component) view).addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        e.consume();
        int i = e.getWheelRotation();
        double factor = Math.pow(1.25, i);
        Camera cam = view.getCamera();
        double zoom = cam.getViewPercent() * factor;
        Point2 pxCenter  = cam.transformGuToPx(cam.getViewCenter().x, cam.getViewCenter().y, 0);
        Point3 guClicked = cam.transformPxToGu(e.getX(), e.getY());
        double newRatioPx2Gu = cam.getMetrics().ratioPx2Gu/factor;
        double x = guClicked.x + (pxCenter.x - e.getX())/newRatioPx2Gu;
        double y = guClicked.y - (pxCenter.y - e.getY())/newRatioPx2Gu;
        cam.setViewCenter(x, y, 0);
        cam.setViewPercent(zoom);
    }
});

来自 http://graphstream-project.org/doc/Tutorials/Graph-Visualisation 的官方文档:

您还可以使用以下方法放大或缩小:

view.getCamera().setViewPercent(0.5);

这将在视图中心缩放 200%。

这里有一个简单的代码,向您展示如何缩放图表:

public class zoomGraph{
   public zoomGraph(){
   }
   //the methode that will zoom the graph
   public static void zoomGraphMouseWheelMoved(MouseWheelEvent mwe, ViewPanel view_panel){
        if (Event.ALT_MASK != 0) {            
            if (mwe.getWheelRotation() > 0) {
                double new_view_percent = view_panel.getCamera().getViewPercent() + 0.05;
                view_panel.getCamera().setViewPercent(new_view_percent);               
            } else if (mwe.getWheelRotation() < 0) {
                double current_view_percent = view_panel.getCamera().getViewPercent();
                if(current_view_percent > 0.05){
                    view_panel.getCamera().setViewPercent(current_view_percent - 0.05);                
                }
            }
        }                     
    }
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        
        frame.setBounds(0, 0, 700, 500);
        frame.setPreferredSize(new Dimension(700, 500));

        //Components
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout());
        frame.add(panel);
        //create a simple graph
        Graph graph = new SingleGraph("tuto_zoom", false, true);
        graph.addNode("node_1");
        graph.addNode("node_2");
        graph.addEdge("edge_1_2", "node_1","node_2");
        //show the graph in the panel
        Viewer viewer = new Viewer(graph, Viewer.ThreadingModel.GRAPH_IN_ANOTHER_THREAD);
        viewer.enableAutoLayout();
        ViewPanel view_panel = viewer.addDefaultView(false); 
        Rectangle rec = panel.getBounds();
        view_panel.setBounds(0, 0, rec.width, rec.height);
        view_panel.setPreferredSize(new Dimension(rec.width, rec.height));
        panel.add(view_panel);

        //add a mouse wheel listener to the ViewPanel for zooming the graph
        view_panel.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent mwe) {
                zoomGraph.zoomGraphMouseWheelMoved(mwe, view_panel);
            }
        });

        frame.setVisible(true);
    }
}

不应该是一个新的答案,而是对@MichaG的答案(https://stackoverflow.com/a/52929241/5036964(的改进。感谢您的出色工作!

在实现这一点时,我意识到,当图形旋转时,缩放中心是关闭的。下面是我的更新版本,在图形旋转时也有效:

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
    e.consume();
    int i = e.getWheelRotation();
    double factor = Math.pow(1.25, i);
    Camera cam = viewPanel.getCamera();
    double zoom = cam.getViewPercent() * factor;
    Point2 pxCenter  = cam.transformGuToPx(cam.getViewCenter().x, cam.getViewCenter().y, 0);
    Point3 guClicked = cam.transformPxToGu(e.getX(), e.getY());
    double newRatioPx2Gu = cam.getMetrics().ratioPx2Gu/factor;
    
    /* 
     * patch begins here.
     * the offset is calculated as before, but afterwards rotated according to 
     * the current rotation of the view.
     */
    Point2 offset = new Point2();
    offset.x = (pxCenter.x - e.getX()) / newRatioPx2Gu;
    offset.y = (pxCenter.y - e.getY()) / newRatioPx2Gu;
    Point2 rotatedOffset = rotateOffset(offset, -cam.getViewRotation());
    
    double x = guClicked.x + rotatedOffset.x;
    double y = guClicked.y - rotatedOffset.y;
    
    cam.setViewCenter(x, y, 0);
    cam.setViewPercent(zoom);
}
/**
 * Rotates a given offset by the supplied angle in degrees.
 * 
 * @param offset  the offset that needs to be rotated
 * @param degrees  the degree in angles of the rotation
 * @return  rotated offset
 */
private Point2 rotateOffset(Point2 offset, double degrees) {
    double sin = Math.sin(Math.toRadians(degrees));
    double cos = Math.cos(Math.toRadians(degrees));
    
    double rotOffsetX = offset.x * cos - offset.y * sin;
    double rotOffsetY = offset.x * sin + offset.y * cos;
    
    return new Point2(rotOffsetX, rotOffsetY);
}

相关内容

  • 没有找到相关文章

最新更新