graph icon indicating copy to clipboard operation
graph copied to clipboard

Graph attributes

Open lavrd opened this issue 2 years ago • 6 comments

Hi, as I see in https://github.com/dominikbraun/graph/blob/main/draw/draw.go current logic doesn't contains possibility to add graph attributes. Example:

strict digraph {
  rankdir="LR";
  dpi="200.0";

  "a" [  weight=0 ];
  "b" [  weight=0 ];
  "a" -> "b" [ weight=0 ];
}

I mean rankdir and dpi attributes.

Is it okay if I add this logic if it is really not present?

lavrd avatar Jan 19 '23 08:01 lavrd

Yes, this is a planned feature. The reason why it hasn't been implemented yet is that I wasn't sure about how the API for this should look like. Usually, graph takes an approach with functional options - in that case, you would create your graph like this:

g := graph.New(graph.StringHash, graph.Attribute("rankdir", "LR"), graph.Attribute("dpi", "200.0"))

This comes with the problem that New already accepts functional options to pass traits (e.g. Directed()). Traits are powered by the Traits type and attributes are backed by the Properties type, and you can't accept functional options for both types in New, which is why I planned to unify those types (see #66).

But I'm starting to see the drawbacks of functional options as they pollute the public package API. Therefore, I was thinking about an AddAttribute method:

g := graph.New(graph.IntHash)
g.AddAttribute("rankdir", "LR")
g.AddAttribute("dpi", "200.0")

This would be my preferred way right now. In any case, we have to reconcile this feature with #50. Would it make sense to you to merge #50 first and implement this feature on top of that, @geoah?

dominikbraun avatar Jan 19 '23 09:01 dominikbraun

Thanks for detailed explanation.

As I understand graph attributes useful only on final graph generation while executing:

draw.DOT(g, file)

Maybe it makes sense to add graph attributes to this function?

lavrd avatar Jan 19 '23 10:01 lavrd

Thanks for detailed explanation.

As I understand graph attributes useful only on final graph generation while executing:

draw.DOT(g, file)

Maybe it makes sense to add graph attributes to this function?

I also thought about that - it's definitely a possibility and pretty cheap to implement. I originally preferred my approach as a more sustainable and holistic solution, but now that I think about it, it might even make sense to have something like AddAttribute and and separate attributes for draw.DOT since there might be attributes that are only relevant when rendering the graph.

Maybe we just implement both... 🤔

dominikbraun avatar Jan 19 '23 13:01 dominikbraun

@dominikbraun yeah should be straightforward, can try to get 50 ready asap once #71 has been merged, and I'd be happy to try picking up the AddAttribute() implementation as well after that if you'd like.

geoah avatar Jan 21 '23 10:01 geoah

Support for global graph attributes for draw.DOT has been added in release v0.17.0:

_ = draw.DOT(g, file, draw.GraphAttribute("label", "my-graph"))

dominikbraun avatar Apr 12 '23 09:04 dominikbraun

Thanks!

lavrd avatar Apr 13 '23 12:04 lavrd