弧线图
示例加载中…
import { BaseEdge, BaseLayout, ExtensionCategory, Graph, register } from '@antv/g6';
class ArcLayout extends BaseLayout {
async execute(data, options) {
const { nodeSep = 20, nodeSize } = { ...this.options, ...options };
const { nodes } = data;
return {
nodes: nodes.map((node, index) => ({
id: node.id,
style: {
x: index * (nodeSep + nodeSize),
y: 0,
},
})),
};
}
}
class ArcEdge extends BaseEdge {
getKeyPath(attributes) {
const [sourcePoint, targetPoint] = this.getEndpoints(attributes);
const [sx, sy] = sourcePoint;
const [tx] = targetPoint;
const r = Math.abs(tx - sx) / 2;
return [
['M', sx, sy],
['A', r, r, 0, 0, sx < tx ? 1 : 0, tx, sy],
];
}
}
register(ExtensionCategory.LAYOUT, 'arc', ArcLayout);
register(ExtensionCategory.EDGE, 'arc', ArcEdge);
const palette = {
analytics: 'rgb(158, 1, 66)',
data: 'rgb(213, 62, 79)',
animate: 'rgb(244, 109, 67)',
display: 'rgb(253, 174, 97)',
flex: 'rgb(254, 224, 139)',
physics: 'rgb(230, 245, 152)',
query: 'rgb(171, 221, 164)',
scale: 'rgb(102, 194, 165)',
util: 'rgb(50, 136, 189)',
vis: 'rgb(94, 79, 162)',
};
fetch('https://gw.alipayobjects.com/os/basement_prod/70cde3be-22e8-4291-98f1-4d5a5b75b62f.json')
.then((res) => res.json())
.then((data) => {
const getCluster = (id) => data.nodes.find((node) => node.id === id).cluster;
const graph = new Graph({
container: 'container',
data,
autoFit: 'view',
node: {
style: {
size: 20,
fill: (d) => palette[d.cluster],
ports: [{ position: 'top' }],
labelText: (d) => d.name,
labelTextAlign: 'start',
labelTextBaseline: 'middle',
labelTransform: [['rotate', 90]],
},
},
edge: {
type: 'arc',
style: {
stroke: (d) => `linear-gradient(${palette[getCluster(d.source)]}, ${palette[getCluster(d.source)]})`,
strokeOpacity: 0.5,
},
},
layout: {
type: 'arc',
nodeSize: 20,
preLayout: true,
},
behaviors: ['zoom-canvas', 'drag-canvas'],
});
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;
}