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;
}