Graph in WPF using graph# isn't drawn as a chain

1.8k Views Asked by At

I'm using WPF with graph# library and I'm trying to to draw a graph as a linear chain, so I defined some vertices and the edges joining them as

new Edge<object>(vertices[i], vertices[i+1])

But the problem is that the resulting graph isn't drawn as expected, it's like the following:

1 -> 2 -> 3 -> 1-> 4

In other words, vertex 3 is passing through vertex 1 to reach vertex 4.

Here's the code of the drawing method

private void CreateGraphToVisualize()
{
    var g = new BidirectionalGraph<object, IEdge<object>>();

    // add the vertices to the graph
    string[] vertices = new string[5];
    for (int i = 0; i < 5; i++)
    {
        vertices[i] = i.ToString();
        g.AddVertex(vertices[i]);
    }

    // add edges to the graph
    g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
    g.AddEdge(new Edge<object>(vertices[1], vertices[2]));
    g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
    g.AddEdge(new Edge<object>(vertices[3], vertices[4]));

    GraphToVisualize = g;
}

And here's a part of the xaml code related to graph#

<DockPanel Grid.Row="2" Margin="10,10,13,10">
    <zoom:ZoomControl>
        <graphsharp:GraphLayout x:Name="graphLayout"
                            Graph="{Binding ElementName=root,Path=GraphToVisualize}" 
                            LayoutAlgorithmType="FR" 
                            OverlapRemovalAlgorithmType="FSA"
                            HighlightAlgorithmType="Simple"/>
    </zoom:ZoomControl>
</DockPanel>
1

There are 1 best solutions below

0
On

I've tried this code and from what I can see there's nothing wrong with it. It may be that some library-references are out of sync (QuickGraph-GraphSharp). I recompiled the Graph#-Controls and fit it all together in the one solution. I removed the Binding in XAML and loaded the graph in the constructor of MainWindow.

The resulting graph that I get from the code is: 0 -> 1 -> 2 -> 3-> 4 and not: 1 -> 2 -> 3 -> 1-> 4. You can download the full source-code here to see for yourself.


Further References