public void Clusters() { RootGraph root = RootGraph.CreateNew("Graph with clusters", GraphType.Directed); Node nodeA = root.GetOrAddNode("A"); Node nodeB = root.GetOrAddNode("B"); Node nodeC = root.GetOrAddNode("C"); Node nodeD = root.GetOrAddNode("D"); // When a subgraph name is prefixed with cluster, // the dot layout engine will render it as a box around the containing nodes. SubGraph cluster1 = root.GetOrAddSubgraph("cluster_1"); cluster1.AddExisting(nodeB); cluster1.AddExisting(nodeC); SubGraph cluster2 = root.GetOrAddSubgraph("cluster_2"); cluster2.AddExisting(nodeD); // COMPOUND EDGES // Graphviz does not really support edges from and to clusters. However, by adding an // invisible dummynode and setting the ltail or lhead attributes of an edge this // behavior can be faked. Graphviz will then draw an edge to the dummy node but clip it // at the border of the cluster. We provide convenience methods for this. // To enable this feature, Graphviz requires us to set the "compound" attribute to "true". Graph.IntroduceAttribute(root, "compound", "true"); // Allow lhead/ltail // The boolean indicates whether the dummy node should take up any space. When you pass // false and you have a lot of edges, the edges may start to overlap a lot. _ = root.GetOrAddEdge(nodeA, cluster1, false, "edge to a cluster"); _ = root.GetOrAddEdge(cluster1, nodeD, false, "edge from a cluster"); _ = root.GetOrAddEdge(cluster1, cluster1, false, "edge between clusters"); root.ComputeLayout(); SubGraph cluster = root.GetSubgraph("cluster_1"); RectangleF clusterbox = cluster.BoundingBox(); RectangleF rootgraphbox = root.BoundingBox(); Utils.AssertPattern(@"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}", clusterbox.ToString()); Utils.AssertPattern(@"{X=[\d.]+,Y=[\d.]+,Width=[\d.]+,Height=[\d.]+}", rootgraphbox.ToString()); }
public void GraphConstruction() { // You can programmatically construct graphs as follows RootGraph root = RootGraph.CreateNew("Some Unique Identifier", GraphType.Directed); // The node names are unique identifiers within a graph in Graphviz Node nodeA = root.GetOrAddNode("A"); Node nodeB = root.GetOrAddNode("B"); Node nodeC = root.GetOrAddNode("C"); Node nodeD = root.GetOrAddNode("D"); // The edge name is only unique between two nodes Edge edgeAB = root.GetOrAddEdge(nodeA, nodeB, "Some edge name"); Edge edgeBC = root.GetOrAddEdge(nodeB, nodeC, "Some edge name"); Edge anotherEdgeBC = root.GetOrAddEdge(nodeB, nodeC, "Another edge name"); // When a subgraph name is prefixed with cluster, // the dot layout engine will render it as a box around the containing nodes. SubGraph cluster = root.GetOrAddSubgraph("cluster_1"); cluster.AddExisting(nodeB); cluster.AddExisting(nodeC); // We can attach attributes to nodes, edges and graphs to store information and instruct // graphviz by specifying layout parameters. At the moment we only support string // attributes. Cgraph assumes that all objects of a given kind (graphs/subgraphs, nodes, // or edges) have the same attributes. The attributes first have to be introduced for a // certain kind, before we can use it. Node.IntroduceAttribute(root, "my attribute", "defaultvalue"); nodeA.SetAttribute("my attribute", "othervalue"); // To introduce and set an attribute at the same time, there are convenience wrappers edgeAB.SafeSetAttribute("color", "red", "black"); edgeBC.SafeSetAttribute("arrowsize", "2.0", "1.0"); // We can simply export this graph to a text file in dot format root.ToDotFile(TestContext.CurrentContext.TestDirectory + "/out.dot"); }