Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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> 
like image 388
Luna28 Avatar asked Apr 15 '12 07:04

Luna28


People also ask

How to use Charts in WPF?

Before you can use any charting related functionality in a WPF application, you must download the WPF Toolkit. After that, you need to add a reference to an assembly. To add a reference, right-click the References folder of your project in Solution Explorer and select Add Reference.

How do you draw a line in WPF?

To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.

How do you make a pie Chart in WPF?

To add a Chart control to your page, just select the Chart control from the list. The list of charting related elements looks like Figure 4. The Chart element represents a WPF Chart control in XAML. The code snippet in Listing 1 creates a Chart and sets its width, height, and background properties of the Chart control.


1 Answers

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

  • QuickGraph, Graph Data Structures And Algorithms for .NET
  • Graph#
like image 89
pdvries Avatar answered Sep 23 '22 01:09

pdvries