Redrawing graph from scratch

Sep 19, 2008 at 1:50 PM
Hi all,

I'm using the following code to draw a simple and static graph:
public partial class Form1 : Form
    {
        private IVertexCollection vertexCollection;
        private IEdgeCollection edgeCollection;

        public Form1()
        {
            InitializeComponent();

            this.netMapControl1.BeginUpdate();

            this.netMapControl1.VertexDrawer = new PerVertexWithLabelDrawer();
            this.netMapControl1.EdgeDrawer = new PerEdgeWithLabelDrawer();

            ((PerVertexWithLabelDrawer)this.netMapControl1.VertexDrawer).Shape = VertexDrawer.VertexShape.Sphere;
            ((PerVertexWithLabelDrawer)this.netMapControl1.VertexDrawer).Color = Color.BlueViolet;
            ((PerVertexWithLabelDrawer)this.netMapControl1.VertexDrawer).Radius = 10.0f;

            vertexCollection = this.netMapControl1.Graph.Vertices;
            edgeCollection = this.netMapControl1.Graph.Edges;

            DrawVerticesAndEdges();

            this.netMapControl1.EndUpdate();
        }

        private void redrawToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.netMapControl1.BeginUpdate();
            DrawVerticesAndEdges();
            netMapControl1.EndUpdate();

        }

        private void DrawVerticesAndEdges()
        {
            vertexCollection.Clear();
            edgeCollection.Clear();

            IVertex vertexA = vertexCollection.Add();
            IVertex vertexB = vertexCollection.Add();
            IVertex vertexC = vertexCollection.Add();
            IVertex vertexD = vertexCollection.Add();
            IVertex vertexE = vertexCollection.Add();
            IVertex vertexF = vertexCollection.Add();
            IVertex vertexG = vertexCollection.Add();
            IVertex vertexH = vertexCollection.Add();

            edgeCollection.Add(vertexA, vertexC);
            edgeCollection.Add(vertexC, vertexB);
            edgeCollection.Add(vertexC, vertexD);
            edgeCollection.Add(vertexD, vertexE);
            edgeCollection.Add(vertexE, vertexF);
            edgeCollection.Add(vertexE, vertexG);
            edgeCollection.Add(vertexD, vertexH);
        }
    }

Now the first time, it will draw a very nice graph with 8 vertices connected to each other. Howerver, once i click the redraw menu item on my form (so  redrawToolStripMenuItem_Click will be executed), the graph will be redrawn, but every vertex will be centered.
It seems that the default layout isn't applied again.
What step am I missing here?

Thanks in advance,

David.
Sep 20, 2008 at 12:36 AM

David:

It's a bug, which I'll fix in the next release.  In the meantime, you can work around it by replacing your ToolStripMenuItem handler with the code below.

You're using the control in a way that I haven't adequately tested, which is dynamically adding and removing vertices and edges, so if you find additional bugs, please be sure to post them.  (I'm a one-man dev/test team, and I devoted most of my testing to the static case used by our Excel template.)

Thanks,
    Tony


private void redrawToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.netMapControl1.BeginUpdate();

 

    {
    // TEMPORARY workaround for bug in NetMapControl.
    this.netMapControl1.Graph.RemoveKey(ReservedMetadataKeys.LayoutBaseLayoutComplete);
    this.netMapControl1.DeselectAll();
    }

    DrawVerticesAndEdges();
    netMapControl1.EndUpdate();
}

Sep 20, 2008 at 10:30 AM
Tony,

Thanks again for your help. Your work around indeed does the trick.

I'll keep you informed when I'm encountering new bugs/issues with the NetMap classes.

Kind regards,

David.
Oct 1, 2008 at 2:45 PM
Hi

Thanks for this workaround - sorted my problem out as well.

Yuben