Do you want to work on this issue?
You can request for a bounty in order to promote it!
[Bug]: Using addData for rendering, the result is a bit unclear every time #6658
mxm-web-develop posted onGitHub
Describe the bug / 问题描述
Reproduction link / 复现链接
No response
Steps to Reproduce the Bug or Issue / 重现步骤
import { Graph, register, ExtensionCategory, AutoAdaptLabel, DragCanvas, ClickSelect, ZoomCanvas, DragElement, } from '@antv/g6'; register(ExtensionCategory.BEHAVIOR, 'AutoAdaptLabel', AutoAdaptLabel);
const default_size = 48; export const registerDefaultGraph = ( graph: Graph, data: any, data_type?: 'gitech_finance' | 'default' ) => { if (!graph || !data) return; const dataType = data_type || 'default'; const [width, height] = graph.getSize(); console.log(width, height, '11111111111'); const centerX = width / 2; const centerY = height / 2; // 先清空数据 graph.clear(); const processedNodes = data.nodes.map((node: any) => ({ ...node, size: default_size, style: { ...node.style, x: node?.x ?? centerX, y: node?.y ?? centerY, }, })); // 设置默认布局 graph.setLayout({ type: 'force', animation: false, //animated: true, // 启用布局动画 // preventOverlap: true, }); graph.addData({ nodes: processedNodes, edges: data.edges, }); // 设置行为 graph.setBehaviors((behaviors) => { console.log(behaviors, '默认行为'); return [ ...behaviors, { type: 'AutoAdaptLabel', enable: true }, { type: 'zoom-canvas', enable: true }, { type: 'drag-canvas', enable: true }, { type: 'click-select', animation: true, multiple: true }, ]; }); console.log(graph.getBehaviors(), '默认行为'); // 设置插件 graph.setPlugins((plugins) => { console.log(plugins, '插件'); return [...plugins, { type: 'minimap' }]; });
// 添加数据前先打印检查 console.log('Processed Nodes:', data.nodes); console.log('Processed Edges:', data.edges);
try { graph.render(); setTimeout(() => { // 确保图形完全布局后再进行视图调整 graph.fitCenter({ duration: 1530, }); }, 2000); graph.on('beforerender', function () { console.log('-----------------------render开始-----------------', graph.getData()); }); graph.on('afterrender', function () { console.log('-----------------------render用时-----------------', graph.getData()); });
let startTimeRender = 0;
graph.on('beforelayout', function () {
startTimeRender = new Date().valueOf();
console.log('-----------------------layout开始-----------------', graph.getData());
});
graph.on('afterlayout', function () {
console.log(new Date().valueOf() - startTimeRender);
console.log('-----------------------layout用时-----------------', graph.getData());
});
} catch (error) { console.error('Graph update failed:', error); console.error('Nodes:', data.nodes); console.error('Edges:', data.edges); throw error; } };
G6 Version / G6 版本
Please select / 请选择
OS / 操作系统
- macOS
- Windows
- Linux
- Others / 其他
Browser / 浏览器
- Chrome
- Edge
- Firefox
- Safari (Limited support / 有限支持)
- IE (Nonsupport / 不支持)
- Others / 其他