In GraphStream visualizations Graphs can be dense. The enableAutoLayout method gives a global visualization of the Graph and so zooming is needed. How to zoom into a 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);
I tried to find a way to zoom to the mouse cursor using the mouse wheel and stumbled across this thread, hoping to find an answer. I figured out how to zoom to the mouse eventually with GraphStream:
The zoom-factor for each mouse wheel rotation in my case is 1.25 (or 0.8 when zooming out). The code calculates the new center of the graph based on the original center of the graph, the clicked point in the graph, the zoom and eventually the ratio of Px to Gu which can be retrieved from the camera.
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);
}
});
From the official documentation at http://graphstream-project.org/doc/Tutorials/Graph-Visualisation:
You can also zoom in or out using:
view.getCamera().setViewPercent(0.5);This will zoom of 200% on the view center.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With