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

Help with graph layout #84

Open
JoshPattman opened this issue Feb 15, 2024 · 1 comment
Open

Help with graph layout #84

JoshPattman opened this issue Feb 15, 2024 · 1 comment

Comments

@JoshPattman
Copy link

I have a graph, and I want to set some nodes to be on the far left, some to be on the far right, and the rest can be anywhere in the middle. I have found some code that does what I want in the dot format directly, but I am not sure how to implement this using this package:

digraph G { 

  rankdir = LR;

  A -> B;
  A -> C -> D;
  X -> Y;

  { rank=min; A; X; } // These two lines do what I want
  { rank=max; B; D; Y; } 

}

I have already got all of the code to create and connect the nodes and render them, but I just need what will probably be the two lines of code to do the laying out.

@jayzhou215
Copy link

jayzhou215 commented May 28, 2024

I have a graph, and I want to set some nodes to be on the far left, some to be on the far right, and the rest can be anywhere in the middle. I have found some code that does what I want in the dot format directly, but I am not sure how to implement this using this package:

digraph G { 

  rankdir = LR;

  A -> B;
  A -> C -> D;
  X -> Y;

  { rank=min; A; X; } // These two lines do what I want
  { rank=max; B; D; Y; } 

}

I have already got all of the code to create and connect the nodes and render them, but I just need what will probably be the two lines of code to do the laying out.

Hi, try this~

func CreateGraph() (string, error) {
	g := graphviz.New()
	graph, err := g.Graph()
	if err != nil {
		return "", err
	}
	defer func() {
		_ = graph.Close()
		_ = g.Close()
	}()
	graph.SetRankDir(cgraph.TBRank)
	s1:=graph.SubGraph("rax", 1)
	s1.SafeSet(string(gographviz.Rank), "same", "")

	a, _ := graph.CreateNode("A")
	b, _:= graph.CreateNode("B")
	c, _:= s1.CreateNode("C")
	d, _:= graph.CreateNode("D")
	x, _:= s1.CreateNode("X")
	y, _:= graph.CreateNode("Y")
	_, _ = graph.CreateEdge("ab", a, b)
	_, _ = graph.CreateEdge("ac", a, c)
	_, _ = graph.CreateEdge("cd", c, d)
	_, _ = graph.CreateEdge("xy", x, y)

	err = g.RenderFilename(graph, graphviz.XDOT, "test.dot")
	data, _ := os.ReadFile("test.dot")
	fmt.Println(string(data))
	return "", err
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants