提示框
示例加载中…
import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
data: {
nodes: [
{ id: '0', data: { label: 'node-0', description: 'This is node-0.' } },
{ id: '1', data: { label: 'node-1', description: 'This is node-1.' } },
{ id: '2', data: { label: 'node-2', description: 'This is node-2.' } },
{ id: '3', data: { label: 'node-3', description: 'This is node-3.' } },
{ id: '4', data: { label: 'node-4', description: 'This is node-4.' } },
{ id: '5', data: { label: 'node-5', description: 'This is node-5.' } },
],
edges: [
{ source: '0', target: '1', data: { description: 'This is edge from node 0 to node 1.' } },
{ source: '0', target: '2', data: { description: 'This is edge from node 0 to node 2.' } },
{ source: '0', target: '3', data: { description: 'This is edge from node 0 to node 3.' } },
{ source: '0', target: '4', data: { description: 'This is edge from node 0 to node 4.' } },
{ source: '0', target: '5', data: { description: 'This is edge from node 0 to node 5.' } },
],
},
layout: {
type: 'grid',
},
plugins: [
{
type: 'tooltip',
getContent: (e, items) => {
let result = `<h4>Custom Content</h4>`;
items.forEach((item) => {
result += `<p>Type: ${item.data.description}</p>`;
});
return result;
},
},
],
behaviors: ['drag-canvas', '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;
}