Dynamically setting diagram bounds


I am running into an issue where I want to double the size of the diagram bounds whenever the bounds of the diagram are changed/calculated. The reason for this is that I want to pan the view beyond the extents defined by the parts on the diagram. For example, I can’t pan a node to the center of the diagram if it is in the upper-right corner of the diagram when a node is in the bottom-left corner.

Currently the only way to accomplish this is to zoom out and then pan the node to the center of the diagram. However, when I zoom back in it shifts back to the original position, constraining the pan. Is there anyway to set the diagram bounds dynamically to be double the calculated bounds?

I tried overriding diagram.computeBounds() with no success (I think it is stated in the API that you should not do this). I also tried working with the DocumentBoundsChanged event on the diagram, but my initial efforts were also unsuccessful. I also do not see anything helpful in the panningTool class.

I feel like I am missing something… Thanks for your help!

It doesn’t sound like you want to double the bounds, but just make sure it’s big enough to include the size of the viewport along the borders of the actual bounds.

The easiest thing to try first is to set Diagram.padding to a large value. If that behavior works as you want, then you can continue using that. Or if it’s not quite right, because it doesn’t depend on some relevant state such as the Diagram.scale, perhaps you could implement a “DocumentBoundsChanged” DiagramEvent listener to set the Diagram.fixedBounds to whatever value you like.

I haven’t tried this, so I hope it works and that you don’t run into any problems.

Thanks for your response.

This is close to what I want. However, now when I use diagram.zoomTo() (or zoomToFit()) and diagram.computeBounds() the returned document bounds has a height and width of 0.

Setting the padding on the diagram seems to break these methods by not allowing the diagram to properly determine it own width and height.

Is this a bug or am I missing something.

Thanks again!

If you have set Diagram.padding to be a non-zero Margin, then I do not see how it is possible for Diagram.computeBounds() to return a zero-sized Rect.

Diagram.computeBounds() is defined to either return the Diagram.fixedBounds, if that Rect.isReal(), or else to figure out what area is occupied by all of the Parts for which Part.isVisible() and Part.isInDocumentBounds are true and then addMargin(this.padding) on that Rect.

I think I have this working now. I set a default padding on the diagram that is twice the size of the diagram on creation. Every time the document bounds change I calculate the real width of the diagram by subtracting the padding from the documentBounds. I then double the padding based on that new width.

Thanks for your help on this!