信息密度
示例加载中…
import { Graph } from '@antv/g6';
/**
* Inspired by https://graphcommons.com/graphs/be8bc972-5b26-4f5c-837d-a34704f33a9e
*/
fetch('https://assets.antv.antgroup.com/g6/unicorns-investors.json')
.then((res) => res.json())
.then((data) => {
const size = (node) => Math.max(...node.style.size);
const graph = new Graph({
data,
autoFit: 'view',
node: {
style: {
fillOpacity: 1,
label: true,
labelText: (d) => d.data?.name,
labelBackground: true,
icon: true,
iconText: (d) => (d.data?.type === 'Investor' ? '💰' : '🦄️'),
fill: (d) => (d.data?.type === 'Investor' ? '#6495ED' : '#FFA07A'),
},
state: {
inactive: {
fillOpacity: 0.3,
icon: false,
label: false,
},
},
},
edge: {
style: {
label: false,
labelText: (d) => d.data?.type,
labelBackground: true,
},
state: {
active: {
label: true,
},
inactive: {
strokeOpacity: 0,
},
},
},
layout: {
type: 'd3-force',
link: { distance: (edge) => size(edge.source) + size(edge.target) },
collide: { radius: (node) => size(node) },
manyBody: { strength: (node) => -4 * size(node) },
animation: false,
},
transforms: [
{
type: 'map-node-size',
scale: 'linear',
maxSize: 60,
minSize: 20,
mapLabelSize: [12, 24],
},
],
behaviors: [
'drag-canvas',
'zoom-canvas',
function () {
return {
key: 'hover-activate',
type: 'hover-activate',
enable: (e) => e.targetType === 'node',
degree: 1,
inactiveState: 'inactive',
onHover: (e) => {
this.frontElement(e.target.id);
e.view.setCursor('pointer');
},
onHoverEnd: (e) => {
e.view.setCursor('default');
},
};
},
{
type: 'fix-element-size',
enable: true,
},
'auto-adapt-label',
],
animation: false,
});
graph.render();
});
const container = document.getElementById('container');
const descriptionDiv = document.createElement('div');
descriptionDiv.innerHTML = 'Network Map of 🦄 Unicorns and Their 💰Investors - 1086 nodes, 1247 edges';
container.appendChild(descriptionDiv);
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;
}