goccy/go-graphviz

Help with graph layout

JoshPattman opened this issue · 1 comments

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.

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
}