goccy / go-graphviz

Go bindings for Graphviz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Edge is not getting labeled

abhijithda opened this issue · comments

The Edge name or label is getting marked as key, and svg image generated doesn't show any name for the edge. (If I change key to label, then edge name shows up)

digraph "" {
	graph [bb="0,0,236.29,108"];
	node [fillcolor=lightgrey,
		label="\N"
	];
	subgraph preupgrade {
		graph [label=preupgrade];
		"A/a.preupgrade"		 [URL="sample/library/A/a.preupgrade",
			height=0.5,
			label="Checking for \"A\" settings",
			pos="118.15,90",
			style=filled,
			width=3.0807];
		"D/d.preupgrade"		 [URL="sample/library/D/d.preupgrade",
			height=0.5,
			label="Checking for \"D\" settings...",
			pos="118.15,18",
			style=filled,
			width=3.2818];
		"A/a.preupgrade" -> "D/d.preupgrade" [key=Requires,pos="e,112.17,36.413 112.19,71.831 111.44,64.131 111.22,54.974 111.53,46.417"];
	"D/d.preupgrade" -> "A/a.preupgrade" [key=RequiredBy,
	pos="e,124.1,71.831 124.12,36.413 124.85,44.059 125.07,53.108 124.76,61.573"];
}
}

Code related to above dot / image: https://github.com/VeritasOS/plugin-manager/blob/v2/vendor/github.com/abhijithda/pm-graph/v3/graph.go#L95-L103 .

image

I'm not sure if I fully understand your issue, but according to Graphviz's documentation, graph edges don't have an attribute called key. If you want to associate some text with an edge, you must use the label attribute.

If your issue is that the linked code incorrectly generates edge labels because it uses key instead of label in DOT syntax, then you should file an issue in that repo.

The code is incorrectly generating key instead of label for Edges.

TL;DR: To associate a label with an edge, you must call SetLabel on the edge. Ex:

e, _ := graph.CreateEdge("e1", n, m)
e.SetLabel("My Label")

Explanation

CreateEdge and it's underlying C counterpart agedge seem to be behaving as intended. Specifically, the "name" parameter in these functions is not supposed to be the label for the defined edges, rather the name is just a custom identifier to enable looking up existing edges.

In fact, successive calls of CreateEdgewill return an object representing the same edge. We can prove this with the following simple graph setup which calls CreateEdge twice with the same name ("e"):

func main() {
	g := graphviz.New()
	graph, _ := g.Graph()

	n, _ := graph.CreateNode("n")
	m, _ := graph.CreateNode("m")

	e1, _ := graph.CreateEdge("e", n, m)
	e1.SetColor("red")

	e2, _ := graph.CreateEdge("e", n, m)
	e2.SetLabel("g")

	var buf bytes.Buffer
	g.Render(graph, "dot", &buf)

	fmt.Println(buf.String())
}

Running the code above prints the following output:

digraph "" {
    graph [bb="0,0,54,124.8"];
    node [label="\N"];
    n      [height=0.5,
            pos="27,106.8",
            width=0.75];
    m      [height=0.5,
            pos="27,18",
            width=0.75];
    n -> m [key=e,
            color=red,
            label=g,
            lp="30.5,62.4",
            pos="e,27,36.072 27,88.401 27,76.295 27,60.208 27,46.467"];
}

As you can see, only one edge was created, and it has both the "red" color attribute of the e1, and the "g" label attribute of e2, demonstrating that e1 and e2 represent the same edge. In other words, it's better to think about the CreateEdge method more as CreateOrGetEdge.

So what does that mean for your code? Well, the value you provide as the edge "name" is not automatically the edge's label. In order to associate a label with an edge, you must explicitly call SetLabel on the edge object like so:

e, _ := graph.CreateEdge("e1", n, m)
e.SetLabel("My Label")

I thought, I read somewhere that by default name will be set as label. But not able to find that now.
Thanks for looking into it. I'll use SetLabel.