Diagram in Xamarin Diagram (SfDiagram)

14 Oct 20224 minutes to read

The diagram control allows to create different types of diagrams such as flow charts, use case diagrams, workflow process diagrams, and more

Page settings

The Page settings are enabled to customize the appearance, width, and height of the diagram page. The size and appearance of the diagram pages can be customized with the PageSettings property.
The PageWidth and PageHeight properties of page settings define the size of the page. You can also customize the appearance of off-page regions with the BackgroundColor property.
The following code illustrates how to customize the page size and appearance of the page and off-page:

//Sets Page background
diagram.BackgroundColor = Color.Lime;
//Sets Page size 
diagram.PageSettings.PageWidth = 500;
diagram.PageSettings.PageHeight = 500;
//Customizes the appearance of Page
diagram.PageSettings.PageBackGround= Color.White;

View Port Start Position

Page settings enable you to customize the viewport start position of the diagram page. The enum ViewPort Start Position has two constants: Default, Origin. The default value is set to Default.


The viewport of the diagram page is decided based on the node’s offsets.


The viewport of the diagram page will be in a negative region when the node’s offsets are negative.


The viewport of the diagram page begins with origin (0,0) even though when the node’s offsets are negative.

The following code explains how to customize the view port start position of the page.

//Sets the viewport start position.
SfDiagram.PageSettings.ViewPortStartPosition = ViewportStartPosition.Origin;


Stencil has a collection of symbols. It is used to clone the desired symbol by dragging it from the stencil and dropping it into the SfDiagram.
The following code snippet illustrates how to add the stencil:

Stencil stencil = new Stencil();

Diagram constraints

The constraints property of diagram allows you to enable or disable certain features.
This will enable or disable the following node behavior.

  • Drag
  • Resize
  • Rotate
  • AnnotationEditing
  • EnableSelectors
  • EnableZoomAndPan
  • IsReadOnly

The following code illustrates how to disable the item dragging.

SfDiagram diagram = new SfDiagram(this);
// Disable the item dragging
diagram.EnableDrag = false;

Diagram style settings

It is easier to apply default rendering styles to all shapes, connectors, stencil symbol, and stencil header in a diagram. Pass the following arguments to diagram style setting constructor:

  • DefaultNodeStyle argument: Defines the node style properties.
  • DefaultConnectorStyle argument: Defines the connector style properties.
  • DefaultSymbolStyle argument: Defines the symbol style properties.
  • DefaultHeaderStyle argument: Defines the header style properties.

The following code shows how to define the diagram style settings for the diagram object.

// Diagram style settings 
Style NodeStyle = new Style() { Brush = new SolidBrush(Color.Blue), StrokeBrush = new SolidBrush(Color.Brown), StrokeWidth = 2, StrokeStyle = StrokeStyle.Dashed };

Style ConnectorStyle = new Style() { StrokeBrush = new SolidBrush(Color.DeepSkyBlue), StrokeWidth = 3, StrokeStyle = StrokeStyle.Dotted };

SymbolStyle SymbolStyle = new SymbolStyle() { Width = 70, Height = 70, StrokeWidth = 3, StrokeColor = Color.Red };

HeaderStyle HeaderStyle = new HeaderStyle() { FontSize = 24, TextBrush = Color.White, Fill = Color.SteelBlue, FontStyle = FontStyle.Italic, HorizontalAlignment = HorizontalAlignment.Center };

DiagramStyleSettings diagramStyleSettings = new DiagramStyleSettings(NodeStyle, ConnectorStyle, SymbolStyle, HeaderStyle);

//Pass diagram style settings instance to SfDiagram constructor argument  
SfDiagram diagram = new SfDiagram(this ,diagramStyleSettings);

Zooming enhancement

Supports customizing the zoom levels. You can set minimum zoom level value to 0.01f and maximum to ‘infinite’.

// Define the minimum and maximum zoom factor value 

  diagram.MinimumZoomFactor = 0.01f;
  diagram.MaximumZoomFactor = 5.00f;