How to customize vertices when using the .NetMap class libraries

Aug 29, 2008 at 7:40 PM
Edited Aug 29, 2008 at 7:49 PM

Here is sample C# code that uses a NetMapControl in a Windows Forms application.  It adds vertices to the graph and customizes their appearance.



using System;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.NetMap.Visualization;
using Microsoft.NetMap.Core;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        netMapControl1.BeginUpdate();

        // The default vertex drawer draws all vertices with the same
        // color, radius, and shape.  Replace it with one that will vary
        // the appearance of each vertex based on metadata values stored in
        // the vertices.

        netMapControl1.VertexDrawer = new PerVertexWithLabelDrawer();

        // Replace the default edge drawer with one that will vary the color
        // and width of each edge based on metadata values stored in the edges.

        netMapControl1.EdgeDrawer = new PerEdgeWithLabelDrawer();

        // Get the graph's vertex collection.

        IVertexCollection oVertices = netMapControl1.Graph.Vertices;

        // Add three vertices.

        IVertex oVertexA = oVertices.Add();
        IVertex oVertexB = oVertices.Add();
        IVertex oVertexC = oVertices.Add();

        // Change the color, radius, and shape of vertex A.

        oVertexB.SetValue(ReservedMetadataKeys.PerColor, Color.Orange);
        oVertexB.SetValue(ReservedMetadataKeys.PerVertexRadius, 20F);
        oVertexB.SetValue(ReservedMetadataKeys.PerVertexShape,
            VertexDrawer.VertexShape.Sphere);

        // Draw vertex B as a primary label instead of a shape.  A primary
        // label is a box containing text.

        oVertexA.SetValue(ReservedMetadataKeys.PerVertexPrimaryLabel,
            "Primary Label");

        // Set the primary label's text and fill colors.

        oVertexA.SetValue(ReservedMetadataKeys.PerColor, Color.White);
        oVertexA.SetValue(ReservedMetadataKeys.PerVertexPrimaryLabelFillColor,
            Color.Black);

        // Add a secondary label to vertex C.  A secondary label is text that
        // is drawn outside the vertex.  It can be added to a shape, image, or
        // primary label.

        oVertexC.SetValue(ReservedMetadataKeys.PerVertexSecondaryLabel,
            "Secondary Label");

        // Get the graph's edge collection.

        IEdgeCollection oEdges = netMapControl1.Graph.Edges;

        // Connect the vertices with directed edges.

        IEdge oEdge1 = oEdges.Add(oVertexA, oVertexB, true);
        IEdge oEdge2 = oEdges.Add(oVertexB, oVertexC, true);
        IEdge oEdge3 = oEdges.Add(oVertexC, oVertexA, true);

        // Customize their appearance.

        oEdge1.SetValue(ReservedMetadataKeys.PerColor, Color.Chartreuse);
        oEdge1.SetValue(ReservedMetadataKeys.PerEdgeWidth, 3);

        oEdge2.SetValue(ReservedMetadataKeys.PerEdgeWidth, 5);

        oEdge3.SetValue(ReservedMetadataKeys.PerColor, Color.ForestGreen);

        netMapControl1.EndUpdate();
    }
}
}

 

May 19, 2011 at 8:30 PM

Is it still possible to customise how vertices are drawn in NodeXL?  As far as I can see, it is no longer possible to replace the VertexDrawer on the NodeXLControl object.  The only likely-looking candidate is the GraphDrawer.VertexDrawer property, but it is read-only.  Is there some other way to supply custom drawing code?

What would be really cool would be if we could use a XAML template to draw nodes... but it looks like a lot of work to implement.

May 19, 2011 at 10:27 PM
Edited May 19, 2011 at 10:28 PM

Tom:

I rewrote the NodeXLControl some time back to use WPF instead of GDI+, and in the process I vastly simplified the control's architecture.  The old control was trying to be too many things to too many people, and it was too hard to test and maintain as a result.  One of the features I dropped was pluggable drawing code.

The WPF version of NodeXLControl doesn't use XAML templates, but you can still customize the vertex appearance.  Create an image of what you want the vertex to look like, then tell NodeXL to use the image:

System.Windows.Media.ImageSource image = GetMyVertexImage();
vertex.SetValue(ReservedMetadataKeys.PerVertexShape, VertexShape.Image);
vertex.SetValue(ReservedMetadataKeys.PerVertexImage, image);

See the ReservedMetadataKeys Members topic in the NodeXLApi.chm help file for details.

-- Tony