Data Import - Getting Started
In This Topic
It is often required to represent the relationships and data of a tabular data source as a diagram. The most common scenario is when the data relationships form tree or graph data structures and the data records involved in these relationships need to be displayed as cards, with entries for each particular data field. Nevron Diagram for .NET features built-in the NTreeDataSourceImporter and NGraphDataSourceImporter classes, which can help you perform these tedious operations.
Sample Dataset
For the purpose of explaining data import, lets assume that we have a simple dataset, which contains two table: Pages and Links.
Pages table |
Field |
Type |
Comments |
Id |
Auto Number |
page id |
ParentId |
Number |
id to the parent page |
Title |
string |
page title |
URL |
Hyper link |
page URL |
Links table |
Field |
Type |
Comments |
Id |
Auto Number |
link id |
FromPageId |
Number |
id of the the source page |
ToPageId |
Number |
id of the the destination page |
The Pages table contains information about the pages and the hierarchical structure of a website. The Links table contains information about the cross links inside this website.
The Pages table is a self-referencing table the records of which can be displayed as a tree - we will use the NTreeDataSourceImporter to do that.
The Pages and Links tables can be displayed as a graph the vertices of which are pages and the edges of which are page links - we will use the NGraphDataSourceImporter to perform this task.
Common Data Import Features
Both the NTreeDataSourceImporter and the NGraphDataSourceImporter classes derive from the base NDataSourceImporter class, which defines their common functionality:
Supported Data Sources
Data importers support the following types of data sources:
Type |
Comments |
DataTable |
|
DataView |
|
OleDbDataAdapter |
Uses the SelectCommand of the adapter |
SqlDataAdapter |
Uses the SelectCommand of the adapter |
OdbcDataAdapter |
Uses the SelectCommand of the adapter |
OleDbCommand |
|
SqlCommand |
|
OdbcCommand |
|
Vertex and Edge Shapes Factories
Since both tree and graphs structures are represented by shapes for their vertices and edges, you can use the VertexShapesFactory and EdgeShapesFactory properties to specify an instance of the INShapesFactory interface responsible for creating vertex and edge shapes respectively. The INShapesFactory interface is implemented by all shape factories shipped with Nevron Diagram. The name of the vertex or edge shape, which needs to be created is specified by the VertexShapeName and EdgeShapeName properties respectively.
Callbacks
Provided are the following events, which the data source importers will raise to notify you about their progress:
Event |
Description |
ImportStarted |
raised when the import operation has started |
VertexImported |
raised when the data source importer has created a vertex shape for a data record. You will typically subscribe to this event to fill the vertex shape with data record information. |
EdgeImported |
raised when the data source importer has created an edge shape for a data record. |
ImportSucceeded |
raised when the import has finished successfully. |
ImportFailed |
raised if the import failed. |
Tree Data Import
To perform tree data import you need a single data source. You also need to specify the Id column (must uniquely identify the records in the data source) and a ParentId column (which specifies the parent record for a given record).
The following code sample perform a tree data import, from an OleDbAdapter, assuming that it selects the Pages table described above:
C# |
Copy Code
|
...
// create the tree data source importer
treeImporter = new NTreeDataSourceImporter();
// set the document in the active layer of which the shapes will be imported
treeImporter.Document = document;
// set the data source
// NOTE: In this example we assume the that oleDbDataAdapter1 selects the Pages table described above
treeImporter.DataSource = oleDbDataAdapter1;
// records are uniquely identified by their Id column
// records link to their parent record by their ParentId column
treeImporter.IdColumnName = "Id";
treeImporter.ParentIdColumnName = "ParentId";
// create vertices as rectangles shapes
NBasicShapesFactory shapesFactory = new NBasicShapesFactory();
treeImporter.VertexShapesFactory = shapesFactory;
treeImporter.VertexShapesName = BasicShapes.Rectangle.ToString();
// use layered tree layout
NLayeredTreeLayout layout = new NLayeredTreeLayout();
layout.Direction = LayoutDirection.LeftToRight;
layout.OrthogonalEdgeRouting = true;
layout.LayerAlignment = RelativeAlignment.Near;
treeImporter.Layout = layout;
// subscribe for the vertex imported event,
// which is raised when a shape was created for a data source record
treeImporter.VertexImported += new ShapeImportedDelegate(OnVertexImported);
// import
treeImporter.Import();
...
private void OnVertexImported(NDataSourceImporter importer, NShape shape, INDataRecord dataRecord)
{
// display the page title in the shape
object text = dataRecord.GetColumnValue("Title");
if (text == null)
{
shape.Text = "Title not specified";
}
else
{
shape.Text = text.ToString();
}
shape.SizeToText(new NMarginsF(10));
// make the URL a tooltip of the shape
object url = dataRecord.GetColumnValue("URL");
if (url == null || url.ToString().Length == 0)
{
shape.Style.InteractivityStyle = new NInteractivityStyle("URL not specified");
}
else
{
shape.Style.InteractivityStyle = new NInteractivityStyle(url.ToString());
}
}
|
VB.NET |
Copy Code
|
...
' create the tree data source importer
treeImporter = New NTreeDataSourceImporter()
' set the document in the active layer of which the shapes will be imported
treeImporter.Document = document
' set the data source
' NOTE: In this example we assume the that oleDbDataAdapter1 selects the Pages table described above
treeImporter.DataSource = oleDbDataAdapter1
' records are uniquely identified by their Id column
' records link to their parent record by their ParentId column
treeImporter.IdColumnName = "Id"
treeImporter.ParentIdColumnName = "ParentId"
' create vertices as rectangles shapes
Dim shapesFactory As New NBasicShapesFactory()
treeImporter.VertexShapesFactory = shapesFactory
treeImporter.VertexShapesName = BasicShapes.Rectangle.ToString()
' use layered tree layout
Dim layout As New NLayeredTreeLayout()
layout.Direction = LayoutDirection.LeftToRight
layout.OrthogonalEdgeRouting = True
layout.LayerAlignment = RelativeAlignment.Near
treeImporter.Layout = layout
' subscribe for the vertex imported event,
' which is raised when a shape was created for a data source record
AddHandler treeImporter.VertexImported, AddressOf OnVertexImported
' import
treeImporter.Import()
...
Private Sub OnVertexImported(ByVal importer As NDataSourceImporter, ByVal shape As NShape, ByVal dataRecord As INDataRecord)
' display the page title in the shape
Dim text As Object = dataRecord.GetColumnValue("Title")
If text Is Nothing Then
shape.Text = "Title not specified"
Else
shape.Text = text.ToString()
End If
shape.SizeToText(New NMarginsF(10))
' make the URL a tooltip of the shape
Dim url As Object = dataRecord.GetColumnValue("URL")
If url Is Nothing Or url.ToString().Length = 0 Then
shape.Style.InteractivityStyle = New NInteractivityStyle("URL not specified")
Else
shape.Style.InteractivityStyle = New NInteractivityStyle(url.ToString())
End If
End Sub
|
Graph Data Import
To perform graph data import you need a two data sources. The first data source must contain the vertex records, while the second data source must contain the edge records. You also need to specify the Id column in vertex data source (must uniquely identify the records in it), as well as specify the FromId and ToId columns in the edges data source, which define the from and to vertex records.
The following code sample perform a graph data import, from two OleDbAdapters, assuming that they selects the Pages and Links tables described above:
C# |
Copy Code
|
// configure the graph data source importer
graphImporter = new NGraphDataSourceImporter();
// set the document in the active layer of which the shapes will be imported
graphImporter.Document = document;
// set the data source
// NOTE: In this example we assume the that PagesDataAdapter selects the Pages table
// while the LinksDataAdapter selects the Links table
graphImporter.VertexDataSource = PagesDataAdapter;
graphImporter.EdgeDataSource = LinksDataAdapter;
// vertex records are uniquely identified by their Id (in the Pages table)
// edges link the vertices with the FromPageId and ToPageId (in the Links table)
graphImporter.VertexIdColumnName = "Id";
graphImporter.FromVertexIdColumnName = "FromPageId";
graphImporter.ToVertexIdColumnName = "ToPageId";
// create vertices as rectangle shapes
NBasicShapesFactory shapesFactory = new NBasicShapesFactory();
graphImporter.VertexShapesFactory = shapesFactory;
graphImporter.VertexShapesName = BasicShapes.Rectangle.ToString();
// use layered graph layout NLayeredGraphLayout
layout = new NLayeredGraphLayout();
layout.Direction = LayoutDirection.LeftToRight;
layout.LayerAlignment = RelativeAlignment.Near;
graphImporter.Layout = layout;
// subscribe for the vertex imported event,
// which is raised when a shape was created for a data source record
graphImporter.VertexImported += new ShapeImportedDelegate(OnVertexImported);
// import
graphImporter.Import();
...
private void OnVertexImported(NDataSourceImporter importer, NShape shape, INDataRecord dataRecord)
{
// display the page title in the shape
object text = dataRecord.GetColumnValue("Title");
if (text == null)
{
shape.Text = "Title not specified";
}
else
{
shape.Text = text.ToString();
}
shape.SizeToText(new NMarginsF(10));
// make the URL a tooltip of the shape
object url = dataRecord.GetColumnValue("URL");
if (url == null || url.ToString().Length == 0)
{
shape.Style.InteractivityStyle = new NInteractivityStyle("URL not specified");
}
else
{
shape.Style.InteractivityStyle = new NInteractivityStyle(url.ToString());
}
}
|
VB.NET |
Copy Code
|
' configure the graph data source importer
graphImporter = New NGraphDataSourceImporter()
' set the document in the active layer of which the shapes will be imported
graphImporter.Document = document
' set the data source
' NOTE: In this example we assume the that PagesDataAdapter selects the Pages table
' while the LinksDataAdapter selects the Links table
graphImporter.VertexDataSource = PagesDataAdapter
graphImporter.EdgeDataSource = LinksDataAdapter
' vertex records are uniquely identified by their Id (in the Pages table)
' edges link the vertices with the FromPageId and ToPageId (in the Links table)graphImporter.VertexIdColumnName = "Id"
graphImporter.FromVertexIdColumnName = "FromPageId"
graphImporter.ToVertexIdColumnName = "ToPageId"
' create vertices as rectangle shapes
Dim shapesFactory As New NBasicShapesFactory()
graphImporter.VertexShapesFactory = shapesFactory
graphImporter.VertexShapesName = BasicShapes.Rectangle.ToString()
' use layered graph layout NLayeredGraphLayout layout = New NLayeredGraphLayout()
layout.Direction = LayoutDirection.LeftToRight
layout.LayerAlignment = RelativeAlignment.Near
graphImporter.Layout = layout
' subscribe for the vertex imported event,
' which is raised when a shape was created for a data source record
AddHandler graphImporter.VertexImported, AddressOf OnVertexImported
' import
graphImporter.Import()
...
Private Sub OnVertexImported(ByVal importer As NDataSourceImporter, ByVal shape As NShape, ByVal dataRecord As INDataRecord)
' display the page title in the shape
Dim text As Object = dataRecord.GetColumnValue("Title")
If text Is Nothing Then
shape.Text = "Title not specified"
Else
shape.Text = text.ToString()
End If
shape.SizeToText(New NMarginsF(10))
' make the URL a tooltip of the shape
Dim url As Object = dataRecord.GetColumnValue("URL")
If url Is Nothing Or url.ToString().Length = 0 Then
shape.Style.InteractivityStyle = New NInteractivityStyle("URL not specified")
Else
shape.Style.InteractivityStyle = New NInteractivityStyle(url.ToString())
End If
End Sub
|
Related Examples
Windows Forms: Data Import Folder
Web Forms: Data Import Folder