额外标签
示例加载中…
import { Text } from '@antv/g';
import { Renderer } from '@antv/g-svg';
import { ExtensionCategory, Graph, Line, register, subStyleProps } from '@antv/g6';
class LabelEdge extends Line {
render(attributes, container) {
super.render(attributes);
this.drawEndLabel(attributes, container, 'start');
this.drawEndLabel(attributes, container, 'end');
}
drawEndLabel(attributes, container, type) {
const key = type === 'start' ? 'startLabel' : 'endLabel';
const [x, y] = this.getEndpoints(attributes)[type === 'start' ? 0 : 1];
const fontStyle = {
x,
y,
dx: type === 'start' ? 15 : -15,
fontSize: 16,
fill: 'gray',
textBaseline: 'middle',
textAlign: type,
};
const style = subStyleProps(attributes, key);
const text = style.text;
this.upsert(`label-${type}`, Text, text ? { ...fontStyle, ...style } : false, container);
}
}
register(ExtensionCategory.EDGE, 'extra-label-edge', LabelEdge);
const graph = new Graph({
container: 'container',
renderer: () => new Renderer(),
data: {
nodes: [
{ id: 'node-0', style: { x: 100, y: 100 } },
{ id: 'node-1', style: { x: 300, y: 100 } },
],
edges: [{ source: 'node-0', target: 'node-1' }],
},
edge: {
type: 'extra-label-edge',
style: {
startArrow: true,
endArrow: true,
stroke: '#F6BD16',
startLabelText: 'start',
endLabelText: 'end',
},
},
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;
}