Automatically create a diagram from a database

Applies to: Nevron Diagram for .NET

How to automatically create a diagram from a database?

The Diagram Database Importer allows you to import the entire hierarchy of a database, which makes the task of creating database entity relation diagrams an easy task.

Nevron Diagram provides support for automatic import of tree and graph data structures from tabular data sources. The data import feature supports the following data sources: DataTable, DataView, OleDbDataAdapter, SqlDataAdapter, OdbcDataAdapter, OleDbCommand, SqlCommand, OdbcCommand. The data import provides you with the ability to control the data records information shown for each record.

The following example demonstrates how to use the NGraphDatasourceImporter to import and automatically arrange a graph data structures from a simple Access database.


Figure 1: the columns in the "Pages" table


Figure 2: the columns in the "Links" table


Figure 3: the automatically generated diagram

[C#]
using Nevron.GraphicsCore;
using Nevron.Diagram;
using Nevron.Diagram.Shapes;
using Nevron.Diagram.WinForm;
using Nevron.Diagram.Layout;
using Nevron.Diagram.DataImport;
private void Form1_Load(object sender, System.EventArgs e)
{
    // begin view init
    DrawingView.BeginInit();
  
    // display the document in the view
    DrawingView.Document = DrawingDocument;
  
    // configure the view
    DrawingView.ViewLayout = ViewLayout.Fit;
    DrawingView.Grid.Visible = false;
    DrawingView.GlobalVisibility.ShowPorts = false;
    DrawingView.HorizontalRuler.Visible = false;
    DrawingView.VerticalRuler.Visible = false;
      
    // create two stylesheets - one for the vertices and one for the edges
    NStyleSheet vertexStyleSheet = new NStyleSheet();
    vertexStyleSheet.Name = "Vertices";
    DrawingDocument.StyleSheets.AddChild(vertexStyleSheet);
  
    NStyleSheet edgeStyleSheet = new NStyleSheet();
    edgeStyleSheet.Name = "Edges";
    edgeStyleSheet.Style.StartArrowheadStyle = new NArrowheadStyle(ArrowheadShape.Circle, "", new NSizeL(5, 5), new NColorFillStyle(Color.Gray), new NStrokeStyle(1, Color.Black));
    edgeStyleSheet.Style.EndArrowheadStyle = new NArrowheadStyle(ArrowheadShape.Arrow, "", new NSizeL(5, 5), new NColorFillStyle(Color.Gray), new NStrokeStyle(1, Color.Black));
    DrawingDocument.StyleSheets.AddChild(edgeStyleSheet);
  
    // configure the graph data source importer
    NGraphDataSourceImporter GraphImporter = new NGraphDataSourceImporter();
  
    // set the document in the active layer of which the shapes will be imported
    GraphImporter.Document = DrawingDocument;
  
    // set the connection string, data sources and DataAdapters
    // in this example we have created two OleDbDataAdapters: 
    // the PagesDataAdapter selects all records and columns from the Pages table of the SiteMap.mdb
    // the LinksDataAdapter selects all records and columns from the Links table of the SiteMap.mdb
    string connString = @"Data Source=""" + Application.StartupPath +
                                        @"\SiteMap.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";";
    OleDbDataAdapter PagesDataAdapter = new OleDbDataAdapter("SELECT * FROM Pages", connString);
    OleDbDataAdapter LinksDataAdapter = new OleDbDataAdapter("SELECT * FROM Links", connString);
  
    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 rectangles shapes, with default size (60, 30)
    NBasicShapesFactory shapesFactory = new NBasicShapesFactory();
    shapesFactory.DefaultSize = new NSizeF(60, 30);
    GraphImporter.VertexShapesFactory = shapesFactory;
    GraphImporter.VertexShapesName = BasicShapes.Rectangle.ToString();
  
    // set stylesheets to be applied to imported vertices and edges
    GraphImporter.VertexStyleSheetName = "Vertices";
    GraphImporter.EdgeStyleSheetName = "Edges";
  
    // use layered graph layout
    NLayeredGraphLayout layout = new NLayeredGraphLayout();
    layout.Direction = LayoutDirection.TopToBottom;
    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();
  
    // end view init
    DrawingView.EndInit();
  
}
  
private void OnVertexImported(NDataSourceImporter dataSourceImporter, NShape shape, INDataRecord record)
{
    // display the page title in the shape
    object text = record.GetColumnValue("Title");
    if (text == null)
    {
        shape.Text = "Title not specified";
    }
    else
    {
        shape.Text = text.ToString();
    }
  
    shape.SizeToText(new NMarginsF(10));
      
}

[VB.NET]
...
Imports Nevron.GraphicsCore
Imports Nevron.Diagram
Imports Nevron.Diagram.Shapes
Imports Nevron.Diagram.WinForm
Imports Nevron.Diagram.Layout
Imports Nevron.Diagram.DataImport
...
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' begin view init 
    DrawingView.BeginInit()
  
    ' display the document in the view 
    DrawingView.Document = DrawingDocument
  
    ' configure the view 
    DrawingView.ViewLayout = ViewLayout.Fit
    DrawingView.Grid.Visible = False
    DrawingView.GlobalVisibility.ShowPorts = False
    DrawingView.HorizontalRuler.Visible = False
    DrawingView.VerticalRuler.Visible = False
  
    ' create two stylesheets - one for the vertices and one for the edges 
    Dim vertexStyleSheet As New NStyleSheet()
    vertexStyleSheet.Name = "Vertices"
    DrawingDocument.StyleSheets.AddChild(vertexStyleSheet)
  
    Dim edgeStyleSheet As New NStyleSheet()
    edgeStyleSheet.Name = "Edges"
    edgeStyleSheet.Style.StartArrowheadStyle = New NArrowheadStyle(ArrowheadShape.Circle, "", New NSizeL(5, 5), New NColorFillStyle(Color.Gray), New NStrokeStyle(1, Color.Black))
    edgeStyleSheet.Style.EndArrowheadStyle = New NArrowheadStyle(ArrowheadShape.Arrow, "", New NSizeL(5, 5), New NColorFillStyle(Color.Gray), New NStrokeStyle(1, Color.Black))
    DrawingDocument.StyleSheets.AddChild(edgeStyleSheet)
  
    ' configure the graph data source importer 
    Dim GraphImporter As New NGraphDataSourceImporter()
  
    ' set the document in the active layer of which the shapes will be imported 
    GraphImporter.Document = DrawingDocument
  
    ' set the connection string, data sources and DataAdapters 
    ' in this example we have created two OleDbDataAdapters:  
    ' the PagesDataAdapter selects all records and columns from the Pages table of the SiteMap.mdb 
    ' the LinksDataAdapter selects all records and columns from the Links table of the SiteMap.mdb 
    Dim connString As String = "Data Source=""" + Application.StartupPath + "\SiteMap.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";"
    Dim PagesDataAdapter As New OleDbDataAdapter("SELECT * FROM Pages", connString)
    Dim LinksDataAdapter As New OleDbDataAdapter("SELECT * FROM Links", connString)
  
    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 rectangles shapes, with default size (60, 30) 
    Dim shapesFactory As New NBasicShapesFactory()
    shapesFactory.DefaultSize = New NSizeF(60, 30)
    GraphImporter.VertexShapesFactory = shapesFactory
    GraphImporter.VertexShapesName = BasicShapes.Rectangle.ToString()
  
    ' set stylesheets to be applied to imported vertices and edges 
    GraphImporter.VertexStyleSheetName = "Vertices"
    GraphImporter.EdgeStyleSheetName = "Edges"
  
    ' use layered graph layout 
    Dim layout As New NLayeredGraphLayout()
    layout.Direction = LayoutDirection.TopToBottom
    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()
  
    ' end view init 
    DrawingView.EndInit()
  
End Sub
  
Private Sub OnVertexImported(ByVal dataSourceImporter As NDataSourceImporter, ByVal shape As NShape, ByVal record As INDataRecord)
    ' display the page title in the shape 
    Dim text As Object = record.GetColumnValue("Title")
    If text Is Nothing Then
        shape.Text = "Title not specified"
    Else
        shape.Text = text.ToString()
    End If
  
    shape.SizeToText(New NMarginsF(10))
  
End Sub
  
Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
  
End Sub

Article ID: 27, Created On: 10/4/2010, Modified: 12/1/2010