How can I manually maintain the diagram position?

I have a model I’m loading where the positions have been set by an external app. Some of the node location coordinates have values less than zero, and I want those to be positioned partially out of view as in the screenshot.

I can accomplish this by setting the diagram position to (0,0), but I am stuck on the other behaviors I want to have.

I want the diagram to always start at position (0,0) and want the position never to go to negative x or y. I see that I can use autoScrollRegion to make sure that dragging nodes to the left and top doesn’t cause automatic scrolling in those directions. I do want to allow autoscrolling to the bottom and right, though.

I’m trying to use the positionComputation function to set the correct position for my diagram, but I feel like I am missing something, since I have no idea how to handle the case when there are scrollbars. It seems like 0,0 is fine any time there is no scrollbar. Do I have to manually add the scroll offsets to the diagram position? How can even find the x or y scroll offset? Because I can’t see anything about this in the docs, it makes me think I’m heading in the wrong direction. Is positionComputation the right way to approach this? How can I correctly set the diagram position when scrollbars are present?


Here’s a fiddle: Edit fiddle - JSFiddle - Code Playground

I believe you want to define a Diagram | GoJS API that never returns negative values for x or y in its returned Point.

  $(go.Diagram, . . .,
    { . . .,
       positionComputation: function(d, p) { return new go.Point(Math.max(0, p.x), Math.max(0, p.y)); }

Thanks for the reply. Your suggested change causes my diagram to shift to positive x,y positions even when there is no scrollbar, which I don’t want. I always want to be at 0,0 unless the diagram is too large to fit in the current viewport.

When I use Math.max(0, x) as you suggest:

When I leave x = 0:

I want the behavior from the second screenshot above. What’s the best way to force the position to 0,0 while still accommodating the scroll offsets when scrollbars are present? Using your suggestion for the position, the scrollbars seem to work nicely. When I force the 0,0 position, then the scrollbars appear in the view when the document is too big, but the scrollbars don’t work properly (presumably because I am inappropriately setting the position). I tried modifying the position by the x,y values of the viewportBounds, but that seemed to cause the scrollbar to appear to be out of sync with the diagram.


Maybe you want your Diagram.positionComputation function to return zero whenever the Diagram.documentBounds fits within the Diagram.viewportBounds (compare their widths or heights), but otherwise let it return any non-negative value. That’s both for the X and the Y dimensions, independently.