Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement an anonymous subgraph #59

Closed
csghuser opened this issue Jun 11, 2020 · 4 comments
Closed

How to implement an anonymous subgraph #59

csghuser opened this issue Jun 11, 2020 · 4 comments
Labels
documentation The answer in this is so good, it counts as documentation

Comments

@csghuser
Copy link

Hello, many thanks for this library!

I have a question which is duplicate of this issue here:

#9 (comment)

In a manual graphviz chart I have the following setup.

At the top of my graph I have defined:

  newrank=true;

As part of my graph I have the following anonymous graph:

{ rank=same; some_node_1; some_node_2; }  # How to inject this line?

This works, however similar to the issue linked above, how do I accomplish this in gographviz?

@awalterschulze
Copy link
Owner

Could you try parsing it with gographviz and reverse engineering it that way?
Please let me know what you find, so that I can try to help.

@csghuser
Copy link
Author

csghuser commented Jun 19, 2020

I managed to get an example of this working, before and after the changes.

The graph for this example has been borrowed from https://stackoverflow.com/q/6824431
Also the answer as well 🙂

g, _ := gographviz.Read([]byte(`digraph G {
    subgraph cluster1 {
        label="Local Datacenter";
        router1;
        host1;
    }
    subgraph cluster2 {
        label="Remote Datacenter";
        router2;
        host2;
    }
    router1 -> router2;
    router2 -> host2;
    router1 -> host1;
}`))
s := g.String()
fmt.Println(s)

This produces the following:

image

Where as we want them on the same level, so..... we add the following section to the code above:

g.AddAttr("G", "newrank", "true")
g.AddSubGraph("G", "force_node_same_level", map[string]string{"rank": "same"})
g.AddNode("force_node_same_level", "router1", nil)
g.AddNode("force_node_same_level", "router2", nil)
s := g.String()
fmt.Println(s)

Which gives us:

image

The dot file looks like this :

digraph G {
  newrank=true;
  router1->router2;
  router2->host2;
  router1->host1;
  subgraph cluster1 {
  label="Local Datacenter";
  host1;
  router1;

}
;
  subgraph cluster2 {
  label="Remote Datacenter";
  host2;
  router2;

}
;
  subgraph force_node_same_level {
  rank=same;
  router1;
  router2;

}
;

}

I was struggling to work out out to add the subgraph above but it's actually fairly straightforward. Hopefully this helps someone else! 🙂

@awalterschulze
Copy link
Owner

Thank you so much.
This is SUPER helpful.
I really appreciate the effort of not only finding the answer, but documenting it so well for others (including me).
Great job.

@awalterschulze awalterschulze added the documentation The answer in this is so good, it counts as documentation label Jun 27, 2020
@awalterschulze
Copy link
Owner

I am closing the issue, but I also added a link to this issue from the Readme, to make it easier for other users to find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation The answer in this is so good, it counts as documentation
Projects
None yet
Development

No branches or pull requests

2 participants