2

I want to implement "Fit to Page" functionality using mxGraph in Java. I tried to find width and height, using view.getGraphBounds().getWidth(), and tried to determine zoom factor to zoom out/in. But didn't worked. Any help/suggestion is greatly appreciated.

Frodo Baggins
  • 8,290
  • 6
  • 45
  • 55
Pramod
  • 21
  • 2
  • Can you explain what "didn't worked" (sic)? Computing the graph bounds, computing a scaling factor from these bounds and the width of the component and setting the zoom factor should be the right thing to do, basically... – Marco13 May 28 '14 at 08:32
  • I am not able to determine a unique formula for zooming factor which suffice all cases. Kindly can you elaborate on how to determine scaling factor based on graph bounds? – Pramod May 28 '14 at 09:10
  • Here is what I am trying out: "viewht" is the graph bound height, using view.getGraphBounds().getHeight(). 674 is my page default size i.e. If my graph is spanning beyond 674, then I will use "Fit to Page" . Here goes my formula: zFac=(((1-((viewht/500)-1)))); where zFac is the Zoom factor. Then I am using graphComponent.zoomTo(zFac, true); – Pramod May 28 '14 at 09:11
  • The formula should roughly be `double zoom = (double)windowWidth/graphWidth` (you can compute this separately for width and height, and finally take the smaller of the resulting zoom factors). – Marco13 May 28 '14 at 12:20
  • Hi Marco, I tried your solution. But it is actually restoring the default image/grah. Something like: 1. I have a graph. 2. I zoom in multiple times. 3. on executing double zoom = (double)windowWidth/graphWidth, it is replacing with the original graph as seen in step 1. – Pramod May 29 '14 at 05:34
  • How did you set the new scaling factor? It might have to be `view.setScale(zoom * view.getScale());` based on your description. Unfortunately, I can not (quickly) test all this, due to the lack of a http://stackoverflow.com/help/mcve – Marco13 May 29 '14 at 11:50
  • I was trying graphComponent.zoomTo(zFac, true); where zFac was calculated using the mentioned formula. – Pramod May 29 '14 at 12:05
  • Thanks Marco.. 80% of the problem solved. I was trying graphComponent.zoomTo(zFac, true); where zFac was calculated using the mentioned formula. I used your correct formula. Thanks for your help again! Another issue- This functionality "Fit to Window" using your formula works fine if and only if my graph component(cell or any other graph component) touches the upper and leftmost window edge (not vertex/meeting point). If component is not touching, then scroll bar will be enabled for the distance equal to the width and height of the nearest graph component away from the upper and left edge – Pramod May 29 '14 at 12:14
  • Sorry, I'm not really familiar with mxGraph. The `true`/`false` flag of the `zoomTo` method **might** have an influence here. You might also have to call `view.setTranslate(p)` where `p` is something like `-bounds.x, -bounds.y` (causing the upper left corner of the graph to be translated to the upper left corner of the window). But again, these are only guesses, because I can not test it. – Marco13 May 29 '14 at 13:08
  • I will give a try. Once again thanks a ton for all your inputs which really helped me to proceed. Thanks a lot!! – Pramod May 30 '14 at 08:41

0 Answers0