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

Label on edges #7

Open
gijsdh opened this issue Dec 24, 2024 · 2 comments
Open

Label on edges #7

gijsdh opened this issue Dec 24, 2024 · 2 comments

Comments

@gijsdh
Copy link

gijsdh commented Dec 24, 2024

How would I show a graph in kotlin notebooks with labels on verteces but also on edges?

@breandan
Copy link
Owner

breandan commented Dec 24, 2024

Hi @gijsdh, thanks for your question. This library is basically a wrapper for graphviz-java.

The default visualization for a graph is done here:

fun <G : IGraph<G, E, V>, E : IEdge<G, E, V>, V : IVertex<G, E, V>>
IGraph<G, E, V>.toGraphviz(): MutableGraph =
graph(directed = true, strict = true) {
val color = if (DARKMODE) WHITE else BLACK
edge[color, NORMAL, lineWidth(THICKNESS)]
graph[CONCENTRATE, Rank.dir(LEFT_TO_RIGHT),
TRANSPARENT.background(), margin(0.0),
COMPOUND, Attributes.attr("nslimit", "20")]
node[color, color.font(), Font.config("JetBrains Mono", 15),
lineWidth(THICKNESS), Attributes.attr("shape", "Mrecord")]
for (vertex in vertices) vertex.render()
for ((vertex, edge) in edgList)
edge.render().also { if (vertex is LGVertex && vertex.occupied) it.add(RED) }
}

It is called by .html() like so:

fun IGraph<*, *, *>.html() = toGraphviz().render(SVG).toString()

Alternatively, you can also output it as a PNG, which is one of the output formats supported by Kotlin Notebooks.

You can add a vertex and edge labels to a custom graph by converting it to a graphviz MutableGraph like so:

for (vertex in states) Factory.mutNode(vertex).add(Label.of(toString()))

(a.render() - b.render()).add(Label.of(""))

The visualization API is a bit clunky, but should do the job. Let me know if you have trouble getting it to work.

@gijsdh
Copy link
Author

gijsdh commented Dec 30, 2024

Wow thanks for the detailed answer :)

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