Skip to content

GemGraph

GemGraph is a graphical representation of a network. It is composed of nodes and edges. Nodes are the entities of the network, and edges are the relationships between the nodes.

Usage Example

/* (c) https://github.com/MontiCore/monticore */
package example_network;

import mc.fenix.arrange.GemColumn;
import common.ExamplePage;
import mc.fenix.networkgraph.GemGraph;
import mc.fenix.basic.GemText;

page GemGraphExample(String lastClickedNodeName = "None") {
  @ExamplePage(
    component = @GemColumn(width = "100%", components = [
      @GemText(value="Last Clicked Node: " + lastClickedNodeName),
      personGraph@GemGraph(
        graph=null,
        showFilterGroup=true,
        showSearchNode=true,
        showControlButtons=true,
        showInformation=true,
        nodePositionsAfterLayout=getPositions(),
        graphClickNode=updateClickedNode(),
        customBoundingBox = null,
        initialLayout = null
      )
    ]),
    basePath = "example_network",
    componentName = "GemGraphExample"
  );
}
GemGraphExample

TypeScript implementation:

/* (c) https://github.com/MontiCore/monticore */
package example_network;

import mc.fenix.networkgraph.networkgraphtypes.Graph;
import mc.fenix.networkgraph.networkgraphtypes.GraphBuilder;
import mc.fenix.networkgraph.networkgraphtypes.GraphEdge;
import mc.fenix.networkgraph.networkgraphtypes.GraphEdgeBuilder;
import mc.fenix.networkgraph.networkgraphtypes.GraphNode;
import mc.fenix.networkgraph.networkgraphtypes.GraphNodeBuilder;
import mc.fenix.networkgraph.networkgraphtypes.InitialGraphLayout;
import mc.fenix.networkgraph.networkgraphtypes.InitialGraphLayoutBuilder;
import mc.fenix.networkgraph.networkgraphtypes.InitialGraphLayoutMode;
import umlp.jsweet.extension.annotation.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import de.se_rwth.commons.logging.Log;

@Component(
    imports = {"...config.imports", "GemGraph"},
    tsImports = {"GemGraph, @umlp/networkgraph"}
)
public class GemGraphExample extends GemGraphExampleTOP {

  protected List<GraphNode> nodes = new ArrayList<>();
  protected List<GraphEdge> edges = new ArrayList<>();

  @Override
  public void init() {
    Optional<GraphNode> node = createGraphNode("1", "Node 1", "Cluster A", Optional.of(100), Optional.of("triangle"));
    node.ifPresent(n -> nodes.add(n));
    //node = createGraphNode("12", "Node 1", "Cluster A", Optional.of(100), Optional.of("square"));
    //node.ifPresent(n -> nodes.add(n));
    //node = createGraphNode("13", "Node 1", "Cluster A", Optional.of(100), Optional.of("circle"));
    //node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("2", "Node 2", "Cluster A", Optional.of(35), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("3", "Node 3", "Cluster A", Optional.of(44), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("4", "Node 4", "Cluster A", Optional.of(55), Optional.empty());
    if (node.isPresent()) {
      node.get().setHoverLabel(Optional.of("another hover label!"));
      node.get().setColor(Optional.of("#ffc0cb"));
      nodes.add(node.get());
    }
    node = createGraphNode("5", "Node 5", "Cluster A", Optional.of(54), Optional.empty()); //fixed:true, x:0, y:0, size:54, tooltip: "custom Tooltip!"
    if (node.isPresent()) {
      node.get().setTooltip(Optional.of("custom Tooltip!"));
      //node.get().setFixed(Optional.of(true));
      //node.get().setX(Optional.of(0.0));
      //node.get().setY(Optional.of(0.0));
      nodes.add(node.get());
    }
    node = createGraphNode("6", "Node 6", "Cluster B", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("7", "Node 7", "Cluster B", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("8", "Node 8", "Cluster B", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("9", "Node 9", "Cluster B", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("10", "Node 10", "Cluster B", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
   // node = createGraphNode("11", "Node 11", "Cluster B", Optional.empty(), Optional.empty()); // hover:"No Group Demo!"
    if (node.isPresent()) {
      node.get().setHoverLabel(Optional.of("No Group Demo!"));
      nodes.add(node.get());
    }


    Optional<GraphEdge> edge = createGraphEdge("1", "1", "2", "Edge 1-2", Optional.of("#fc4c4e"));
    if (edge.isPresent()) {
      edge.get().setSize(Optional.of(25));
      edge.get().setDirected(Optional.of(true));
      edges.add(edge.get());
    }
    edge = createGraphEdge("2", "2", "3", "Edge 2-3", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("3", "3", "3", "Edge 3-4", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("4", "4", "5", "Edge 4-5", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("5", "1", "5", "Edge 1-5", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("6", "2", "4", "Edge 2-4", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("7", "6", "7", "Edge 6-7", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("8", "7", "8", "Edge 7-8", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("9", "8", "9", "Edge 8-9", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("10", "9", "10", "Edge 9-10", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("11", "6", "10", "Edge 6-10", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("12", "7", "9", "Edge 7-9", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("13", "1", "6", "Edge 1-6", Optional.of("#fc4c4e"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("14", "5", "10", "Edge 5-10", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("15", "3", "8", "Edge 3-8", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("16", "1", "3", "Edge 1-3", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("17", "2", "5", "Edge 2-5", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("18", "4", "1", "Edge Edge 4-1", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("19", "6", "8", "Edge 6-8", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("20", "7", "10", "Edge 7-10", Optional.empty());
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("21", "9", "6", "Edge 9-6", Optional.empty());
    edge.ifPresent(e -> edges.add(e));

    // Second disconnected component
    node = createGraphNode("A", "Node A", "Cluster C", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("B", "Node B", "Cluster C", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("C", "Node C", "Cluster C", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));
    node = createGraphNode("D", "Node D", "Cluster C", Optional.empty(), Optional.empty());
    node.ifPresent(n -> nodes.add(n));

    edge = createGraphEdge("22", "A", "B", "Edge A-B", Optional.of("#4c9ffc"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("23", "A", "C", "Edge A-C", Optional.of("#4c9ffc"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("24", "B", "D", "Edge B-D", Optional.of("#4c9ffc"));
    edge.ifPresent(e -> edges.add(e));
    edge = createGraphEdge("25", "A", "D", "Edge A-D", Optional.of("#4c9ffc"));
    edge.ifPresent(e -> edges.add(e));

    super.init();
  }

  public List<GraphEdge> getPersonGraphEdges() {
    return this.edges;
  }

  public List<GraphNode> getPersonGraphNodes() {
    return this.nodes;
  }

  private static Optional<GraphNode> createGraphNode(String id, String label, String group, Optional<Integer> size,
      Optional<String> nodeProgram) {
    return new GraphNodeBuilder()
        .id(id)
        .label(label)
        .group(Optional.of(group))
        .size(size)
        .nodeProgram(nodeProgram)
        .build();
  }

  private static Optional<GraphEdge> createGraphEdge(String id, String from, String to, String name, Optional<String> color) {
    return new GraphEdgeBuilder()
        .id(id)
        .source(from)
        .target(to)
        .color(color)
        .name(Optional.of(name))
        .build();
  }

  public Consumer<GraphNode> updateClickedNode() {
    return (node) -> {
      this.setLastClickedNodeName(node.getLabel());
      Log.info("GemGraphExample", "Clicked on node: " + node.getId() + " (" + node.getLabel() + ")");
    };
  }

  @Override
  public Graph getPersonGraphGraph() {
    return new GraphBuilder()
        .edges(this.getPersonGraphEdges())
        .nodes(this.getPersonGraphNodes())
        .build().get();
  }

  @Override
  public InitialGraphLayout getPersonGraphInitialLayout() {
    return new InitialGraphLayoutBuilder()
        .mode(InitialGraphLayoutMode.HIERARCHICAL)
        .build().get();
  }

}

GUI Model

component GemGraph(
  Graph graph,
  String minHeight="50vH",
  boolean showFilterGroup=true,
  boolean showSearchNode=true,
  boolean showControlButtons=true,
  boolean showInformation=true,
  List<String> metricKeys,
  InitialGraphLayout initialLayout = null,
  ComplexGraphLayout complexLayout = null,
  Event<Integer> layoutIteration,
  Event<List<NodePos>> nodePositionsAfterLayout,
  Object customBoundingBox = null,
  Event<GraphNode> graphClickNode
  // TODO configure Layout from GUIDsl

  //@Input()
Name Type Description Default value
graph Graph The graph data (nodes and edges) to be displayed.
minHeight String Minimum height of the component in px, %, vh, etc. "50vH"
showFilterGroup boolean Whether the filter group control is shown. true
showSearchNode boolean Whether the search-node control is shown. true
showControlButtons boolean Whether the layout control buttons are shown. true
showInformation boolean Whether the information panel is shown. true
metricKeys List<String> Keys of the node/edge metrics that can be selected for coloring/filtering.
initialLayout InitialGraphLayout Initial layout used to arrange the graph, e.g., circular or hierarchical, see InitialGraphLayout. null
complexLayout ComplexGraphLayout Complex layout that refines the positions of the initial layout, e.g., force-directed, see ComplexGraphLayout. null
layoutIteration Event<Integer> Callback that is executed on each iteration of the layout algorithm.
nodePositionsAfterLayout Event<List<NodePos>> Callback that is executed once with the final node positions after layouting.
customBoundingBox Object Custom bounding box constraining the graph's rendering area. null
graphClickNode Event<GraphNode> Callback that is executed when a node is clicked.

Types

NetworkGraphTypes

package mc.fenix.networkgraph;
import java.util.List;
import java.util.Optional;

<<pojoCD>>
classdiagram NetworkGraphTypes {

  public class Graph {
    List<GraphNode> nodes;
    List<GraphEdge> edges;
  }

  public class GraphNode {
    String id;
    Optional<Integer> value;
    String label;
    Optional<Integer> size;
    Optional<String> color;
    Optional<String> group;

    Optional<Boolean> fixed;
    Optional<Double> x;
    Optional<Double> y;

    Optional<String> nodeProgram;
    Optional<String> imageUrl;

    Optional<String> hoverLabel;
    Optional<String> hover;
    Optional<String> tooltip;
  }

  public class NodePos {
    String id;
    Optional<Double> x;
    Optional<Double> y;
  }

  public class GraphEdge {
    String id;
    String source;
    String target;
    Optional<String> name;
    Optional<Integer> size;
    Optional<Boolean> directed;
    Optional<String> color;
  }

  public class InitialGraphLayout {
    InitialGraphLayoutMode mode;
    Optional<CircularLayoutOptions> circular;
    Optional<RandomLayoutOptions> random;
    Optional<CirclePackLayoutOptions> circlePack;
    Optional<HierarchicalLayoutOptions> hierarchical;
    Optional<ChannelHierarchicalLayoutOptions> channelHierarchical;
  }

  public enum InitialGraphLayoutMode {
    CIRCULAR,
    RANDOM,
    CIRCLE_PACK,
    HIERARCHICAL,
    CHANNEL_HIERARCHICAL;
  }

  public class CircularLayoutOptions {
    Optional<Double> center;
    Optional<Double> scale;
  }

  public class RandomLayoutOptions {
    Optional<Double> center;
    Optional<Double> scale;
  }

  public class CirclePackLayoutOptions {
    Optional<Double> center;
    Optional<Double> scale;
    List<String> hierarchyAttributes;
  }

  public class HierarchicalLayoutOptions {
    Optional<HierarchicalLayoutDirection> direction;
    Optional<Boolean> useDummyNodes;
  }

  public class ChannelHierarchicalLayoutOptions {
    Optional<HierarchicalLayoutDirection> direction;
    Optional<Boolean> useDummyNodes;
    Optional<Double> levelSeparation;
    Optional<Double> channelWidth;
  }

  public enum HierarchicalLayoutDirection {
    VERTICAL,
    HORIZONTAL;
  }

  public class ComplexGraphLayout {
    ComplexGraphLayoutMode mode;
    Optional<Integer> maxIterations;
    Optional<Integer> maxLayoutTime;
    Optional<ForceLayoutOptions> force;
    Optional<ForceAtlas2LayoutOptions> forceAtlas2;
    Optional<NoOverlapLayoutOptions> noOverlap;
  }

  public enum ComplexGraphLayoutMode {
    FORCE,
    FORCEATLAS2,
    NO_OVERLAP;
  }

  public class ForceLayoutOptions {
    Optional<Double> attraction;
    Optional<Double> repulsion;
    Optional<Double> gravity;
    Optional<Double> inertia;
    Optional<Double> maxMove;
  }

  public class ForceAtlas2LayoutOptions {
    Optional<Boolean> linLogMode;
    Optional<Boolean> outboundAttractionDistribution;
    Optional<Boolean> adjustSizes;
    Optional<Double> edgeWeightInfluence;
    Optional<Double> scalingRatio;
    Optional<Boolean> strongGravityMode;
    Optional<Double> gravity;
    Optional<Double> slowDown;
    Optional<Boolean> barnesHutOptimize;
    Optional<Double> barnesHutTheta;
  }

  public class NoOverlapLayoutOptions {
    Optional<Integer> gridSize;
    Optional<Double> margin;
    Optional<Double> expansion;
    Optional<Double> ratio;
    Optional<Double> speed;
  }

}

Graph

Name Type Description Default value
nodes List<GraphNode>
edges List<GraphEdge>

GraphNode

Name Type Description Default value
id String
value Optional<Integer>
label String
size Optional<Integer>
color Optional<String>
group Optional<String>
fixed Optional<Boolean> * Whether the position of the node is fixed or may change during layout.
x Optional<Double>
y Optional<Double>
nodeProgram Optional<String> * In which shapes should nodes be drawn. Allowed values are triangle, square, circle and image. Default is circle.
imageUrl Optional<String> * The image url in case nodeProgram is set to image.
hoverLabel Optional<String>
hover Optional<String>
tooltip Optional<String>

NodePos

Name Type Description Default value
id String
x Optional<Double>
y Optional<Double>

GraphEdge

Name Type Description Default value
id String
source String
target String
name Optional<String>
size Optional<Integer>
directed Optional<Boolean>
color Optional<String>

InitialGraphLayout

Name Type Description Default value
mode InitialGraphLayoutMode
circular Optional<CircularLayoutOptions>
random Optional<RandomLayoutOptions>
circlePack Optional<CirclePackLayoutOptions>
hierarchical Optional<HierarchicalLayoutOptions>
channelHierarchical Optional<ChannelHierarchicalLayoutOptions>

CircularLayoutOptions

Name Type Description Default value
center Optional<Double> Center of the circle on both axes (default 0.5).
scale Optional<Double> Scale of the layout (default 1).

RandomLayoutOptions

Name Type Description Default value
center Optional<Double> Center of the area the nodes are placed in on both axes (default 0.5).
scale Optional<Double> Scale of the layout (default 1).

CirclePackLayoutOptions

Name Type Description Default value
center Optional<Double> Center of the layout on both axes (default 0).
scale Optional<Double> Scale of the layout (default 1).
hierarchyAttributes List<String> Node attributes (e.g. group) whose values nest the nodes into circles, outermost first.

HierarchicalLayoutOptions

Name Type Description Default value
direction Optional<HierarchicalLayoutDirection> Direction of the layers (default VERTICAL, top to bottom).
useDummyNodes Optional<Boolean> Whether edges spanning several layers bend through the layers in between (default false).

ChannelHierarchicalLayoutOptions

Name Type Description Default value
direction Optional<HierarchicalLayoutDirection> Direction of the layers (default VERTICAL, top to bottom).
useDummyNodes Optional<Boolean> Whether edges spanning several layers bend through the layers in between (default false).
levelSeparation Optional<Double> Distance between two layers (default 250).
channelWidth Optional<Double> Distance between two channels (default 400).

ComplexGraphLayout

Name Type Description Default value
mode ComplexGraphLayoutMode
maxIterations Optional<Integer> Maximum number of iterations, after which the layout stops (default unlimited).
maxLayoutTime Optional<Integer> Maximum duration of the layout in milliseconds, after which it stops (default unlimited).
force Optional<ForceLayoutOptions>
forceAtlas2 Optional<ForceAtlas2LayoutOptions>
noOverlap Optional<NoOverlapLayoutOptions>

ForceLayoutOptions

Name Type Description Default value
attraction Optional<Double> Strength of the attraction along the edges (default 0.0005).
repulsion Optional<Double> Strength of the repulsion between the nodes (default 0.1).
gravity Optional<Double> Strength of the attraction to the center (default 0.0001).
inertia Optional<Double> Share of the previous movement a node keeps in the next iteration (default 0.6).
maxMove Optional<Double> Maximum distance a node moves in one iteration (default 200).

ForceAtlas2LayoutOptions

Name Type Description Default value
linLogMode Optional<Boolean> Logarithmic attraction, which gives tighter clusters (default false).
outboundAttractionDistribution Optional<Boolean> Distributes the attraction along outbound edges, which pushes hubs to the border (default false).
adjustSizes Optional<Boolean> Takes the size of the nodes into account to prevent overlaps (default false).
edgeWeightInfluence Optional<Double> Influence of the edge weights, 0 ignores them (default 1).
scalingRatio Optional<Double> Scale of the repulsion, larger values spread the graph (default 10).
strongGravityMode Optional<Boolean> Gravity that does not decrease with the distance to the center (default true).
gravity Optional<Double> Strength of the attraction to the center (default 0.05).
slowDown Optional<Double> Reduces the speed of the nodes, larger values are more stable (default 1 + ln(number of nodes)).
barnesHutOptimize Optional<Boolean> Approximates the repulsion (Barnes-Hut), faster for large graphs (default true above 2000 nodes).
barnesHutTheta Optional<Double> Accuracy of the Barnes-Hut approximation, smaller values are more accurate (default 0.5).

NoOverlapLayoutOptions

Name Type Description Default value
gridSize Optional<Integer> Number of grid cells per axis used to find overlapping nodes (default 20).
margin Optional<Double> Minimal distance between two nodes (default 5).
expansion Optional<Double> Factor by which the area is expanded in each iteration (default 1.1).
ratio Optional<Double> Ratio by which the node sizes are scaled (default 1).
speed Optional<Double> Speed of the movement of the nodes (default 3).