2

I have an objects with data, and each object connected to other objects. (Like object1->object2, object2->object3, object1->object3). For example objects and connections represented by two classes:

class Object
{
    public int Id{get;set;}
    public string Name{get;set;}
    public Object(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

class Relation
{
    public Object obj1{get;set;}
    public Object obj2{get;set;}
    public Relation(Object o1, Object o2)
    {
        obj1 = o1;
        obj2 = o2;
    }
}

And, for example, data stored this way:

List<Relation> relations = new List<Relation>();
List<Object> objects = new List<Object>();

I generated a DGML (Directed Graph Markup Language) file in my solution, which contains info for a Visual Studio DGML Viewer to build a graph
I need to show its content to user through the form. I am using Windows Forms for my project. But there are no such control as VS DGML Viewer.
So I need another idea how to build directed graph automatically, because I cannot sort data to get correct looking graph made with System.Graphics. How can I achieve the goal?

Solution

I used a GraphX for .NET library to achieve my goal. This library allows to build different graphs based on data with automated visualization. But for windows forms needed to use ElementHost control to host WPF controls inside windows form.

For building graph I used this code:

private GraphExample GenerateGraph()
        {
            var dataGraph = new GraphExample();

            foreach (Object obj in objects)
            {
                var dataVertex = new DataVertex(obj.name);
                dataGraph.AddVertex(dataVertex);
            }

            var vlist = dataGraph.Vertices.ToList();

            foreach (Relation rel in relations)
            {
                var v1 = vlist.First(x => x.Text == rel.obj1.name);
                var v2 = vlist.First(x => x.Text == rel.obj2.name);
                var dataEdge = new DataEdge(v1, v2) { Text = $"From {v1.Text} to {v2.Text}" };
                dataGraph.AddEdge(dataEdge);
            }

            return dataGraph;
        }
baruchiro
  • 2,609
  • 2
  • 25
  • 43
Paul
  • 47
  • 7
  • What is DGML file? How does it look like? – Chetan Ranpariya Aug 01 '17 at 05:23
  • it look like directed graph with nodes connected by links, created using sml with "http://schemas.microsoft.com/vs/2009/dgml" scheme. Something like this: http://www.softwareprocessengineering.com/_SPDiag/DGMLNOGroups.jpg – Paul Aug 01 '17 at 05:29
  • Possible related question: https://stackoverflow.com/questions/5206450/is-there-a-dgml-viewer – ccalboni Aug 01 '17 at 07:58
  • Maybe I can convert dgml to image using code somehow? Or maybe there are some alternatives for automatic visual graph generation? – Paul Aug 01 '17 at 09:50

0 Answers0