Search code examples
angulardirected-acyclic-graphsngx-charts

How to create DAG (Graph) with different shapes using NGX-Graph


I want to create graph with custom node shapes and custom colors. I also want to have the edges with labels on them. How can I do this using NGX-Graph (https://swimlane.github.io/ngx-graph/)

Eghere is the sample of the graph what I want to build.


Solution

  • You can define your own template for the node and customize it however you want:

    <ngx-graph [legend]="false" [links]="graph.links" [scheme]="colorScheme" [nodes]="graph.nodes" [nodeWidth]="70" [nodeHeight]="70" [orientation]="'LR'" [curve]="curve">
      <ng-template #nodeTemplate let-node>
        <svg:g class="node">
          <svg:rect [attr.width]="node.width" [attr.height]="node.height" stroke="#3d465a" fill="#262C38" stroke-width="2" rx="15" ry="15" />
          <svg:rect [attr.width]="node.width - 10" [attr.height]="node.height - 10" [attr.fill]="node.options.color" rx="10" ry="10" x="5" y="5" />
          <svg:text class="node-type" alignment-baseline="central" [attr.x]="16" [attr.y]="15">
            {{node.type}}
          </svg:text>
          <svg:image alignment-baseline="central" [attr.x]="(node.width / 2) - 16" [attr.y]="25" [attr.width]="32" [attr.height]="32" [attr.xlink:href]="getImageForNode(node)" />
          <svg:text class="node-label" alignment-baseline="central" [attr.x]="(node.width / 2) - ((node.label.length * 5) / 2)" [attr.y]="85">
            {{node.label}}
          </svg:text>
        </svg:g>
      </ng-template>
    </ngx-graph>
    

    You have access to the node data in that template, so you can draw different shapes based on the node's properties.