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

Custom shape drawing question

4 Answers 138 Views
Diagram, DiagramRibbonBar, DiagramToolBox
This is a migrated thread and some comments may be shown as answers.
Valentino
Top achievements
Rank 1
Valentino asked on 23 Aug 2017, 04:24 AM

I have a data model that I'm trying to draw in a Diagram. I'm having some success with a custom shape class something like below. MyModelElement has X, Y, W, H & Text as well as other model data. I'm getting the bounding box in a different location to my visible shape, and it's always 100x100. I tried adding 

Size = new Size(element.W, element.H)

in the  RadDiagramShape creation below, but didn't change anything. Any help appreciated.

 

My code to populate the diagram is similar to below.

private void PopulateDiagram()
{
    foreach (var element in elements)
    {
        var elementShape = new MyElementShape(element);
 
        var shape = new RadDiagramShape
        {
            Text = element.Text,
            ElementShape = elementShape,
            ForeColor = Color.Black,
            DrawBorder = true,
            BorderThickness = new Padding(1),
            Position = new Telerik.Windows.Diagrams.Core.Point(element.X, element.Y)
        };
        radDiagram1.AddShape(shape);
    }
 
 My custom shape class
public class MYElementShape : ElementShape
{
    public MyElementShape(MyModelElement element)
    {
        _element = element;
    }
    public override GraphicsPath CreatePath(Rectangle bounds)
    {
        GraphicsPath path = new GraphicsPath();
        path.AddRectangle( new Rectangle(_element.X, _element.Y, _element.W, _element.H));
        return path;

   }

 }

4 Answers, 1 is accepted

Sort by
0
Valentino
Top achievements
Rank 1
answered on 23 Aug 2017, 04:27 AM
Ignore minor errors in the code, as I edited it for the thread.
0
Valentino
Top achievements
Rank 1
answered on 23 Aug 2017, 04:39 AM

Sorry for the spamming. I think adding Size = ... actually does help and I'll look into this more, but I'd still like to know if I'm on the right track with the above code.

I'm creating a UI editor, where everything is rectangle based in shape, and has a label (Text). The label should be placed in relation to the UI element (the label has it's own X, Y, W, H, Text properties so can be positioned by these, but I haven't gotten that far with RadDiagram yet). I'm hoping the Diagram control can do the job. I'll keep experimenting.

0
Valentino
Top achievements
Rank 1
answered on 23 Aug 2017, 11:08 PM

I had more luck by changing the line above to this, using bounds.X and bounds,Y. Also some of my coords were incorrect.

    path.AddRectangle( new Rectangle(bounds.X, bounds.Y, _element.ScrnW, _element.ScrnH));<br>

I've also decided to use separate Shapes for my labels and treat them separately.

0
Hristo
Telerik team
answered on 24 Aug 2017, 06:17 AM
Hi Valentino,

Thank you for writing.

Generally speaking, your approach is correct. You can access the Size property of the shapes and set it according to your requirement. The CreatePath method also provides the current bounds and the shape can be manipulated according to it: http://docs.telerik.com/devtools/winforms/diagram/custom-shapes#creating-custom-shapes-programmatically.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Diagram, DiagramRibbonBar, DiagramToolBox
Asked by
Valentino
Top achievements
Rank 1
Answers by
Valentino
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or