时间模式
示例加载中…
import { Graph } from '@antv/g6';
const startTime = new Date('2023-08-01').getTime();
const diff = 3600 * 24 * 1000;
const timebarData = [10, 2, 3, 4, 15, 10, 5, 0, 3, 1].map((value, index) => ({
time: new Date(startTime + index * diff),
value,
label: new Date(startTime + index * diff).toLocaleString(),
}));
const graphData = {
nodes: new Array(49).fill(0).map((_, index) => ({
id: `node-${index}`,
data: {
timestamp: startTime + (index % 10) * diff,
value: index % 20,
label: new Date(startTime + (index % 10) * diff).toLocaleString(),
},
})),
edges: new Array(49).fill(0).map((_, i) => ({
id: `edge-${i}`,
source: `node-${i % 30}`,
target: `node-${(i % 20) + 29}`,
data: {
edgeType: 'e1',
},
})),
};
const graph = new Graph({
container: 'container',
data: graphData,
behaviors: ['drag-canvas', 'drag-element', 'zoom-canvas'],
layout: {
type: 'grid',
cols: 7,
},
plugins: [
{
type: 'timebar',
key: 'timebar',
data: timebarData,
width: 450,
loop: true,
},
],
autoFit: 'view',
padding: [10, 0, 65, 0],
});
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;
}