[Bug]: 当我初始化设置了node或者edge的颜色,后续更新就无效了吗? #7113
Answered
by
yvonneyx
JOECHEN0319
asked this question in
Q&A
-
Describe the bug / 问题描述 const init = async () => {
// 创建图实例
const newGraph = new Graph({
container: mindMapRef.value,
autoFit: "view",
data: treeToGraphData(data),
node: {
style: {
labelText: d => d.id,
labelPlacement: "right",
labelBackground: true,
ports: [{ placement: "right" }, { placement: "left" }],
},
},
edge: {
type: "cubic-horizontal",
style: {},
},
layout: {
type: "mindmap",
direction: "LR",
getHeight: () => 32,
getWidth: () => 32,
getVGap: () => 4,
getHGap: () => 100,
},
behaviors: ["collapse-expand", "drag-canvas", "zoom-canvas"],
});
newGraph.render();
// 等待布局完成
setTimeout(() => {
// 获取所有节点
const graphData = newGraph.getData();
// 按层级关系组织节点和边,以便每个节点和相关边一起变色
animateNodeWithEdges(newGraph, graphData.nodes, graphData.edges);
}, 500);
graph.value = newGraph;
};
// 节点和相关边一起变色的动画
function animateNodeWithEdges(graph, nodes, edges) {
const delay = 300; // 每组的延迟时间
// 创建节点ID到边的映射
const nodeToEdgesMap = {};
// 为每个节点查找相关的边
nodes.forEach(node => {
nodeToEdgesMap[node.id] = edges.filter(
edge => edge.source === node.id || edge.target === node.id
);
});
// 逐个处理节点及其相关边
nodes.forEach((node, index) => {
setTimeout(() => {
try {
// 准备更新数据
const updateData = {
nodes: [{ id: node.id, style: { fill: "red" } }],
edges: []
};
// 添加相关边的更新
const relatedEdges = nodeToEdgesMap[node.id];
if (relatedEdges && relatedEdges.length > 0) {
relatedEdges.forEach(edge => {
updateData.edges.push({
id: edge.id,
style: { stroke: "red" }
});
});
}
// 批量更新节点和相关边
graph.updateData(updateData);
graph.draw();
} catch (err) {
console.error(`更新节点 ${node.id} 及相关边出错:`, err);
}
}, index * delay);
});
}如果我初始化时设置了相关style,后续使用updateData去更新就无效,无论是node和edge都是这样,但是当初始化不设置,例如不设置fill时,后面我要改fill就可以成功。我原本想使用opacity从0到1去实现元素的逐级显示的,现在没法搞了 Reproduction link / 复现链接No response Steps to Reproduce the Bug or Issue / 重现步骤No response Version / 版本🆕 5.x OS / 操作系统
Browser / 浏览器
|
Beta Was this translation helpful? Give feedback.
Answered by
yvonneyx
May 12, 2025
Replies: 2 comments
-
|
看起来是配置的优先级问题,理论上 data 上的优先级高于 style 吧。 |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
文档里有提到如何调整优先级,可以看下 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
yvonneyx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
文档里有提到如何调整优先级,可以看下
https://g6.antv.antgroup.com/manual/element/node/overview#%E8%B0%83%E6%95%B4%E4%BC%98%E5%85%88%E7%BA%A7