圆圈不重叠了

This commit is contained in:
chatgpt-yunju 2025-07-07 19:26:21 +08:00
parent b00b1293e4
commit 1688d8b200

View File

@ -8,6 +8,38 @@
import { ref, onMounted, onBeforeUnmount } from "vue";
import G6 from "@antv/g6";
//
G6.registerEdge('combo-border-edge', {
draw(cfg, group) {
const { sourceCombo, targetCombo } = cfg;
if (!sourceCombo || !targetCombo) return;
//
const { x: x1, y: y1, r: r1 } = sourceCombo;
const { x: x2, y: y2, r: r2 } = targetCombo;
// 线
const dx = x2 - x1, dy = y2 - y1;
const len = Math.sqrt(dx * dx + dy * dy);
const ux = dx / len, uy = dy / len;
//
const sx = x1 + ux * r1;
const sy = y1 + uy * r1;
const tx = x2 - ux * r2;
const ty = y2 - uy * r2;
// 线线
return group.addShape('path', {
attrs: {
stroke: '#fff',
lineWidth: 2,
path: [
['M', sx, sy],
// ['Q', (sx+tx)/2+20, (sy+ty)/2-20, tx, ty]
['L', tx, ty]
]
}
});
}
}, 'line');
let graph = null;
const containerRef = ref(null);
@ -87,28 +119,74 @@ onMounted(() => {
// --- 2. G6 Graph Initialization ---
const container = containerRef.value;
// 1. combo
const comboNodeCount = {};
nodes.forEach(node => {
comboNodeCount[node.comboId] = (comboNodeCount[node.comboId] || 0) + 1;
});
//
const sortedCombos = combos
.map(combo => ({ ...combo, count: comboNodeCount[combo.id] || 0 }))
.sort((a, b) => b.count - a.count);
//
const topCombos = sortedCombos.slice(0, 3);
const bottomCombos = sortedCombos.slice(3, 6);
const topMaxR = Math.max(...topCombos.map(c => c.r));
const bottomMaxR = Math.max(...bottomCombos.map(c => c.r));
// = combo +
function getX(i, combosArr, width) {
let x = combosArr[0].r + 60; //
for (let j = 0; j < i; j++) {
x += combosArr[j].r + combosArr[j + 1].r + 80; // 80
}
return x;
}
const width = container.scrollWidth || 1200;
const height = container.scrollHeight || 800;
const topY = height * 0.25;
const bottomY = height * 0.75;
//
topCombos.forEach((combo, idx) => {
combo.x = getX(idx, topCombos, width);
combo.y = topY;
});
//
bottomCombos.forEach((combo, idx) => {
combo.x = getX(idx, bottomCombos, width);
combo.y = bottomY;
});
// combos
combos.forEach(c => {
const found = [...topCombos, ...bottomCombos].find(sc => sc.id === c.id);
c.x = found.x;
c.y = found.y;
});
// 5. combo
nodes.forEach(node => {
const combo = combos.find(c => c.id === node.comboId);
// combo
const angle = Math.random() * 2 * Math.PI;
const radius = Math.random() * (combo.r - nodeSize / 2);
node.x = combo.x + Math.cos(angle) * radius;
node.y = combo.y + Math.sin(angle) * radius;
});
// 6. layout: nullG6
graph = new G6.Graph({
container,
width,
height,
layout: null, //
groupByTypes: false, // combo-to-combo
// --- MODIFICATION: Set preventComboOverlap to true to avoid circle overlaps ---
layout: {
type: 'comboForce',
preventComboOverlap: true, // combo
preventNodeOverlap: true, //
linkDistance: 250, //
nodeSpacing: 50, //
comboSpacing: 150, // combo
gravity: 40,
comboGravity: 30,
},
groupByTypes: false,
layout: null,
modes: {
default: ['drag-canvas', 'zoom-canvas', 'drag-combo', 'drag-node'],
default: ['drag-canvas', 'zoom-canvas', 'drag-combo', 'drag-node']
},
defaultNode: {
size: nodeSize,