Applies to: Nevron Diagram for .NET

How to bind Diagram to an Excel spreadsheet?

You can easily bind Diagram to an Excel spreadsheet by using Microsoft's OLEDB provider for Excel. The following example demonstrates how to bind a simple diagram to an Excel spreadsheet.


Figure 1: the diagram generated from the Excell spreadsheet

[C#]
using System;
using System.Data.OleDb;
using System.Drawing;
using System.Windows.Forms;
using Nevron.Diagram;
using Nevron.Diagram.DataImport;
using Nevron.Diagram.Layout;
using Nevron.Diagram.Shapes;
using Nevron.GraphicsCore;
private void Form1_Load(object sender, 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.EndArrowheadStyle = new NArrowheadStyle(ArrowheadShape.SunkenArrow, "", new NSizeL(6, 4), new NColorFillStyle(Color.Black), new NStrokeStyle(1, Color.Black));
    edgeStyleSheet.Style.StrokeStyle = new NStrokeStyle(1, Color.Black); 
    DrawingDocument.StyleSheets.AddChild(edgeStyleSheet);
  
    // create the tree data source importer
    NTreeDataSourceImporter TreeImporter = new NTreeDataSourceImporter();
  
    // set the document in the active layer of which the shapes will be imported
    TreeImporter.Document = DrawingDocument;
  
    // SET THE DATA SOURCE
    // in this example we have created an OleDbDataAdapter, 
    // which selects all columns and records from the Sheet1 of the CompanyData Excel
    string connection = @"Data Source=""" + Application.StartupPath +
                                @"\CompanyData.xls"";Provider=""Microsoft.Jet.OLEDB.4.0""; Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";";
  
    OleDbDataAdapter DataAdapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection);
      
    TreeImporter.DataSource = DataAdapter;
  
    // 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, with default size (60,30)
    NBasicShapesFactory shapesFactory = new NBasicShapesFactory();
    shapesFactory.DefaultSize = new NSizeF(60, 30);
    TreeImporter.VertexShapesFactory = shapesFactory;
    TreeImporter.VertexShapesName = BasicShapes.Rectangle.ToString();
  
    // set stylesheets to be applied to imported vertices and edges
    TreeImporter.VertexStyleSheetName = "Vertices";
    TreeImporter.EdgeStyleSheetName = "Edges";
  
    // use layered tree layout
    NLayeredTreeLayout layout = new NLayeredTreeLayout();
    layout.Direction = LayoutDirection.LeftToRight;
    layout.OrthogonalEdgeRouting = true;
    layout.BusAlignment = 0.5f;
    layout.BusBetweenLayers = true;
    layout.CompactBreadth = true;
    layout.LayerAlignment = RelativeAlignment.Center;
    layout.LayerSpacing = 50;
    layout.VertexSpacing = 50;
    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();
  
    // set a shadow to the DrawingDocument. Since styles are inheritable all objects will reuse this shadow 
    DrawingDocument.Style.ShadowStyle = new NShadowStyle(
        ShadowType.GaussianBlur,
        Color.Gray,
        new NPointL(3, 3),
        1,
        new NLength(5));
  
    // shadows must be displayed behind document content 
    DrawingDocument.ShadowsZOrder = ShadowsZOrder.BehindDocument;
  
    // end view init 
    DrawingView.EndInit();
  
}
  
private void OnVertexImported(NDataSourceImporter importer, NShape shape, INDataRecord dataRecord)
{
    // display the Employee title in the shape
    object text = dataRecord.GetColumnValue("Name");
    if (text == null)
    {
        shape.Text = "Name not specified";
    }
    else
    {
        shape.Text = text.ToString();
    }
      
    shape.SizeToText(new NMarginsF(10));
  
    // make the employee position a tooltip of the shape
    object position = dataRecord.GetColumnValue("Position");
    if (position == null || position.ToString().Length == 0)
    {
        shape.Style.InteractivityStyle = new NInteractivityStyle("Position not specified");
    }
    else
    {
        shape.Style.InteractivityStyle = new NInteractivityStyle(position.ToString());
    }
}

 

[VB.NET]
Imports System
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms
Imports Nevron.Diagram
Imports Nevron.Diagram.DataImport
Imports Nevron.Diagram.Layout
Imports Nevron.Diagram.Shapes
Imports Nevron.GraphicsCore
Private Sub Form1_Load(ByVal sender As Object, ByVal e As 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.EndArrowheadStyle = New NArrowheadStyle(ArrowheadShape.SunkenArrow, "", New NSizeL(6, 4), New NColorFillStyle(Color.Black), New NStrokeStyle(1, Color.Black))
    edgeStyleSheet.Style.StrokeStyle = New NStrokeStyle(1, Color.Black)
    DrawingDocument.StyleSheets.AddChild(edgeStyleSheet)
  
    ' create the tree data source importer
    Dim TreeImporter As New NTreeDataSourceImporter()
  
    ' set the document in the active layer of which the shapes will be imported
    TreeImporter.Document = DrawingDocument
  
    ' SET THE DATA SOURCE
    ' in this example we have created an OleDbDataAdapter, 
    ' which selects all columns and records from the Sheet1 of the CompanyData Excel
    Dim connection As String = "Data Source=""" + Application.StartupPath + "\CompanyData.xls"";Provider=""Microsoft.Jet.OLEDB.4.0""; Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"";"
  
    Dim DataAdapter As New OleDbDataAdapter("SELECT * FROM [Sheet1$]", connection)
  
    TreeImporter.DataSource = DataAdapter
  
    ' 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, with default size (60,30)
    Dim shapesFactory As New NBasicShapesFactory()
    shapesFactory.DefaultSize = New NSizeF(60, 30)
    TreeImporter.VertexShapesFactory = shapesFactory
    TreeImporter.VertexShapesName = BasicShapes.Rectangle.ToString()
  
    ' set stylesheets to be applied to imported vertices and edges
    TreeImporter.VertexStyleSheetName = "Vertices"
    TreeImporter.EdgeStyleSheetName = "Edges"
  
    ' use layered tree layout
    Dim layout As New NLayeredTreeLayout()
    layout.Direction = LayoutDirection.LeftToRight
    layout.OrthogonalEdgeRouting = True
    layout.BusAlignment = 0.5F
    layout.BusBetweenLayers = True
    layout.CompactBreadth = True
    layout.LayerAlignment = RelativeAlignment.Center
    layout.LayerSpacing = 50
    layout.VertexSpacing = 50
    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()
  
    ' set a shadow to the DrawingDocument. Since styles are inheritable all objects will reuse this shadow 
    DrawingDocument.Style.ShadowStyle = New NShadowStyle(ShadowType.GaussianBlur, Color.Gray, New NPointL(3, 3), 1, New NLength(5))
  
    ' shadows must be displayed behind document content 
    DrawingDocument.ShadowsZOrder = ShadowsZOrder.BehindDocument
  
    ' end view init 
    DrawingView.EndInit()
  
End Sub
  
Private Sub OnVertexImported(ByVal importer As NDataSourceImporter, ByVal shape As NShape, ByVal dataRecord As INDataRecord)
    ' display the Employee title in the shape
    Dim text As Object = dataRecord.GetColumnValue("Name")
    If text Is Nothing Then
        shape.Text = "Name not specified"
    Else
        shape.Text = text.ToString()
    End If
  
    shape.SizeToText(New NMarginsF(10))
  
    ' make the employee position a tooltip of the shape
    Dim position As Object = dataRecord.GetColumnValue("Position")
    If position Is Nothing OrElse position.ToString().Length = 0 Then
        shape.Style.InteractivityStyle = New NInteractivityStyle("Position not specified")
    Else
        shape.Style.InteractivityStyle = New NInteractivityStyle(position.ToString())
    End If
End Sub

 

Article ID: 103, Created On: 10/28/2010, Modified: 12/1/2010