This is a migrated thread and some comments may be shown as answers.

How to bring all elements of a raddiagram into view? BringIntoView not working

1 Answer 146 Views
Diagram, DiagramRibbonBar, DiagramToolBox
This is a migrated thread and some comments may be shown as answers.
Cezara
Top achievements
Rank 1
Cezara asked on 27 May 2020, 11:41 AM

Hello,

I am trying to bring into view all elements of a diagram, but cannot manage to fit it to screen. Is there any workaround for BringIntoView method?
I tried to use the followings and won't work.

Last piece of code sometimes works, sometimes doesn't.
Is there a formula to calculate the bounds, the view port size and zoom to fit all elements of a diagram into view?

Thank you!

 

1.
var box = selectedHardwareVariant.Shapes.GetEnclosingBounds();
selectedHardwareVariant.DiagramElement.BringIntoView(box, false);
selectedHardwareVariant.DiagramElement.Controller.OnViewportChanged();

2.

var box = selectedHardwareVariant.Shapes.GetEnclosingBounds(); selectedHardwareVariant.PanToPosition(box.Center()); selectedHardwareVariant.DiagramElement.BringIntoView(box, false); selectedHardwareVariant.DiagramElement.Controller.OnViewportChanged();

3.

Telerik.Windows.Diagrams.Core.DiagramConstants.MinimumZoom = 0.001;
                Telerik.Windows.Diagrams.Core.DiagramConstants.MaximumZoom = 50;
                selectedHardwareVariant.AutoScroll = true;
                selectedHardwareVariant.AutoLayout = true;
                selectedHardwareVariant.ZoomIn(1);
                selectedHardwareVariant.Zoom = 1;
                selectedHardwareVariant.DiagramElement.Controller.OnViewportChanged();
                var box = selectedHardwareVariant.Shapes.GetEnclosingBounds();
                selectedHardwareVariant.PanToPosition(box.Center());
                selectedHardwareVariant.DiagramElement.BringIntoView(box, false);
                selectedHardwareVariant.DiagramElement.Controller.OnViewportChanged();
                IEnumerable<IShape> shapesOutside = selectedHardwareVariant.DiagramElement.Shapes.Where(x => !selectedHardwareVariant.DiagramElement.IsInViewport(x));
                for (int i = 0; i < shapesOutside.Count(); i++)
                {
                    double newZoom = Math.Round(selectedHardwareVariant.Zoom/1.1, 3);
                    box = selectedHardwareVariant.Shapes.GetEnclosingBounds();
                    selectedHardwareVariant.DiagramElement.PanToPosition(box.Center());
                    selectedHardwareVariant.DiagramElement.Controller.OnViewportChanged();
                    selectedHardwareVariant.ZoomOut(newZoom, box.Center());
                    selectedHardwareVariant.Zoom = newZoom;
                    if (selectedHardwareVariant.DiagramElement.Shapes.All(x => selectedHardwareVariant.DiagramElement.IsInViewport(x)))
                    {
                        break;
                    }
                }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 May 2020, 01:45 PM

Hello, Cezara,  

RadDiagram has an overload for the DiagramElement.BringIntoView method that accepts a RadDigramShape item. This is the easiest way to scroll the shape into the view: 

this.radDiagram1.DiagramElement.BringIntoView(this.radDiagram1.Shapes[2])

You can use a RadDiagramContainerShape  that contains multiple shapes in it. Thus, you can easy bring the whole container into the view with the many shapes:

this.radDiagram1.DiagramElement.BringIntoView(this.radDiagram1.Shapes[3]);

Each shape offers Position property indicating its original location on the diagram. The Position of RadDiagramElement indicates whether the view is panned and with what offset. Thus, considering the Size of the diagram element and its Position, you can determine whether a shape is visible in the view or not ( by shape's Position). Don't forget the zoom factor because it also has to be consider when taking the position of the shape, e.g. shape's position * zoom = current position, then determine whether this current position intersect with the diagram element's size and Position offset. Hence, you can play with the zoom level or pan offset to ensure that all shapes are displayed into the view.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
Diagram, DiagramRibbonBar, DiagramToolBox
Asked by
Cezara
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or