File

projects/atft/src/lib/actor/data-center/layout/dagre-utils.ts

Description

WIKI: https://github.com/dagrejs/dagre/wiki

Index

Methods

Methods

Static getLayout
getLayout(model: GraphModel)
Parameters :
Name Type Optional
model GraphModel No
Returns : any
Static modelToGraph
modelToGraph(model: GraphModel)
Parameters :
Name Type Optional
model GraphModel No
Returns : dagre.graphlib.Graph
Static updateBaseInfo
updateBaseInfo(g: dagre.graphlib.Graph, baseInfo: Array<BaseInfo> | undefined)
Parameters :
Name Type Optional
g dagre.graphlib.Graph No
baseInfo Array<BaseInfo> | undefined No
Returns : void
Static updateEdges
updateEdges(g: dagre.graphlib.Graph, model: GraphModel)
Parameters :
Name Type Optional
g dagre.graphlib.Graph No
model GraphModel No
Returns : void
Static updateGraph
updateGraph(g: dagre.graphlib.Graph, model: GraphModel)
Parameters :
Name Type Optional
g dagre.graphlib.Graph No
model GraphModel No
Returns : void
import * as dagre from 'dagre';
import {BaseInfo, Edge, GraphModel, Node} from './dagre-model';


/**
 * WIKI: https://github.com/dagrejs/dagre/wiki
 */
export class DagreUtils {

  public static modelToGraph(model: GraphModel): dagre.graphlib.Graph {
    const g = new dagre.graphlib.Graph({
      compound: true,
      multigraph: true
    });

    g.setGraph(this.getLayout(model))

    g.setDefaultEdgeLabel(function () {
      return {};
    });

    this.updateGraph(g, model);

    // console.log('DagreUtils.layout model', model);
    // console.log('DagreUtils.layout g', g);
    dagre.layout(g);
    // console.log('DagreUtils.layout result (g.nodes())', g.nodes());
    return g;
  }

  public static updateBaseInfo(g: dagre.graphlib.Graph, baseInfo: Array<BaseInfo> | undefined ) {
    if (baseInfo) {
      baseInfo.forEach((node: Node) => {
        g.setNode(node.name, {label: node.label, width: 18, height: 18});
        if (node.composition) {
          g.setParent(node.name, node.composition);
        }
      });
    }
  }

  public static updateEdges(g: dagre.graphlib.Graph, model: GraphModel) {
    if (model.edges) {
      model.edges.forEach((edge: Edge) => {
        g.setEdge(edge.from, edge.to, {name: edge.name});
      });
    }
  }

  public static updateGraph(g: dagre.graphlib.Graph, model: GraphModel) {
      this.updateBaseInfo(g, model.compositions);
      this.updateBaseInfo(g, model.nodes);
      this.updateEdges(g, model);
  }

  public static getLayout(model: GraphModel): any {
    const layout: dagre.GraphLabel | undefined = model.layout;
    return layout;
  }

}

results matching ""

    No results matching ""