Replies: 5 comments 3 replies
-
BTW WTF, do you know anything about |
Beta Was this translation helpful? Give feedback.
-
My working attempt, import dagre from '@dagrejs/dagre';
import { Position, useVueFlow } from '@vue-flow/core';
import { ref } from 'vue';
import { Node, Edge } from '@vue-flow/core';
export type Direction = 'TB' | 'BT' | 'LR' | 'RL';
/**
* Composable to run the layout algorithm on the graph.
* It uses the `dagre` library to calculate the layout of the nodes and edges.
*
* Forked from https://vueflow.dev/examples/layout.html
*/
export function useLayout() {
const { findNode } = useVueFlow();
const graph = ref(new dagre.graphlib.Graph());
const previousDirection = ref('LR');
function layout(nodes: Node[], edges: Edge[], direction: 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();
graph.value = dagreGraph;
dagreGraph.setDefaultEdgeLabel(() => ({}));
const isHorizontal = direction === 'LR';
dagreGraph.setGraph({ rankdir: direction });
previousDirection.value = direction;
for (const node of nodes) {
/**
* 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);
if (!graphNode) {
console.error('graphNode not found');
continue;
}
dagreGraph.setNode(node.id, {
width: graphNode.dimensions.width || 150,
height: graphNode.dimensions.height || 50
});
}
for (const edge of edges) {
dagreGraph.setEdge(edge.source, edge.target);
}
dagre.layout(dagreGraph);
// set nodes with updated positions
return nodes.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 }
};
}) as Node[];
}
return { graph, layout, previousDirection };
} |
Beta Was this translation helpful? Give feedback.
-
I kept all the examples in JS for a reason. |
Beta Was this translation helpful? Give feedback.
-
Ok, but I think it's much easier to strip types from |
Beta Was this translation helpful? Give feedback.
-
It's even can be fully automated. TypeScript examples with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
At the layout demo dagre 0.8.5 used (see
useLayout.js
).Seems modern
@dagrejs/dagre
1.0.1+ has type definitionsSo
useLayout.js
can be renamed touseLayout.ts
and typings can be addedBeta Was this translation helpful? Give feedback.
All reactions