Layout / Positioning
Scale the entire objects
By default, zooming only changes the relative position of the nodes, and does not change the size of the object, such as the size of nodes, the font size of labels, or the line width of links.
If you set the view.scalingObjects config to true, the objects will change as you zoom.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import { reactive, ref } from "vue"
import data from "./data"
const configs = reactive(
vNG.defineConfigs({
view: {
scalingObjects: true,
minZoomLevel: 0.1,
maxZoomLevel: 16,
},
})
)
const zoomLevel = ref(1.5)
</script>
<template>
<div class="demo-control-panel">
<el-checkbox v-model="configs.view.scalingObjects">Scaling objects</el-checkbox>
<el-slider-custom-zoom v-model="zoomLevel" />
</div>
<v-network-graph
v-model:zoom-level="zoomLevel"
:nodes="data.nodes"
:edges="data.edges"
:layouts="data.layouts"
:configs="configs"
/>
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Nodes, Edges, Layouts } from "v-network-graph"
const nodes: Nodes = {
node1: { name: "Node 1" },
node2: { name: "Node 2" },
node3: { name: "Node 3" },
node4: { name: "Node 4" },
}
const edges: Edges = {
edge1: { source: "node1", target: "node2" },
edge2: { source: "node2", target: "node3" },
edge3: { source: "node3", target: "node4" },
}
const layouts: Layouts = {
nodes: {
node1: { x: 0, y: 0 },
node2: { x: 50, y: 50 },
node3: { x: 100, y: 0 },
node4: { x: 150, y: 50 },
},
}
export default {
nodes,
edges,
layouts,
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Snap nodes to the grid
You can change the node positioning strategy in the config.
An example of modifying the view.layoutHandler config and snapping to the grid is shown below.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import data from "./data"
const configs = vNG.defineConfigs({
view: {
layoutHandler: new vNG.GridLayout({ grid: 15 }),
},
})
</script>
<template>
<v-network-graph
:nodes="data.nodes"
:edges="data.edges"
:layouts="data.layouts"
:configs="configs"
/>
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { Nodes, Edges, Layouts } from "v-network-graph"
const nodes: Nodes = {
node1: { name: "Node 1" },
node2: { name: "Node 2" },
node3: { name: "Node 3" },
node4: { name: "Node 4" },
}
const edges: Edges = {
edge1: { source: "node1", target: "node2" },
edge2: { source: "node2", target: "node3" },
edge3: { source: "node3", target: "node4" },
}
const layouts: Layouts = {
nodes: {
node1: { x: 0, y: 0 },
node2: { x: 50, y: 50 },
node3: { x: 100, y: 0 },
node4: { x: 150, y: 50 },
},
}
export default {
nodes,
edges,
layouts,
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Position nodes with d3-force
This component also provide a layout strategy with simulating physical forces on particles with d3-force.
d3-force is an optional dependency and should be added to your project manually.
npm install d3-forceIn this example, clicking on a node with Alt key pressed will fix the position of the node. Clicking the node again with the Alt key will return it to the position of the d3-force calculation result.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import {
ForceLayout,
ForceNodeDatum,
ForceEdgeDatum,
} from "v-network-graph/lib/force-layout"
import { reactive, ref, watch } from "vue"
const nodeCount = ref(20)
const nodes = reactive({})
const edges = reactive({})
// initialize network
buildNetwork(nodeCount.value, nodes, edges)
watch(nodeCount, () => {
buildNetwork(nodeCount.value, nodes, edges)
})
const configs = reactive(
vNG.defineConfigs({
view: {
layoutHandler: new ForceLayout({
positionFixedByDrag: false,
positionFixedByClickWithAltKey: true,
createSimulation: (d3, nodes, edges) => {
// d3-force parameters
const forceLink = d3
.forceLink<ForceNodeDatum, ForceEdgeDatum>(edges)
.id(d => d.id)
return d3
.forceSimulation(nodes)
.force("edge", forceLink.distance(40).strength(0.5))
.force("charge", d3.forceManyBody().strength(-800))
.force("center", d3.forceCenter().strength(0.05))
.alphaMin(0.001)
// * The following are the default parameters for the simulation.
// const forceLink = d3.forceLink<ForceNodeDatum, ForceEdgeDatum>(edges).id(d => d.id)
// return d3
// .forceSimulation(nodes)
// .force("edge", forceLink.distance(100))
// .force("charge", d3.forceManyBody())
// .force("collide", d3.forceCollide(50).strength(0.2))
// .force("center", d3.forceCenter().strength(0.05))
// .alphaMin(0.001)
},
}),
},
node: {
label: {
visible: false,
},
},
})
)
function buildNetwork(count: number, nodes: vNG.Nodes, edges: vNG.Edges) {
const idNums = [...Array(count)].map((_, i) => i)
// nodes
const newNodes = Object.fromEntries(idNums.map(id => [`node${id}`, {}]))
Object.keys(nodes).forEach(id => delete nodes[id])
Object.assign(nodes, newNodes)
// edges
const makeEdgeEntry = (id1: number, id2: number) => {
return [`edge${id1}-${id2}`, { source: `node${id1}`, target: `node${id2}` }]
}
const newEdges = Object.fromEntries([
...idNums
.map(n => [n, (Math.floor(n / 4) * 4) % count])
.map(([n, m]) => (n === m ? [n, (n + 4) % count] : [n, m]))
.map(([n, m]) => makeEdgeEntry(n, m)),
])
Object.keys(edges).forEach(id => delete edges[id])
Object.assign(edges, newEdges)
}
</script>
<template>
<div class="demo-control-panel">
<label>Node count:</label>
<el-input-number v-model="nodeCount" :min="3" :max="200" />
<label>(<= 200)</label>
</div>
<v-network-graph :zoom-level="0.5" :nodes="nodes" :edges="edges" :configs="configs" />
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Fix the position of a specific node in the force layout
If you set fixed: true to the node position object in layouts, the node will be fixed without the effect of force layout.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import {
ForceLayout,
ForceNodeDatum,
ForceEdgeDatum,
} from "v-network-graph/lib/force-layout"
import { reactive, ref } from "vue"
const NODE_COUNT = 20
const nodes = reactive({})
const edges = reactive({})
// The fixed position of the node can be specified.
const layouts = ref({
nodes: {
node0: {
x: 0,
y: 0,
fixed: true,
},
},
})
const configs = reactive(
vNG.defineConfigs({
view: {
layoutHandler: new ForceLayout({
positionFixedByDrag: false,
positionFixedByClickWithAltKey: true,
createSimulation: (d3, nodes, edges) => {
// d3-force parameters
const forceLink = d3
.forceLink<ForceNodeDatum, ForceEdgeDatum>(edges)
.id(d => d.id)
return d3
.forceSimulation(nodes)
.force("edge", forceLink.distance(60).strength(0.2))
.force("charge", d3.forceManyBody().strength(-120))
.alphaMin(0.001)
},
}),
},
node: {
normal: {
color: n => (n.id === "node0" ? "#ff0000" : "#4466cc"),
},
label: {
visible: false,
},
},
})
)
buildNetwork(NODE_COUNT, nodes, edges)
function buildNetwork(count: number, nodes: vNG.Nodes, edges: vNG.Edges) {
const idNums = [...Array(count)].map((_, i) => i)
// nodes
const newNodes = idNums.map(id => [`node${id}`, { id: `node${id}` }])
Object.assign(nodes, Object.fromEntries(newNodes))
// edges
const makeEdgeEntry = (id1: number, id2: number) => {
return [`edge${id1}-${id2}`, { source: `node${id1}`, target: `node${id2}` }]
}
const newEdges = []
for (let i = 1; i < count; i++) {
newEdges.push(makeEdgeEntry(Math.floor(i / 4), i))
}
Object.assign(edges, Object.fromEntries(newEdges))
}
</script>
<template>
<v-network-graph
:zoom-level="0.5"
:nodes="nodes"
:edges="edges"
:layouts="layouts"
:configs="configs"
/>
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Enable and disable force layout
Nodes keep moving while the force layout runs, which can get in the way when you want to look at a particular node. You can enable the force layout first and disable it later.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import { ForceLayout } from "v-network-graph/lib/force-layout"
import { reactive, ref, watch, computed } from "vue"
const nodeCount = ref(15)
const nodes = reactive({})
const edges = reactive({})
// initialize network
buildNetwork(nodeCount.value, nodes, edges)
watch(nodeCount, () => {
buildNetwork(nodeCount.value, nodes, edges)
})
const d3ForceEnabled = computed({
get: () => configs.view.layoutHandler instanceof ForceLayout,
set: (value: boolean) => {
if (value) {
configs.view.layoutHandler = new ForceLayout()
} else {
configs.view.layoutHandler = new vNG.SimpleLayout()
}
},
})
const layoutHandler: vNG.LayoutHandler = new ForceLayout()
const configs = reactive(
vNG.defineConfigs({
view: {
layoutHandler,
},
node: {
label: {
visible: false,
},
},
})
)
function buildNetwork(count: number, nodes: vNG.Nodes, edges: vNG.Edges) {
const idNums = [...Array(count)].map((_, i) => i)
// nodes
const newNodes = Object.fromEntries(idNums.map(id => [`node${id}`, {}]))
Object.keys(nodes).forEach(id => delete nodes[id])
Object.assign(nodes, newNodes)
// edges
const makeEdgeEntry = (id1: number, id2: number) => {
return [`edge${id1}-${id2}`, { source: `node${id1}`, target: `node${id2}` }]
}
const newEdges = Object.fromEntries([
...idNums
.map(n => [n, (Math.floor(n / 5) * 5) % count])
.map(([n, m]) => (n === m ? [n, (n + 5) % count] : [n, m]))
.map(([n, m]) => makeEdgeEntry(n, m)),
])
Object.keys(edges).forEach(id => delete edges[id])
Object.assign(edges, newEdges)
}
</script>
<template>
<div class="demo-control-panel">
<label>Node count:</label>
<el-input-number v-model="nodeCount" :min="3" :max="200" />
<label>(<= 200)</label>
<el-checkbox v-model="d3ForceEnabled" label="D3-Force enabled" />
</div>
<v-network-graph
:zoom-level="0.5"
:nodes="nodes"
:edges="edges"
:configs="configs"
/>
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
Customize d3-force parameters: advance the specified ticks first
This example shows how to customize the d3-force parameter and how to perform the coordinate calculation by d3-force for the specified ticks.
In the previous d3-force examples, the nodes move after being displayed, but in this example, the nodes stop and are displayed after the coordinates are calculated.
Also, in this example, d3-force is set to not recalculate even if nodes are dragged or network changes occur. This behavior can be customized. Please check the example source code for details.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import {
ForceLayout,
ForceNodeDatum,
ForceEdgeDatum,
} from "v-network-graph/lib/force-layout"
import data from "./data"
const configs = vNG.defineConfigs({
view: {
autoPanAndZoomOnLoad: "fit-content",
layoutHandler: new ForceLayout({
positionFixedByDrag: false,
positionFixedByClickWithAltKey: false,
noAutoRestartSimulation: true, // If the line is deleted or set to false,
// d3-force recalculation will be performed when nodes are dragged or
// the network changes.
createSimulation: (d3, nodes, edges) => {
const forceLink = d3
.forceLink<ForceNodeDatum, ForceEdgeDatum>(edges)
.id((d: ForceNodeDatum) => d.id)
// Specify your own d3-force parameters
return d3
.forceSimulation(nodes)
.force("edge", forceLink.distance(10).strength(1))
.force("charge", d3.forceManyBody().strength(-2000))
.force("x", d3.forceX())
.force("y", d3.forceY())
.stop() // tick manually
.tick(100)
},
}),
},
node: {
label: {
visible: false,
},
},
})
</script>
<template>
<v-network-graph :nodes="data.nodes" :edges="data.edges" :configs="configs" />
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Nodes, Edges } from "v-network-graph"
const nodes: Nodes = {
node0: {},
node1: {},
node2: {},
node3: {},
node4: {},
node5: {},
node6: {},
node7: {},
node8: {},
node9: {},
node10: {},
node11: {},
node12: {},
node13: {},
node14: {},
node15: {},
}
const edges: Edges = {
"edge0-4": { source: "node0", target: "node4" },
"edge1-0": { source: "node1", target: "node0" },
"edge2-0": { source: "node2", target: "node0" },
"edge3-0": { source: "node3", target: "node0" },
"edge4-8": { source: "node4", target: "node8" },
"edge5-4": { source: "node5", target: "node4" },
"edge6-4": { source: "node6", target: "node4" },
"edge7-4": { source: "node7", target: "node4" },
"edge8-12": { source: "node8", target: "node12" },
"edge9-8": { source: "node9", target: "node8" },
"edge10-8": { source: "node10", target: "node8" },
"edge11-8": { source: "node11", target: "node8" },
"edge12-0": { source: "node12", target: "node0" },
"edge13-12": { source: "node13", target: "node12" },
"edge14-12": { source: "node14", target: "node12" },
"edge15-12": { source: "node15", target: "node12" },
}
export default { nodes, edges }2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Automatic layout
Since node position is an input to <v-network-graph>, layout such as node arrangement/alignment is out of scope of this library.
Node layout can be achieved by writing the x,y coordinates of each node given in the layouts property of <v-network-graph> according to the layout desired to be realized in the application.
The following is an example of using an external library (dagre: Directed graph layout for JavaScript) to automatically layout the tree structure of a directed graph.
<script setup lang="ts">
import * as vNG from "v-network-graph"
import { ref } from "vue"
import data from "./data"
// dagre: Directed graph layout for JavaScript
// https://github.com/dagrejs/dagre
// @ts-expect-error: dagre ships no type declarations for its bundled build
import dagre from "dagre/dist/dagre.min.js"
const nodeSize = 40
const configs = vNG.defineConfigs({
view: {
autoPanAndZoomOnLoad: "fit-content",
onBeforeInitialDisplay: () => layout("TB"),
},
node: {
normal: { radius: nodeSize / 2 },
label: { direction: "center", color: "#fff" },
},
edge: {
normal: {
color: "#aaa",
width: 3,
},
margin: 4,
marker: {
target: {
type: "arrow",
width: 4,
height: 4,
},
},
},
})
const graph = ref<vNG.VNetworkGraphInstance>()
function layout(direction: "TB" | "LR") {
if (Object.keys(data.nodes).length <= 1 || Object.keys(data.edges).length == 0) {
return
}
// convert graph
// ref: https://github.com/dagrejs/dagre/wiki
const g = new dagre.graphlib.Graph()
// Set an object for the graph label
g.setGraph({
rankdir: direction,
nodesep: nodeSize * 2,
edgesep: nodeSize,
ranksep: nodeSize * 2,
})
// Default to assigning a new object as a label for each new edge.
g.setDefaultEdgeLabel(() => ({}))
// Add nodes to the graph. The first argument is the node id. The second is
// metadata about the node. In this case we're going to add labels to each of
// our nodes.
Object.entries(data.nodes).forEach(([nodeId, node]) => {
g.setNode(nodeId, { label: node.name, width: nodeSize, height: nodeSize })
})
// Add edges to the graph.
Object.values(data.edges).forEach(edge => {
g.setEdge(edge.source, edge.target)
})
dagre.layout(g)
g.nodes().forEach((nodeId: string) => {
// update node position
const x = g.node(nodeId).x
const y = g.node(nodeId).y
data.layouts.nodes[nodeId] = { x, y }
})
}
function updateLayout(direction: "TB" | "LR") {
// Animates the movement of an element.
graph.value?.transitionWhile(() => {
layout(direction)
})
}
</script>
<template>
<div class="demo-control-panel">
<el-button @click="updateLayout('LR')">Layout: Left to Right</el-button>
<el-button @click="updateLayout('TB')">Layout: Top to Bottom</el-button>
</div>
<v-network-graph
ref="graph"
class="graph"
:nodes="data.nodes"
:edges="data.edges"
:layouts="data.layouts"
:configs="configs"
/>
</template>2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { Nodes, Edges, Layouts } from "v-network-graph"
import { reactive } from "vue"
const nodes: Nodes = {
node1: { name: "N1" },
node2: { name: "N2" },
node3: { name: "N3" },
node4: { name: "N4" },
node5: { name: "N5" },
node6: { name: "N6" },
node7: { name: "N7" },
}
const edges: Edges = {
edge1: { source: "node1", target: "node2" },
edge2: { source: "node2", target: "node3" },
edge3: { source: "node3", target: "node4" },
edge4: { source: "node3", target: "node5" },
edge5: { source: "node2", target: "node6" },
edge6: { source: "node6", target: "node7" },
}
const layouts: Layouts = reactive({
nodes: {},
})
export default {
nodes,
edges,
layouts,
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
INFO
dagre: Directed graph layout for JavaScript
https://github.com/dagrejs/dagre/wiki
*It can be installed with the following command:
npm install dagre