Table of Contents
This chapter describes how automatic layout and graph analysis algorithms from the yFiles Layout and yFiles Basic library components can be used with the IGraph-based graph structure of yFiles for Silverlight Viewer.
The yFiles for Silverlight Viewer graph model is constituted by interface IGraph, its actual
implementation, and interfaces INode, IEdge, etc. from the
yWorks.yFiles.UI.Model
namespace.
Together, these types present a model that differs from that used with the
yFiles Basic and yFiles Layout library components.
In order to use the algorithms provided by yFiles Basic and yFiles Layout, a proper mapping of the graph models and any necessary data associated with its elements needs to be established. Using the adapter mechanisms provided by classes CopiedLayoutIGraph and YGraphAdapter, this mapping is done transparently.
Adapter class CopiedLayoutIGraph
bridges the gap between the IGraph-based yFiles for Silverlight Viewer graph model and the graph model
that is used with the yFiles Basic and yFiles Layout library components.
It implements interfaces IGraphInterface
and IGraphLayout
which together
provide the functionality that is needed for the yFiles automatic layout
and network analysis algorithms.
CopiedLayoutIGraph is also responsible for providing access to any additional
information associated with an IGraph and its elements in a way that is
standard with the graph model that the yFiles algorithms use.
Specifically, this means that any IMapper instances which are registered with the
IGraph's mapper registry
are appropriately wrapped using data providers.
Among other things, this also covers any grouping-related data that is present with
grouped graphs.
The following convenience method in CopiedLayoutIGraph can be used to invoke a layout algorithm:
static void ApplyLayout(IGraph graph, ILayouter layouter) |
|
| Description | Convenience method in CopiedLayoutIGraph. |
These methods in other classes basically delegate to the aforementioned in CopiedLayoutIGraph:
void ApplyLayout(ILayouter layouter) |
|
| Description | Convenience (extension) method(s) in IGraph. |
void DoLayout(IGraph graph) |
|
| Description | Convenience (extension) method in ILayouter. |
Invoking a layout algorithm can be as simple as this one-liner that implicitly uses the services of class CopiedLayoutIGraph:
Example 3.1. Invoking a layout algorithm
// 'graph' is of type yWorks.yFiles.UI.Model.IGraph. graph.ApplyLayout(new IncrementalHierarchicLayouter());
Using the services of class CopiedLayoutIGraph enables proper invocation of any
implementations of interface
ILayouter
via the
DoLayout
method.
This also includes all
yFiles major layout algorithms,
which are descendants of abstract class
CanonicMultiStageLayouter
.
Basically, the following code gets executed when using either of the aforementioned convenience methods to invoke a layout algorithm:
Example 3.2. Invoking an ILayouter implementation
void InvokeLayouter(ILayouter layouter, IGraph graph) {
CopiedLayoutIGraph copy = new CopiedLayoutIGraph(graph);
layouter.DoLayout(copy);
copy.CommitLayoutToOriginalGraph();
}
Committing back the results of the layout calculation to the original graph effectively prevents layout morphing, the effect of smoothly transforming node and bend positions from their original positions to the newly calculated ones. Enabling layout morphing is described in the section called “Layout Morphing”.
Connecting graph models especially in the context of calling
analysis algorithms
from the yFiles Basic library component is conveniently supported by adapter
class YGraphAdapter
.
Similar to class CopiedLayoutIGraph it creates a copy of the IGraph given at
creation time.
The copy is of type Graph
and can be readily used in conjunction with any algorithms from the
yWorks.yFiles.Algorithms
namespace.
It is accessible via the
YGraph
property of the adapter class.
Example 3.3. Invoking a graph analysis algorithm
// 'graph' is of type yWorks.yFiles.UI.Model.IGraph. YGraphAdapter graphAdapter = new YGraphAdapter(graph); // Class GraphChecker is from the yWorks.yFiles.Algorithms namespace. bool isConnected = GraphChecker.IsConnected(graphAdapter.YGraph);
Mappings between the graph elements from the IGraph and the copied graph elements in the Graph are available via convenience methods.
YGraphAdapter provides a number of methods that facilitate converting IMapper<K, V> or IEnumerable<T> instances both to and from appropriate data type instances which are typically used as parameter types with graph analysis algorithms. Note that conversion of IMapper<K, V> instances is done by wrapping them.
DataProvider CreateDataProvider<K, V>(IMapper<K, V> mapper) where K : class |
|
| Description | Converting to algorithm data types. |
DataMap CreateDataMap<K, V>(IMapper<K, V> mapper) where K : class |
|
| Description | Converting to algorithm data types. |
NodeList CreateNodeList(IEnumerable<INode> nl) |
|
| Description | Converting to algorithm data types. |
IEnumerable<INode> CreateNodeEnumerable(NodeList nl) |
|
| Description | Converting from algorithm data types. |
Example 3.4, “Converting data type instances when invoking a graph analysis algorithm” presents an invocation of an analysis algorithm where data type instances are converted to provide the appropriate input type and to get the algorithm's results from the output type.
Example 3.4. Converting data type instances when invoking a graph analysis algorithm
// 'graph' is of type yWorks.yFiles.UI.Model.IGraph. // 'someNodes' is of type System.Collections.Generic.IEnumerable<INode>. YGraphAdapter graphAdapter = new YGraphAdapter(graph); // Class GraphConnectivity is from the yWorks.yFiles.Algorithms namespace. NodeList inputNL = graphAdapter.CreateNodeList(someNodes); NodeList outputNL = GraphConnectivity.GetSuccessors(graphAdapter.YGraph, inputNL, 2); IEnumerable<INode> successors = graphAdapter.CreateNodeEnumerable(outputNL);
|
Copyright ©2004-2011, yWorks GmbH. All rights reserved. |