如何放大GraphStream视图?

时间:2017-06-21 12:14:42

标签: zoom zooming graphstream

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

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

3 个答案:

答案 0 :(得分:4)

我试图找到一种使用鼠标滚轮缩放到鼠标光标的方法,偶然发现了该线程,希望找到答案。我想出了最终使用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);
    }
});

答案 1 :(得分:1)

以下简单的代码向您展示如何缩放图形:

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);
    }

}

答案 2 :(得分:0)

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

  

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

     

view.getCamera().setViewPercent(0.5);

     

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