Applies to: Nevron Diagram for .NET
How to change object color and shape via data import?
You can use the DataImport for generating a graph using data tables. You can add labels to the connectors, change the shape for certain objects and change from one color to another. The following source code makes a simple drawing which imports data from two tables. The vertices have different shape and color, and edges have labels.
[C#]
using
System;
using
System.Data;
using
System.Drawing;
using
System.Windows.Forms;
using
Nevron.Diagram;
using
Nevron.Diagram.DataImport;
using
Nevron.Diagram.Layout;
using
Nevron.Diagram.WinForm;
using
Nevron.GraphicsCore;
...
private
void
Form1_Load(
object
sender, EventArgs e)
{
NDrawingView view =
new
NDrawingView();
Controls.Add(view);
view.Dock = DockStyle.Fill;
view.GlobalVisibility.ShowPorts =
false
;
NDrawingDocument doc =
new
NDrawingDocument();
view.Document = doc;
DataTable vtable =
new
DataTable();
vtable.Columns.Add(
"id"
,
typeof
(
int
));
vtable.Columns.Add(
"text"
,
typeof
(
string
));
vtable.Rows.Add(1,
"root"
);
vtable.Rows.Add(2,
"c1"
);
vtable.Rows.Add(3,
"c2"
);
DataTable etable =
new
DataTable();
etable.Columns.Add(
"from"
,
typeof
(
int
));
etable.Columns.Add(
"to"
,
typeof
(
int
));
etable.Columns.Add(
"text"
,
typeof
(
string
));
etable.Rows.Add(1, 2,
"label1"
);
etable.Rows.Add(1, 3,
"label2"
);
NGraphDataSourceImporter importer =
new
NGraphDataSourceImporter();
importer.Document = doc;
importer.VertexDataSource = vtable;
importer.EdgeDataSource = etable;
importer.VertexIdColumnName =
"id"
;
importer.FromVertexIdColumnName =
"from"
;
importer.ToVertexIdColumnName =
"to"
;
importer.Layout =
new
NLayeredGraphLayout();
importer.VertexShapesFactory =
new
MyShapeFactory();
importer.VertexImported +=
new
ShapeImportedDelegate(importer_VertexImported);
importer.EdgeImported +=
new
ShapeImportedDelegate(importer_EdgeImported);
importer.Import();
}
void
importer_EdgeImported(NDataSourceImporter dataSourceImporter, NShape shape, INDataRecord record)
{
shape.Text = record.GetColumnValue(
"text"
).ToString();
}
void
importer_VertexImported(NDataSourceImporter dataSourceImporter, NShape shape, INDataRecord record)
{
shape.Text = record.GetColumnValue(
"text"
).ToString();
NCompositeShape cshape = shape
as
NCompositeShape;
if
(shape.Text ==
"root"
)
{
cshape.Primitives.AddChild(
new
NRectanglePath(0, 0, 80, 80));
cshape.Style.FillStyle =
new
NColorFillStyle(Color.CornflowerBlue);
}
else
{
cshape.Primitives.AddChild(
new
NEllipsePath(0, 0, 80, 80));
cshape.Style.FillStyle =
new
NColorFillStyle(Color.OrangeRed);
}
cshape.UpdateModelBounds();
cshape.CreateShapeElements(ShapeElementsMask.Ports);
cshape.Ports.AddChild(
new
NDynamicPort(
new
NContentAlignment(), DynamicPortGlueMode.GlueToContour));
}
public
class
MyShapeFactory : INShapesFactory
{
#region INShapesFactory Members
public
bool
CanCreateShape(
string
name)
{
return
true
;
}
public
NShape CreateShape(
string
name)
{
return
new
NCompositeShape();
}
#endregion
}
[VB.NET]
Imports
System
Imports
System.Data
Imports
System.Drawing
Imports
System.Windows.Forms
Imports
Nevron.Diagram
Imports
Nevron.Diagram.DataImport
Imports
Nevron.Diagram.Layout
Imports
Nevron.Diagram.WinForm
Imports
Nevron.GraphicsCore
...
Private
Sub
Form1_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
Dim
view
As
New
NDrawingView()
Controls.Add(view)
view.Dock = DockStyle.Fill
view.GlobalVisibility.ShowPorts =
False
Dim
doc
As
New
NDrawingDocument()
view.Document = doc
Dim
vtable
As
New
DataTable()
vtable.Columns.Add(
"id"
,
GetType
(
Integer
))
vtable.Columns.Add(
"text"
,
GetType
(
String
))
vtable.Rows.Add(1,
"root"
)
vtable.Rows.Add(2,
"c1"
)
vtable.Rows.Add(3,
"c2"
)
Dim
etable
As
New
DataTable()
etable.Columns.Add(
"from"
,
GetType
(
Integer
))
etable.Columns.Add(
"to"
,
GetType
(
Integer
))
etable.Columns.Add(
"text"
,
GetType
(
String
))
etable.Rows.Add(1, 2,
"label1"
)
etable.Rows.Add(1, 3,
"label2"
)
Dim
importer
As
New
NGraphDataSourceImporter()
importer.Document = doc
importer.VertexDataSource = vtable
importer.EdgeDataSource = etable
importer.VertexIdColumnName =
"id"
importer.FromVertexIdColumnName =
"from"
importer.ToVertexIdColumnName =
"to"
importer.Layout =
New
NLayeredGraphLayout()
importer.VertexShapesFactory =
New
MyShapeFactory()
AddHandler
importer.VertexImported,
AddressOf
importer_VertexImported
AddHandler
importer.EdgeImported,
AddressOf
importer_EdgeImported
importer.Import()
End
Sub
Private
Sub
importer_EdgeImported(
ByVal
dataSourceImporter
As
NDataSourceImporter,
ByVal
shape
As
NShape,
ByVal
record
As
INDataRecord)
shape.Text = record.GetColumnValue(
"text"
).ToString()
End
Sub
Private
Sub
importer_VertexImported(
ByVal
dataSourceImporter
As
NDataSourceImporter,
ByVal
shape
As
NShape,
ByVal
record
As
INDataRecord)
shape.Text = record.GetColumnValue(
"text"
).ToString()
Dim
cshape
As
NCompositeShape = TryCast(shape, NCompositeShape)
If
shape.Text =
"root"
Then
cshape.Primitives.AddChild(
New
NRectanglePath(0, 0, 80, 80))
cshape.Style.FillStyle =
New
NColorFillStyle(Color.CornflowerBlue)
Else
cshape.Primitives.AddChild(
New
NEllipsePath(0, 0, 80, 80))
cshape.Style.FillStyle =
New
NColorFillStyle(Color.OrangeRed)
End
If
cshape.UpdateModelBounds()
cshape.CreateShapeElements(ShapeElementsMask.Ports)
cshape.Ports.AddChild(
New
NDynamicPort(
New
NContentAlignment(), DynamicPortGlueMode.GlueToContour))
End
Sub
Public
Class
MyShapeFactory
Implements
INShapesFactory
#Region "INShapesFactory Members"
Public
Function
CanCreateShape(
ByVal
name
As
String
)
As
Boolean
Implements
Nevron.Diagram.INShapesFactory.CanCreateShape
Return
True
End
Function
Public
Function
CreateShape(
ByVal
name
As
String
)
As
NShape
Implements
Nevron.Diagram.INShapesFactory.CreateShape
Return
New
NCompositeShape()
End
Function
#End Region
End
Class
A simple shape factory is implemented, which always returns a composite shape. In the VertexImported handler the composite shape geometry primitives are initialized depending on the data record for which the vertex is created. By default if an edge factory is not assigned the data importer will create NRoutableConnector instances. These connectors have a single label of type NLogicalLineLabel which is anchored to 50% of the connector length. Setting the Text property of the connector will actually set this label text value.
Article ID: 82, Created On: 10/8/2010, Modified: 12/1/2010