docs: add dagre layout example

This commit is contained in:
braks
2024-02-05 07:51:12 +01:00
committed by Braks
parent 0bfb5f4046
commit 3220e20c0e
13 changed files with 192 additions and 40 deletions
+70
View File
@@ -0,0 +1,70 @@
<script setup>
import dagre from 'dagre'
import { nextTick, ref } from 'vue'
import { Panel, Position, VueFlow, useVueFlow } from '@vue-flow/core'
import { initialEdges, initialNodes } from './initial-elements.js'
const nodes = ref(initialNodes)
const edges = ref(initialEdges)
const { findNode, fitView } = useVueFlow()
function handleLayout(direction) {
// we create a new graph instance, in case some nodes/edges were removed, otherwise dagre would act as if they were still there
const dagreGraph = new dagre.graphlib.Graph()
dagreGraph.setDefaultEdgeLabel(() => ({}))
const isHorizontal = direction === 'LR'
dagreGraph.setGraph({ rankdir: direction })
for (const node of nodes.value) {
// if you need width+height of nodes for your layout, you can use the dimensions property of the internal node (`GraphNode` type)
const graphNode = findNode(node.id)
dagreGraph.setNode(node.id, { width: graphNode.dimensions.width || 150, height: graphNode.dimensions.height || 50 })
}
for (const edge of edges.value) {
dagreGraph.setEdge(edge.source, edge.target)
}
dagre.layout(dagreGraph)
// set nodes with updated positions
nodes.value = nodes.value.map((node) => {
const nodeWithPosition = dagreGraph.node(node.id)
return {
...node,
targetPosition: isHorizontal ? Position.Left : Position.Top,
sourcePosition: isHorizontal ? Position.Right : Position.Bottom,
position: { x: nodeWithPosition.x, y: nodeWithPosition.y },
}
})
nextTick(() => {
fitView()
})
}
</script>
<template>
<div class="layoutflow">
<VueFlow :nodes="nodes" :edges="edges" @nodes-initialized="handleLayout('TB')">
<Panel style="display: flex; gap: 1rem" position="top-right">
<button @click="handleLayout('TB')">vertical layout</button>
<button @click="handleLayout('LR')">horizontal layout</button>
</Panel>
</VueFlow>
</div>
</template>
<style>
.layoutflow {
height: 100%;
width: 100%;
}
</style>
+2
View File
@@ -0,0 +1,2 @@
export { default as LayoutApp } from './App.vue?raw'
export { default as LayoutElements } from './initial-elements.js?raw'
+70
View File
@@ -0,0 +1,70 @@
const position = { x: 0, y: 0 }
export const initialNodes = [
{
id: '1',
type: 'input',
label: 'input',
position,
},
{
id: '2',
label: 'node 2',
position,
},
{
id: '2a',
label: 'node 2a',
position,
},
{
id: '2b',
label: 'node 2b',
position,
},
{
id: '2c',
label: 'node 2c',
position,
},
{
id: '2d',
label: 'node 2d',
position,
},
{
id: '3',
label: 'node 3',
position,
},
{
id: '4',
label: 'node 4',
position,
},
{
id: '5',
label: 'node 5',
position,
},
{
id: '6',
type: 'output',
label: 'output',
position,
},
]
export const initialEdges = [
{ id: '7', type: 'output', label: 'output', position: { x: 400, y: 450 } },
{ id: 'e12', source: '1', target: '2', type: 'smoothstep', animated: true },
{ id: 'e13', source: '1', target: '3', type: 'smoothstep', animated: true },
{ id: 'e22a', source: '2', target: '2a', type: 'smoothstep', animated: true },
{ id: 'e22b', source: '2', target: '2b', type: 'smoothstep', animated: true },
{ id: 'e22c', source: '2', target: '2c', type: 'smoothstep', animated: true },
{ id: 'e2c2d', source: '2c', target: '2d', type: 'smoothstep', animated: true },
{ id: 'e45', source: '4', target: '5', type: 'smoothstep', animated: true },
{ id: 'e56', source: '5', target: '6', type: 'smoothstep', animated: true },
{ id: 'e57', source: '5', target: '7', type: 'smoothstep', animated: true },
]