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

Keep aspect ratio while resizing DiagramShape/DiagramItem

6 Answers 496 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Boris Rogge
Top achievements
Rank 1
Boris Rogge asked on 11 Oct 2012, 10:22 AM
Hello Telerik,


How could I make the resizing of the diagram shapes keep the aspect ratio?

Thanks in advance,
Metanous

6 Answers, 1 is accepted

Sort by
0
Zarko
Telerik team
answered on 15 Oct 2012, 04:28 PM
Hello Boris,
Unfortunately at the moment there's no way to achieve this. We're working on a way to allow customization of the dragging, resizing, rotation etc. but this feature will be ready for our next Q (Q1 2013).
For now there's a possible workaround but I'm not sure if it'll be helpful to you - you could handle the PreviewResize and Resize events and manually change the shapes' size after the resizing:
private Dictionary<object, double> ratios;
private void OnDiagramResize(object sender, ResizeRoutedEventArgs e)
{
    foreach (var item in e.Items)
    {
        var container = this.diagram.ContainerGenerator.ContainerFromItem(item);
        if (container != null)
            container.Height = container.Width / ratios[container];
    }
}
 
private void OnDiagramPreviewResize(object sender, ResizeRoutedEventArgs e)
{
    this.ratios = new Dictionary<object, double>();
    foreach (var item in e.Items)
    {
        var container = this.diagram.ContainerGenerator.ContainerFromItem(item);
        if (container != null)
            ratios[container] = Math.Round(container.Width / container.Height, 2);
    }
}
If you have further questions please feel free to ask.

Regards,
Zarko
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Davide
Top achievements
Rank 1
answered on 16 May 2013, 12:57 PM
Hi Boris, 
regarding the possibility of sizing a raddiagramshape is there the possibility to resize it as it happens in Office when the SHIFT key is pressed?   The problem is that i have a raddiagramshape with a circle in it. When I change its size the circle continues to be a circle but my adorner assumes a rectangle (i've attached an image of what i mean). Is there any way to limit the adorner size in order to be a square?


Thanks in advance

Davide
0
Zarko
Telerik team
answered on 17 May 2013, 04:44 PM
Hello Davide,
You can easily achieve this with a custom resizing service. You can read about this here, and you can also examine the sample project that I've attached - in it the resizing adorner is always square.
I hope I was able to help you and if you have further questions feel free to ask.

All the best,
Zarko
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Davide
Top achievements
Rank 1
answered on 21 May 2013, 08:02 AM
Thanks Zarko
that was what i was looking for.
I've noticed that if you have two shapes in the panel and you select them both the area of each selected item is no more square but what is kept constant is the ratio of the selection. Is there a way to avoid this problem?

Thanks in advance

Davide
0
Zarko
Telerik team
answered on 23 May 2013, 12:55 PM
Hello Davide,
If you want to keep the size ratio on multi resize you'll have to add some more custom logic to the resizing service:
public class MyResizing : ResizingService
{
    private Rect adornerBounds;
    private double sizeRatio;
 
    public MyResizing(RadDiagram graph)
        : base(graph)
    { }
 
    public override void InitializeResize(IEnumerable<IDiagramItem> newSelectedItems, double adornerAngle, Rect adornerBounds, ResizeDirection resisingDirection, Point startPoint)
    {
        base.InitializeResize(newSelectedItems, adornerAngle, adornerBounds, resisingDirection, startPoint);
        this.adornerBounds = adornerBounds;
        this.sizeRatio = this.adornerBounds.Width / this.adornerBounds.Height;
    }
 
    protected override System.Windows.Point CalculateNewDelta(System.Windows.Point newPoint)
    {
        var newDelta = base.CalculateNewDelta(newPoint);
        double newOffset = Math.Max(newDelta.X, newDelta.Y);
 
        double newXDelta = this.sizeRatio > 1 ? newOffset * this.sizeRatio : newOffset;
        double newYDelta = this.sizeRatio > 1 ? newOffset : newOffset / this.sizeRatio;
 
        if (newOffset < 0)
        {
            newXDelta = Math.Max(-this.adornerBounds.Width, newXDelta);
            newYDelta = Math.Max(-this.adornerBounds.Height, newYDelta);
        }
 
        return new Point(newXDelta, newYDelta);
    }
}
With this code the multi resize should work as you want it to so could you please try it out and if you need more help feel free to ask.

Regards,
Zarko
Telerik

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Davide
Top achievements
Rank 1
answered on 23 May 2013, 02:30 PM
Thanks Zarko ... 

it's just what I was looking for ....

Thx again

Davide
Tags
Diagram
Asked by
Boris Rogge
Top achievements
Rank 1
Answers by
Zarko
Telerik team
Davide
Top achievements
Rank 1
Share this question
or