Collapsing and Expanding of Vertex Groups

Oct 28, 2011 at 6:13 AM
Edited Oct 28, 2011 at 7:16 AM

I am running the following source code. For a vertex click, the Vertex Group 1 collapses. I want to expand the same Vertex Group 1 for the next Vertex Click. I would like to know how do i do it. As i am new to the dot.net platform, any help would be appreciated. Thank you.

--Pradeep

Oct 28, 2011 at 6:16 AM
Edited Oct 28, 2011 at 7:18 AM

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.NodeXL.Core;
using Microsoft.NodeXL.Visualization.Wpf;


namespace NodeXL_Form_app
{
public partial class Form1 : Form
{
    private NodeXLControl nodeXLControl1;

    public Form1()
    {
        InitializeComponent();
        nodeXLControl1 = new NodeXLControl();
        elementHost1.Child = nodeXLControl1;

        NodeXLControl1.MouseMode =  MouseMode.DoNothing;


        nodeXLControl1.VertexClick += new VertexEventHandler(
                btnCollapse_Click);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateAndDrawGraph();
    }

    protected void PopulateAndDrawGraph()
    {
        // Add vertices to the graph.

        IVertexCollection oVertices = nodeXLControl1.Graph.Vertices;

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

        IVertex oVertexD = oVertices.Add();
        IVertex oVertexE = oVertices.Add();

        // Connect the vertices with directed edges.

        IEdgeCollection oEdges = nodeXLControl1.Graph.Edges;

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

        // Create vertex groups.

        GroupInfo oGroup1 = new GroupInfo("Group1", false);
        oGroup1.Vertices.Add(oVertexA);
        oGroup1.Vertices.Add(oVertexB);
        oGroup1.Vertices.Add(oVertexC);

        GroupInfo oGroup2 = new GroupInfo("Group2", false);
        oGroup2.Vertices.Add(oVertexD);
        oGroup2.Vertices.Add(oVertexE);

        // Add the groups to the graph.

        nodeXLControl1.Graph.SetValue(ReservedMetadataKeys.GroupInfo,
            new GroupInfo[] { oGroup1, oGroup2});

        nodeXLControl1.DrawGraph(true);
    }

    private void btnCollapse_Click(object sender, EventArgs e)
    {
        // Collapse one of the vertex groups.

        nodeXLControl1.CollapseGroup("Group1", true);
    }

    private void btnExpand_Click(object sender, EventArgs e)
    {
        // Expand one of the vertex groups.

        nodeXLControl1.ExpandGroup("Group1", true);
    }
}
}

 

Oct 28, 2011 at 7:25 PM

You're saying that you click a vertex to get it to collapse into a group, and now you want to click the group to get it to expand?  If so, there are a few tasks involved here.

First, in your VertexClick event handler, you need to determine whether the clicked vertex is a normal vertex or a collapsed group.  A collapsed group is distinguished by the ReservedMetadataKeys.CollapsedVertices key that the NodeXLControl adds to the vertex that represents a collapsed group (the one that has a plus sign), so you can check for that key:

Boolean clickedVertexIsCollapsedGroup = clickedVertex.ContainsKey(ReservedMetadataKeys.CollapsedVertices);

If the clicked vertex is a collapsed group then you need to expand it; otherwise you need to collapse the group the vertex belongs to.  In either case, you need some way to map a vertex to the group it belongs to.  One way to do this is to store the group name in each vertex's Tag property as you populate the graph:

oVertexA.Tag = "Group1";

Your VertexClick event handler will then look something like what you see in the next post.

-- Tony

Oct 28, 2011 at 7:28 PM

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Microsoft.NodeXL.Core;
using Microsoft.NodeXL.Visualization.Wpf;


namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
    private NodeXLControl nodeXLControl1;

    public Form1()
    {
        InitializeComponent();
        nodeXLControl1 = new NodeXLControl();
        nodeXLControl1.MouseMode = MouseMode.DoNothing;
        elementHost1.Child = nodeXLControl1;

        nodeXLControl1.VertexClick +=
            new VertexEventHandler(this.NodeXLControl1_VertexClick);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        PopulateAndDrawGraph();
    }

    protected void PopulateAndDrawGraph()
    {
        // Add vertices to the graph.

        IVertexCollection oVertices = nodeXLControl1.Graph.Vertices;

        IVertex oVertexA = oVertices.Add();
        oVertexA.Tag = "Group1";

        IVertex oVertexB = oVertices.Add();
        oVertexB.Tag = "Group1";

        IVertex oVertexC = oVertices.Add();
        oVertexC.Tag = "Group1";

        IVertex oVertexD = oVertices.Add();
        oVertexD.Tag = "Group2";

        IVertex oVertexE = oVertices.Add();
        oVertexE.Tag = "Group2";

        // Connect the vertices with directed edges.

        IEdgeCollection oEdges = nodeXLControl1.Graph.Edges;

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

        // Create vertex groups.

        GroupInfo oGroup1 = new GroupInfo("Group1", false);
        oGroup1.Vertices.Add(oVertexA);
        oGroup1.Vertices.Add(oVertexB);
        oGroup1.Vertices.Add(oVertexC);

        GroupInfo oGroup2 = new GroupInfo("Group2", false);
        oGroup2.Vertices.Add(oVertexD);
        oGroup2.Vertices.Add(oVertexE);

        // Add the groups to the graph.

        nodeXLControl1.Graph.SetValue(ReservedMetadataKeys.GroupInfo,
            new GroupInfo[] { oGroup1, oGroup2});

        nodeXLControl1.DrawGraph(true);
    }

    private void NodeXLControl1_VertexClick(object sender, VertexEventArgs e)
    {
        IVertex clickedVertex = e.Vertex;

        Boolean clickedVertexIsCollapsedGroup =
            clickedVertex.ContainsKey(ReservedMetadataKeys.CollapsedVertices);

        if (clickedVertexIsCollapsedGroup)
        {
            // Get the group name from one of the collapsed group's vertices.

            ICollection<IVertex>verticesInGroup = (ICollection<IVertex>)
                clickedVertex.GetValue(ReservedMetadataKeys.CollapsedVertices);

            String groupName = (String)verticesInGroup.First().Tag;
            nodeXLControl1.ExpandGroup(groupName, true);
        }
        else
        {
            // Get the group name directly from the clicked vertex.

            String groupName = (String)clickedVertex.Tag;
            nodeXLControl1.CollapseGroup(groupName, true);
        }
    }

}
}