Show the names of nodes and edges using GraphStream in scala

450 Views Asked by At

I am working on creating a graph using GraphStream in Scala.

A small verison of the code is the following :

    val graph = new SingleGraph("Tutorial 1")

    graph.addNode("A")
    graph.addNode("B")
    graph.addNode("C")
    graph.addEdge("AB", "A", "B")
    graph.addEdge("BC", "B", "C")
    graph.addEdge("CA", "C", "A")

    System.setProperty("org.graphstream.ui", "swing")
    graph.display

This code gives the following result :

graph

I've looked in the official documentation of GraphStream but I couldn't find how to make the Ids of Nodes and Edges appear. So my question is, how can I add text to my graph ?

2

There are 2 best solutions below

0
On

Here you are

System.setProperty("org.graphstream.ui", "swing");

var graph = new SingleGraph("Tutorial 1");
graph.setAttribute("ui.stylesheet", "node{\n" +
                "    size: 30px, 30px;\n" +
                "    fill-color: #f7f7f0;\n" +
                "    text-mode: normal; \n" +
                "}");

graph.addNode("A").setAttribute("ui.label", "A");
graph.addNode("B").setAttribute("ui.label", "B");
graph.addNode("C").setAttribute("ui.label", "C");
graph.addEdge("AB", "A", "B").setAttribute("ui.label", "AB");
graph.addEdge("BC", "B", "C");
graph.addEdge("CA", "C", "A");

graph.display();
0
On

You can specify this in the CSS stylesheet of the graphstream. Simply use text-mode: normal. Stylesheet can be set by graph.setAttribute("ui.stylesheet", myStyleSheet)

Example from my stylesheet:

node{
    size: 10px, 10px;
    shape: circle;
    fill-color: #B1DFF7;
    stroke-mode: plain;
    stroke-color: #B1DFF7;
    stroke-width: 3;
    text-mode: normal; /*normal or hidden*/
    text-background-mode: plain; /*plain or none*/
    text-background-color: rgba(255, 255, 255, 200);
    text-alignment: above;
}