Пример #1
0
        /// <summary>
        /// Creates a small graph and applies a hierarchic group layout to it.
        /// The output of the calculated coordinates will be displayed in the
        /// console.
        /// </summary>
        public void Run()
        {
            DefaultLayoutGraph graph = new DefaultLayoutGraph();

            //construct graph. assign sizes to nodes
            Node v1 = graph.CreateNode();

            graph.SetSize(v1, 30, 30);
            Node v2 = graph.CreateNode();

            graph.SetSize(v2, 30, 30);
            Node v3 = graph.CreateNode();

            graph.SetSize(v3, 30, 30);
            Node v4 = graph.CreateNode();

            graph.SetSize(v4, 30, 30);

            Node groupNode = graph.CreateNode();

            graph.SetSize(groupNode, 100, 100);

            Edge e1 = graph.CreateEdge(v1, v2);
            Edge e2 = graph.CreateEdge(v4, groupNode);
            Edge e3 = graph.CreateEdge(v1, v3);
            Edge e4 = graph.CreateEdge(v1, v1);
            Edge e5 = graph.CreateEdge(v2, groupNode);
            Edge e6 = graph.CreateEdge(groupNode, v2);

            //optionally setup some edge groups
            IEdgeMap spg = graph.CreateEdgeMap();
            IEdgeMap tpg = graph.CreateEdgeMap();

            graph.AddDataProvider(PortConstraintKeys.SourceGroupIdDpKey, spg);
            graph.AddDataProvider(PortConstraintKeys.TargetGroupIdDpKey, tpg);

            spg.Set(e1, "SGroup1");
            spg.Set(e3, "SGroup1");
            tpg.Set(e1, "TGroup1");
            tpg.Set(e3, "TGroup1");

            //optionally setup the node grouping
            INodeMap nodeId       = graph.CreateNodeMap();
            INodeMap parentNodeId = graph.CreateNodeMap();
            INodeMap groupKey     = graph.CreateNodeMap();

            graph.AddDataProvider(GroupingKeys.NodeIdDpKey, nodeId);
            graph.AddDataProvider(GroupingKeys.ParentNodeIdDpKey, parentNodeId);
            graph.AddDataProvider(GroupingKeys.GroupDpKey, groupKey);

            //mark a node as a group node
            groupKey.SetBool(groupNode, true);

            // add ids for each node
            nodeId.Set(v1, "v1");
            nodeId.Set(v2, "v2");
            nodeId.Set(v3, "v3");
            nodeId.Set(v4, "v4");
            nodeId.Set(groupNode, "groupNode");

            // set the parent for each grouped node
            parentNodeId.Set(v2, "groupNode");
            parentNodeId.Set(v3, "groupNode");

            HierarchicLayout layout = new HierarchicLayout();

            layout.MinimumLayerDistance = 0;
            layout.EdgeLayoutDescriptor.MinimumDistance = 10;

            new BufferedLayout(layout).ApplyLayout(graph);

            Console.WriteLine("\n\nGRAPH LAID OUT USING HIERARCHICLAYOUT");
            Console.WriteLine("v1 center position = " + graph.GetCenter(v1));
            Console.WriteLine("v2 center position = " + graph.GetCenter(v2));
            Console.WriteLine("v3 center position = " + graph.GetCenter(v3));
            Console.WriteLine("v4 center position = " + graph.GetCenter(v4));
            Console.WriteLine("group center position = " + graph.GetCenter(groupNode));
            Console.WriteLine("group size = " + graph.GetSize(groupNode));
            Console.WriteLine("e1 path = " + graph.GetPath(e1));
            Console.WriteLine("e2 path = " + graph.GetPath(e2));
            Console.WriteLine("e3 path = " + graph.GetPath(e3));
            Console.WriteLine("e4 path = " + graph.GetPath(e4));
            Console.WriteLine("e5 path = " + graph.GetPath(e5));
            Console.WriteLine("e6 path = " + graph.GetPath(e4));

            //display the result in a simple viewer
            Application.Run(new Demo.yWorks.LayoutGraphViewer.GraphViewer(graph, "Hierarchical Group Layout"));
        }