2013-02-19 33 views
2

我嘗試使用CodePlex中的GraphSharp創建樹。GraphSharp Tree Layout LeftToRight

我查看了示例應用程序,並嘗試「重新設計」該示例。

問題是,如果我嘗試以編程方式設置LayoutAlgorithmType =「Tree」,我得到一個TargetInvocationException ...,因爲在它的工作原理中是神祕的。

我的問題是:如何創建一個圖形與樹佈局和方向從左到右。

感謝提前:)

我的代碼:

public partial class MainWindow : Window 
{ 
    private IBidirectionalGraph<object, IEdge<object>> _graphToVisualize; 

    public IBidirectionalGraph<object, IEdge<object>> GraphToVisualize 
    { 
     get { return _graphToVisualize; } 
    } 

    public MainWindow() 
    { 
     CreateGraphToVisualize(); 

     InitializeComponent(); 
    } 

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



     string[] vs = new string[5]; 
     for (int i = 0; i < 5; i++) 
     { 
      vs[i] = i.ToString(); 
      g.AddVertex(vs[i]); 
     } 

     //add some edges 
     g.AddEdge(new Edge<object>(vs[0], vs[1])); 
     g.AddEdge(new Edge<object>(vs[0], vs[2])); 
     g.AddEdge(new Edge<object>(vs[2], vs[3])); 
     g.AddEdge(new Edge<object>(vs[1], vs[4])); 
     g.AddEdge(new Edge<object>(vs[3], vs[4])); 

     layout.LayoutMode = LayoutMode.Automatic; 
     layout.LayoutAlgorithmType = "Tree"; 

     _graphToVisualize = g; 

    } 
} 

XAML:

<Window x:Class="Test.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:graphsharp="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls" 
     xmlns:GraphSharp_Controls="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls" 
     xmlns:graph="clr-namespace:GraphSharp.Controls;assembly=GraphSharp.Controls" 
     xmlns:Controls="clr-namespace:WPFExtensions.Controls;assembly=WPFExtensions" 

     Title="MainWindow" Height="350" Width="525" 
     x:Name="root"> 

    <Grid> 
     <Controls:ZoomControl> 
      <graph:GraphLayout x:Name="layout" /> 
     </Controls:ZoomControl> 

    </Grid> 
</Window> 

回答

4

看圖形#11b的源代碼後,我發現了一個解決方案由我自己。

首先,你必須將LayoutAlgorithm添加到您的命名空間:

xmlns:tree="clr-namespace:GraphSharp.Algorithms.Layout.Simple.Tree;assembly=GraphSharp" 

之後,你可以添加額外的LayoutParameter您GraphLayout。在我的情況下,我只是將樹的方向從TopToBottom更改爲LeftToRight。

<graphsharp:GraphLayout x:Name="graphLayout" 
           Graph="{Binding ElementName=root,Path=GraphToVisualize}" 
           LayoutAlgorithmType="Tree" 
           OverlapRemovalAlgorithmType="FSA" 
           HighlightAlgorithmType="Simple" RenderTransformOrigin="0.5,0.5"> 
      <graphsharp:GraphLayout.LayoutParameters> 
       <tree:SimpleTreeLayoutParameters Direction="LeftToRight"></tree:SimpleTreeLayoutParameters> 
      </graphsharp:GraphLayout.LayoutParameters> 
     </graphsharp:GraphLayout> 

所以,如果你想改變你不得不看的圖形(例如在Visual Studio的解決方案資源管理)的參數算法 - >佈局 - >簡單 - >樹 - > SimpleTreeLayoutParameters在我的情況。

1

在代碼:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void root_Loaded(object sender, RoutedEventArgs e) 
    { 
     CreateGraphToVisualize(); 
    } 

    private void CreateGraphToVisualize() 
    { 
     var g = new BidirectionalGraph<object, IEdge<object>>(); 
     string[] vs = new string[5]; 
     for (int i = 0; i < 5; i++) 
     { 
      vs[i] = i.ToString(); 
      g.AddVertex(vs[i]); 
     } 

     //add some edges 
     g.AddEdge(new Edge<object>(vs[0], vs[1])); 
     g.AddEdge(new Edge<object>(vs[0], vs[2])); 
     g.AddEdge(new Edge<object>(vs[2], vs[3])); 
     g.AddEdge(new Edge<object>(vs[1], vs[4])); 
     g.AddEdge(new Edge<object>(vs[3], vs[4])); 

     //Simple Tree layout parameter variable 
     GraphSharp.Algorithms.Layout.Simple.Tree.SimpleTreeLayoutParameters simpleTreeLayoutParameters = new GraphSharp.Algorithms.Layout.Simple.Tree.SimpleTreeLayoutParameters(); 
     //Simple Tree layout parameter variable values 
     simpleTreeLayoutParameters.Direction = GraphSharp.Algorithms.Layout.LayoutDirection.LeftToRight; //THIS IS WHAT YOU EXPECT 
     simpleTreeLayoutParameters.LayerGap = 10.0; 
     simpleTreeLayoutParameters.OptimizeWidthAndHeight = false; 
     simpleTreeLayoutParameters.SpanningTreeGeneration = GraphSharp.Algorithms.Layout.Simple.Tree.SpanningTreeGeneration.DFS; 
     simpleTreeLayoutParameters.VertexGap = 10.0; 
     simpleTreeLayoutParameters.WidthPerHeight = 2.0; 
     //Set layout 
     layout.LayoutAlgorithmType = "Tree"; 
     layout.LayoutParameters = simpleTreeLayoutParameters; 
     layout.LayoutMode = LayoutMode.Automatic; 
     layout.Graph = g; 
    } 
}