Applies to: Nevron Diagram for .NET
How to implement show-hide (expand/collapse) on a tree diagram?
The following code example will generate a random tree diagram with show-hide subtree decorator for each shape that has children:
[C#]
using
Nevron.GraphicsCore;
using
Nevron.Diagram;
using
Nevron.Diagram.Shapes;
using
Nevron.Diagram.WinForm;
using
Nevron.Diagram.Templates;
using
Nevron.Diagram.Filters;
using
Nevron.Diagram.Layout;
using
Nevron.Diagram.DataStructures;
…
view.BeginInit();
view.Dock = DockStyle.Fill;
view.ViewLayout = ViewLayout.Fit;
view.Grid.Visible =
false
;
view.GlobalVisibility.ShowPorts =
false
;
document.BeginInit();
// create a random tree
NGenericTreeTemplate treeTemplate =
new
NGenericTreeTemplate();
treeTemplate.BranchNodes = 3;
treeTemplate.VerticesSize =
new
NSizeF(50, 50);
treeTemplate.Balanced =
false
;
treeTemplate.Levels = 5;
treeTemplate.Create(document);
// add show-hide subtree decorator for each shape that has children
foreach
(NShape shape
in
document.ActiveLayer.Children(NFilters.Shape2D))
{
if
(shape.GetOutgoingShapes().Count == 0)
continue
;
// create the decorators collection
shape.CreateShapeElements(ShapeElementsMask.Decorators);
NShowHideSubtreeDecorator decorator =
new
NShowHideSubtreeDecorator();
decorator.Offset =
new
NSizeF(-11, 11);
decorator.Size =
new
NSizeF(15, 15);
decorator.Alignment =
new
NContentAlignment(ContentAlignment.TopLeft);
shape.Decorators.AddChild(decorator);
}
document.AutoBoundsMinSize =
new
NSizeF(400, 400);
// layout with a layered tree layout
NLayoutContext context =
new
NLayoutContext();
context.GraphAdapter =
new
NShapeGraphAdapter(
true
);
context.BodyAdapter =
new
NShapeBodyAdapter(document);
context.BodyContainerAdapter =
new
NDrawingBodyContainerAdapter(document);
NLayeredTreeLayout layout =
new
NLayeredTreeLayout();
layout.VertexSpacing = 50;
layout.LayerSpacing = 50;
layout.Layout(document.ActiveLayer.Children(
null
), context);
// size to visible shapes
document.SizeToContent(document.AutoBoundsMinSize, document.AutoBoundsPadding, NFilters.Visible);
document.EndInit();
view.EndInit();
[VB.NET]
Imports
Nevron.GraphicsCore
Imports
Nevron.Diagram
Imports
Nevron.Diagram.Shapes
Imports
Nevron.Diagram.WinForm
Imports
Nevron.Diagram.Templates
Imports
Nevron.Diagram.Filters
Imports
Nevron.Diagram.Layout
Imports
Nevron.Diagram.DataStructures
…
view.BeginInit()
view.Dock = DockStyle.Fill
view.ViewLayout = ViewLayout.Fit
view.Grid.Visible =
False
view.GlobalVisibility.ShowPorts =
False
document.BeginInit()
' create a random tree
Dim
treeTemplate
As
New
NGenericTreeTemplate()
treeTemplate.BranchNodes = 3
treeTemplate.VerticesSize =
New
NSizeF(50, 50)
treeTemplate.Balanced =
False
treeTemplate.Levels = 5
treeTemplate.Create(document)
' add show-hide subtree decorator for each shape that has children
For
Each
shape
As
NShape
In
document.ActiveLayer.Children(NFilters.Shape2D)
If
shape.GetOutgoingShapes().Count = 0
Then
Continue
For
End
If
' create the decorators collection
shape.CreateShapeElements(ShapeElementsMask.Decorators)
Dim
decorator
As
New
NShowHideSubtreeDecorator()
decorator.Offset =
New
NSizeF(-11, 11)
decorator.Size =
New
NSizeF(15, 15)
decorator.Alignment =
New
NContentAlignment(ContentAlignment.TopLeft)
shape.Decorators.AddChild(decorator)
Next
document.AutoBoundsMinSize =
New
NSizeF(400, 400)
' layout with a layered tree layout
Dim
context
As
New
NLayoutContext()
context.GraphAdapter =
New
NShapeGraphAdapter(
True
)
context.BodyAdapter =
New
NShapeBodyAdapter(document)
context.BodyContainerAdapter =
New
NDrawingBodyContainerAdapter(document)
Dim
layout
As
New
NLayeredTreeLayout()
layout.VertexSpacing = 50
layout.LayerSpacing = 50
layout.Layout(document.ActiveLayer.Children(
Nothing
), context)
' size to visible shapes
document.SizeToContent(document.AutoBoundsMinSize, document.AutoBoundsPadding, NFilters.Visible)
document.EndInit()
view.EndInit()
For more detailed information about Decorators, take a look at the Help Documentation:
Diagram for .NET > User's Guide > Document Object Model > Models > Shapes > Decorators
Related examples:
Diagram Windows Forms: Document Object Model - Shapes - Decorators folder
Diagram Web Forms: Show/Hide Subtree Decorator
Article ID: 99, Created On: 10/21/2010, Modified: 12/29/2010