<template>
<div ref="graphContainer" class="graph-container"></div>
</template>
<script>
import { defineComponent, onMounted, ref } from 'vue';
import { Graph } from '@antv/x6';
const nodeData = { id: 'root', label: 'Root', children: [ { id: 'child1', label: 'Child 1', children: [ { id: 'grandchild1', label: 'Grandchild 1' },
{ id: 'grandchild2', label: 'Grandchild 2' }, ], },
{ id: 'child2', label: 'Child 2', children: [{ id: 'grandchild3', label: 'Grandchild 3' }],
},
],
};
function calculateLayout(node, xOffset = 50, yOffset = 50, level = 0) { const { id, label, children } = node;
const x = xOffset + level * 150;
const y = yOffset + level * 50;
return { id, x, y, width: 100, height: 40, label: { text: label }, children: children ? children.map(child => calculateLayout(child, xOffset, y + 50, level + 1)) : [], }; }
function flattenLayout(nodes) {
return nodes.map(node => ({ ...node, children: undefined,
export default defineComponent({ name: 'X6HierarchicalLayout', setup() {
const graphContainer = ref(null);
let graph = null;
onMounted(() => {
graph = new Graph({ container: graphContainer.value, width: 800, height: 600, grid: { size: 10, visible: true }, });
const layout = calculateLayout(nodeData);
const flatLayout = flattenLayout(layout);
flatLayout.forEach(nodeData => { graph.addNode({ ...nodeData, }); });
</script>
<style scoped>
.graph-container { border: 1px solid #ccc; }
</style>