A piece of code using MSAGL with the GDI viewer is able to make an acceptable rendition of a graph with subgraphs, e.g.:
open System.Windows.Controls
open System.Windows.Media
open Microsoft.Msagl.Drawing
type GraphViewer = Microsoft.Msagl.GraphViewerGdi.GViewer
let addNamespaceToGraph (graph: Graph) (ns: string) =
let rec loop (parentGraph: Subgraph) (path: string list) (names: string list) =
match names with
| [] -> ()
| name :: rest ->
let path' = name::path
let subNamespace = String.Join('.', path' |> List.rev)
let subgraph' =
match parentGraph.Subgraphs |> Seq.tryFind (fun sg -> sg.Id = subNamespace) with
| Some sg -> sg
| None ->
let newSubgraph = new Subgraph(subNamespace)
newSubgraph.LabelText <- name
parentGraph.AddSubgraph(newSubgraph)
newSubgraph
loop subgraph' path' rest
let root = graph.RootSubgraph
let path = ns.Split('.') |> List.ofArray
loop root [] path
let createGraph () : Graph =
let graph = new Graph("Namespaces")
let addNamespace = addNamespaceToGraph graph
// Add namespaces
addNamespace "Foo.Bar.Baz"
addNamespace "Foo.Bar.Qux"
addNamespace "Foo.Baz"
// Add an edge between nodes in different subgraphs
graph.AddEdge("Foo.Bar.Baz", "Foo.Baz") |> ignore
graph
let showGraph (graph: Graph) =
let viewer = new GraphViewer()
viewer.Graph <- graph // Set the graph after layout is performed
viewer
let graph = createGraph()
showGraph graph |> Dump
Becomes:
However, almost identical code, using the WPF viewer makes an unusable rendition:
open System.Windows.Controls
open System.Windows.Media
open Microsoft.Msagl.Drawing
type GraphViewer = Microsoft.Msagl.WpfGraphControl.GraphViewer
let addNamespaceToGraph (graph: Graph) (ns: string) =
let rec loop (parentGraph: Subgraph) (path: string list) (names: string list) =
match names with
| [] -> ()
| name :: rest ->
let path' = name::path
let subNamespace = String.Join('.', path' |> List.rev)
let subgraph' =
match parentGraph.Subgraphs |> Seq.tryFind (fun sg -> sg.Id = subNamespace) with
| Some sg -> sg
| None ->
let newSubgraph = new Subgraph(subNamespace)
newSubgraph.LabelText <- name
parentGraph.AddSubgraph(newSubgraph)
newSubgraph
loop subgraph' path' rest
let root = graph.RootSubgraph
let path = ns.Split('.') |> List.ofArray
loop root [] path
let createGraph () : Graph =
let graph = new Graph("Namespaces")
let addNamespace = addNamespaceToGraph graph
// Add namespaces
addNamespace "Foo.Bar.Baz"
addNamespace "Foo.Bar.Qux"
addNamespace "Foo.Baz"
// Add an edge between nodes in different subgraphs
graph.AddEdge("Foo.Bar.Baz", "Foo.Baz") |> ignore
graph
let showGraph (graph: Graph) =
let viewer = new GraphViewer()
let canvas = new Canvas()
viewer.BindToPanel(canvas)
viewer.Graph <- graph // Set the graph after layout is performed
canvas
let graph = createGraph()
showGraph graph |> Dump
Becomes:
What can I do to fix the rendering with the WPF viewer, if anything?