The barycenter layout method splits the input graph into a set of fixed and free vertices. Fixed vertices are nailed to the corners of a strictly convex polygon, while free vertices are placed in the barycenter of their neighbors. The barycenter force accessible from the BarycenterForce property of the layout is responsible for attracting the vertices to their barycenter.
In case there are no fixed vertices this will place all vertices at a single point, which is obviously not a good graph drawing. That is why the barycenter layout needs at least three fixed vertices.
The minimal amount of fixed vertices is specified by the MinFixedVerticesCount property. If the input graph does not have that many fixed vertices, the layout will automatically fulfill this requirement. This is done by fixing the vertices with the smallest degree.
In this example the fixed vertices are highlighted in orange.

| Language: C# |
using Nevron.Nov;
using Nevron.Nov.DataStructures;
using Nevron.Nov.Diagram;
using Nevron.Nov.Diagram.Batches;
using Nevron.Nov.Diagram.Layout;
using Nevron.Nov.Diagram.Shapes;
using Nevron.Nov.Dom;
using Nevron.Nov.Editors;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System;
using System.Windows.Forms;
namespace NovWinFormsProject
{
public partial class Form1 : Form
{
#region Constructors
public Form1()
{
InitializeComponent();
// Set form title
Text = "NOV WinForms - NBarycenterGraphLayoutExample";
// Create the NOV example and its controls
NWidget exampleContent = CreateExampleContent();
NWidget exampleControls = CreateExampleControls();
if (exampleControls != null)
{
exampleControls.PreferredWidth = 300;
}
// Place the example and its controls in a pair box
NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
pairBox.FillMode = ENStackFillMode.First;
pairBox.FitMode = ENStackFitMode.First;
pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);
// Host the example in the form
NNovWidgetHost host = new NNovWidgetHost(pairBox);
host.Dock = DockStyle.Fill;
Controls.Add(host);
}
#endregion
#region Example
public string GetExampleDescription()
{
return @"
The barycenter layout method splits the input graph into a set of fixed and free vertices.
Fixed vertices are nailed to the corners of a strictly convex polygon,
while free vertices are placed in the barycenter of their neighbors.
The barycenter force accessible from the BarycenterForce property of the layout is
responsible for attracting the vertices to their barycenter.
In case there are no fixed vertices this will place all vertices at a single point,
which is obviously not a good graph drawing. That is why the barycenter layout needs
at least three fixed vertices.
The minimal amount of fixed vertices is specified by the MinFixedVerticesCount property.
If the input graph does not have that many fixed vertices, the layout will automatically
fulfill this requirement. This is done by fixing the vertices with the smallest degree.
In this example the fixed vertices are highlighted in orange.
";
}
///
/// Gets the relevant documentation links for the example
///
///
public (string Name, string Url)[] GetDocumentationUrls()
{
return new[]
{
("Barycenter Layout", "https://www.nevron.com/docs/openvision/diagram-barycenter-layout.html"),
("Layouts Overview", "https://www.nevron.com/docs/openvision/diagram-drawing-layouts-overview.html"),
("Graph Regions and Regions Layouts", "https://www.nevron.com/docs/openvision/diagram-graph-regions-and-region-layouts.html"),
("Shape Navigation", "https://www.nevron.com/docs/openvision/diagram-shape-navigation.html"),
};
}
public NWidget CreateExampleContent()
{
// Create a simple drawing
NDrawingViewWithRibbon drawingViewWithRibbon = new NDrawingViewWithRibbon();
m_DrawingView = drawingViewWithRibbon.View;
m_DrawingView.Document.HistoryService.Pause();
try
{
InitDiagram(m_DrawingView.Document);
}
finally
{
m_DrawingView.Document.HistoryService.Resume();
}
return drawingViewWithRibbon;
}
public NWidget CreateExampleControls()
{
m_Layout.Changed += OnLayoutChanged;
NStackPanel stack = new NStackPanel();
// property editor
NEditor editor = NDesigner.GetDesigner(m_Layout).CreateStandaloneEditor(m_Layout);
stack.Add(new NGroupBox("Properties", editor));
NButton arrangeButton = new NButton("Arrange Diagram");
arrangeButton.Click += OnArrangeButtonClick;
stack.Add(arrangeButton);
// items stack
NStackPanel itemsStack = new NStackPanel();
NButton triangularGrid6 = new NButton("Create Triangular Grid (levels 6)");
triangularGrid6.Click += delegate(NEventArgs args) { CreateTriangularGridDiagram(6); };
stack.AddChild(triangularGrid6);
NButton triangularGrid8 = new NButton("Create Triangular Grid (levels 8)");
triangularGrid8.Click += delegate(NEventArgs args) { CreateTriangularGridDiagram(8); };
stack.AddChild(triangularGrid8);
NButton random10 = new NButton("Random (fixed 10, free 10)");
random10.Click += delegate(NEventArgs args) { CreateRandomBarycenterDiagram(10, 10); };
stack.AddChild(random10);
NButton random15 = new NButton("Random (fixed 15, free 15)");
random15.Click += delegate(NEventArgs args) { CreateRandomBarycenterDiagram(15, 15); };
stack.AddChild(random15);
stack.Add(new NGroupBox("Items", itemsStack));
return stack;
}
private void InitDiagram(NDrawingDocument drawingDocument)
{
// Hide ports
drawingDocument.Content.ScreenVisibility.ShowPorts = false;
// Create a random diagram
CreateRandomBarycenterDiagram(8, 10);
// Arrange the diagram
ArrangeDiagram(drawingDocument);
// Fit active page
drawingDocument.Content.ActivePage.ZoomMode = ENZoomMode.Fit;
}
#endregion
#region Implementation
///
/// Creates a random barycenter diagram with the specified settings
///
/// number of fixed vertices (must be larger than 3)
/// number of free vertices
private void CreateRandomBarycenterDiagram(int fixedCount, int freeCount)
{
if (fixedCount < 3)
throw new ArgumentException("Needs at least three fixed vertices");
// clean up the active page
NPage activePage = m_DrawingView.ActivePage;
activePage.Items.Clear();
// we will be using basic circle shapes with default size of (30, 30)
NBasicShapeFactory basicShapes = new NBasicShapeFactory();
basicShapes.DefaultSize = new NSize(30, 30);
// create the fixed vertices
NShape[] fixedShapes = new NShape[fixedCount];
for (int i = 0; i < fixedCount; i++)
{
fixedShapes[i] = basicShapes.CreateShape(ENBasicShape.Circle);
//((NDynamicPort)fixedShapes[i].Ports.GetChildByName("Center", -1)).GlueMode = DynamicPortGlueMode.GlueToLocation;
fixedShapes[i].Geometry.Fill = new NStockGradientFill(ENGradientStyle.Horizontal, ENGradientVariant.Variant3, new NColor(251, 203, 156), new NColor(247, 150, 56));
fixedShapes[i].Geometry.Stroke = new NStroke(1, new NColor(68, 90, 108));
// setting the ForceXMoveable and ForceYMoveable properties to false
// specifies that the layout cannot move the shape in both X and Y directions
NForceDirectedGraphLayout.SetXMoveable(fixedShapes[i], false);
NForceDirectedGraphLayout.SetYMoveable(fixedShapes[i], false);
activePage.Items.AddChild(fixedShapes[i]);
}
// create the free vertices
NShape[] freeShapes = new NShape[freeCount];
for (int i = 0; i < freeCount; i++)
{
freeShapes[i] = basicShapes.CreateShape(ENBasicShape.Circle);
freeShapes[i].Geometry.Fill = new NStockGradientFill(ENGradientStyle.Horizontal, ENGradientVariant.Variant3, new NColor(192, 194, 194), new NColor(129, 133, 133));
freeShapes[i].Geometry.Stroke = new NStroke(1, new NColor(68, 90, 108));
activePage.Items.AddChild(freeShapes[i]);
}
// link the fixed shapes in a circle
for (int i = 0; i < fixedCount; i++)
{
NRoutableConnector connector = new NRoutableConnector();
connector.MakeLine();
activePage.Items.AddChild(connector);
if (i == 0)
{
connector.GlueBeginToShape(fixedShapes[fixedCount - 1]);
connector.GlueEndToShape(fixedShapes[0]);
}
else
{
connector.GlueBeginToShape(fixedShapes[i - 1]);
connector.GlueEndToShape(fixedShapes[i]);
}
}
// link each free shape with two different random fixed shapes
Random rnd = new Random();
for (int i = 0; i < freeCount; i++)
{
int firstFixed = rnd.Next(fixedCount);
int secondFixed = (firstFixed + rnd.Next(fixedCount / 3) + 1) % fixedCount;
// link with first fixed
NRoutableConnector lineShape = new NRoutableConnector();
lineShape.MakeLine();
activePage.Items.AddChild(lineShape);
lineShape.GlueBeginToShape(freeShapes[i]);
lineShape.GlueEndToShape(fixedShapes[firstFixed]);
// link with second fixed
lineShape = new NRoutableConnector();
lineShape.MakeLine();
activePage.Items.AddChild(lineShape);
lineShape.GlueBeginToShape(freeShapes[i]);
lineShape.GlueEndToShape(fixedShapes[secondFixed]);
}
// link each free shape with another free shape
for (int i = 1; i < freeCount; i++)
{
NRoutableConnector connector = new NRoutableConnector();
connector.MakeLine();
activePage.Items.AddChild(connector);
connector.GlueBeginToShape(freeShapes[i - 1]);
connector.GlueEndToShape(freeShapes[i]);
}
// send all edges to back
NBatchReorder batchReorder = new NBatchReorder(m_DrawingView.Document);
batchReorder.Build(activePage.GetShapes(false, NDiagramFilters.ShapeType1D).CastAll());
batchReorder.SendToBack(activePage);
// arrange the elements
ArrangeDiagram(m_DrawingView.Document);
}
///
/// Creates a triangular grid diagram with the specified count of levels
///
///
private void CreateTriangularGridDiagram(int levels)
{
// clean up the active page
NPage activePage = m_DrawingView.ActivePage;
activePage.Items.Clear();
// we will be using basic circle shapes with default size of (30, 30)
NBasicShapeFactory basicShapes = new NBasicShapeFactory();
basicShapes.DefaultSize = new NSize(30, 30);
NShape prev = null;
NList prevRowShapes = null;
for (int level = 1; level < levels; level++)
{
NList curRowShapes = new NList();
for (int i = 0; i < level; i++)
{
NShape cur = basicShapes.CreateShape(ENBasicShape.Circle);
cur.Geometry.Fill = new NStockGradientFill(ENGradientStyle.Horizontal, ENGradientVariant.Variant3, new NColor(192, 194, 194), new NColor(129, 133, 133));
cur.Geometry.Stroke = new NStroke(1, new NColor(68, 90, 108));
activePage.Items.Add(cur);
NRoutableConnector edge;
// connect with prev
if (i > 0)
{
edge = new NRoutableConnector();
edge.MakeLine();
activePage.Items.Add(edge);
edge.GlueBeginToShape(prev);
edge.GlueEndToShape(cur);
}
// connect with ancestors
if (level > 1)
{
if (i < prevRowShapes.Count)
{
edge = new NRoutableConnector();
edge.MakeLine();
activePage.Items.Add(edge);
edge.GlueBeginToShape(prevRowShapes[i]);
edge.GlueEndToShape(cur);
}
if (i > 0)
{
edge = new NRoutableConnector();
edge.MakeLine();
activePage.Items.Add(edge);
edge.GlueBeginToShape(prevRowShapes[i - 1]);
edge.GlueEndToShape(cur);
}
}
// fix the three corner vertices
if (level == 1 || (level == levels - 1 && (i == 0 || i == level - 1)))
{
NForceDirectedGraphLayout.SetXMoveable(cur, false);
NForceDirectedGraphLayout.SetYMoveable(cur, false);
cur.Geometry.Fill = new NStockGradientFill(ENGradientStyle.Horizontal, ENGradientVariant.Variant3, new NColor(251, 203, 156), new NColor(247, 150, 56));
cur.Geometry.Stroke = new NStroke(1, new NColor(68, 90, 108));
}
curRowShapes.Add(cur);
prev = cur;
}
prevRowShapes = curRowShapes;
}
// send all edges to back
NBatchReorder batchReorder = new NBatchReorder(m_DrawingView.Document);
batchReorder.Build(activePage.GetShapes(false, NDiagramFilters.ShapeType1D).CastAll());
batchReorder.SendToBack(activePage);
// arrange the elements
ArrangeDiagram(m_DrawingView.Document);
}
///
/// Arranges the shapes in the active page.
///
///
private void ArrangeDiagram(NDrawingDocument drawingDocument)
{
// get all top-level shapes that reside in the active page
NPage activePage = drawingDocument.Content.ActivePage;
NList shapes = activePage.GetShapes(false);
// create a layout context and use it to arrange the shapes using the current layout
NDrawingLayoutContext layoutContext = new NDrawingLayoutContext(drawingDocument, activePage);
m_Layout.Arrange(shapes.CastAll