Search code examples
d3.jsangular8ngx-graph

Need help in ngx-graph links layout


I am trying to use ngx-graph in my project to draw a hierarchical graph. I have issue with links that connecting two nodes. I have tried all curve types, but not getting expected result.

Here is some code snippets:

HTML

[links]="links"
[nodes]="nodes"
[layoutSettings]="layoutSettings"
[curve]="curve"
[draggingEnabled]="false"
[panningEnabled]="true"
[enableZoom]="false"
[autoZoom]="true"
[autoCenter]="false"

<ng-template #nodeTemplate let-node>
<svg:g class="node">
<svg:rect [attr.width]="100" [attr.height]="50" fill ="#fff" stroke-width ="1" stroke = "#000" />
<svg:text alignment-baseline="central" [attr.x]="10" [attr.y]="node.dimension.height / 2">{{node.label}}</svg:text>
</svg:g>
</ng-template>
</ngx-graph>

TS


layoutSettings = {
orientation: 'TB'
};

nodes: Node[] = [
{
id: 'first',
label: 'Parent'
}, {
id: 'c1',
label: 'Child 1'
}, {
id: 'c2',
label: 'Child 2'
}, {
id: 'c3',
label: 'Child 3'
}
];

links: Edge[] = [
{
id: 'a',
source: 'first',
target: 'c1',
label: 'is parent of'
}, {
id: 'b',
source: 'first',
target: 'c2',
label: 'custom label'
}, {
id: 'c',
source: 'first',
target: 'c3',
label: 'custom label'
}
];

Output that I am getting is:

enter image description here

Expected output is:

enter image description here

Any help or suggestions would be appreciated Thank you.


Solution

  • I found answer for my own question. This can be achieved by custom layouts. Here you can found a custom layout, which resolves my issue.