额外按钮
示例加载中…
import { Circle, Path } from '@antv/g';
import { Renderer } from '@antv/g-svg';
import { CircleCombo, ExtensionCategory, Graph, register } from '@antv/g6';
const collapse = (x, y, r) => {
return [
['M', x - r, y],
['a', r, r, 0, 1, 0, r * 2, 0],
['a', r, r, 0, 1, 0, -r * 2, 0],
['M', x - r + 4, y],
['L', x + r - 4, y],
];
};
const expand = (x, y, r) => {
return [
['M', x - r, y],
['a', r, r, 0, 1, 0, r * 2, 0],
['a', r, r, 0, 1, 0, -r * 2, 0],
['M', x - r + 4, y],
['L', x - r + 2 * r - 4, y],
['M', x - r + r, y - r + 4],
['L', x, y + r - 4],
];
};
class CircleComboWithExtraButton extends CircleCombo {
render(attributes, container) {
super.render(attributes, container);
this.drawButton(attributes);
}
drawButton(attributes) {
const { collapsed } = attributes;
const [, height] = this.getKeySize(attributes);
const btnR = 8;
const y = height / 2 + btnR;
const d = collapsed ? expand(0, y, btnR) : collapse(0, y, btnR);
const hitArea = this.upsert('hit-area', Circle, { cy: y, r: 10, fill: '#fff', cursor: 'pointer' }, this);
this.upsert('button', Path, { stroke: '#3d81f7', d, cursor: 'pointer' }, hitArea);
}
onCreate() {
this.shapeMap['hit-area'].addEventListener('click', () => {
const id = this.id;
const collapsed = !this.attributes.collapsed;
const { graph } = this.context;
if (collapsed) graph.collapseElement(id);
else graph.expandElement(id);
});
}
}
register(ExtensionCategory.COMBO, 'circle-combo-with-extra-button', CircleComboWithExtraButton);
const graph = new Graph({
container: 'container',
renderer: () => new Renderer(),
data: {
nodes: [
{ id: 'node-0', combo: 'combo-0', style: { x: 100, y: 100 } },
{ id: 'node-1', combo: 'combo-0', style: { x: 150, y: 100 } },
{ id: 'node-2', style: { x: 250, y: 100 } },
],
edges: [{ source: 'node-1', target: 'node-2' }],
combos: [{ id: 'combo-0' }],
},
combo: {
type: 'circle-combo-with-extra-button',
},
behaviors: ['drag-element'],
});
graph.render();
import { Graph } from '@antv/g6';
import GUI from 'lil-gui';
export function addPanel(renderPanel: (gui: GUI) => void) {
const gui = new GUI({ container: document.body });
gui.title('Control');
Object.assign(gui.domElement.style, { position: 'absolute', top: '0', right: '0', zIndex: '10' });
renderPanel(gui);
addEventListener('pagehide', () => gui.destroy(), { once: true });
}
export async function createGraph(
options: ConstructorParameters<typeof Graph>[0],
size: { width?: number; height?: number } = {},
renderPanel?: (gui: GUI, graph: Graph) => void,
) {
const container = document.createElement('div');
Object.assign(container.style, {
width: '100%',
maxWidth: `${size.width || 600}px`,
height: `${size.height || 400}px`,
});
document.getElementById('container')!.append(container);
const graph = new Graph({ ...size, ...options, width: container.clientWidth, container, autoResize: true });
addEventListener('pagehide', () => graph.destroy(), { once: true });
await graph.render();
if (renderPanel) addPanel((gui) => renderPanel(gui, graph));
return container;
}