Dagre 和 React-Flow 不会自动创建句柄(边缘源和目标)。 (树可视化)
我正在使用 Dagre 和 React-flow 创建组件树可视化,不幸的是我遇到了一些困难。边缘是正确的,都具有源和目标的正确标识符,但是如果我不使用 react-flow-renderer
提供的 Handle
组件,则手柄(小点、边缘的连接点)不会出现。即使我设置了元素 targetPosition
和 sourcePosition
。我认为 el.targetPosition 和 el.sourcePosition 没有做任何事情。下面的大部分实现来自React-flow官网,他们没有使用Handle
组件。句柄 ID 为空。
您还可以在下面找到快照。
渲染元素
{元素&& (
元素={元素} 节点类型={{reactComponent:ComponentNode}} onNodeMouseEnter={(_e, 节点) =>高亮组件(节点,假)} onNodeMouseLeave={(_e, 节点) =>删除突出显示(节点)} onPaneClick={重置突出显示} > 其余代码
constpositionElements = (元素, dagreGraph, 方向) => { 返回 elements.forEach((el) => { if (isNode(el)) { if (方向 === GraphLabels.topToBottom) { dagreGraph.setNode(el.id, { 宽度:节点宽度, 高度:baseNodeHeight + el.data.linesOfCode, }); } } 别的 { dagreGraph.setEdge(el.source, el.target); } }); }; 导出 const getLayoutedElements = (元素, 方向) => { const dagreGraph = new dagre.graphlib.Graph(); // 构建图表 dagreGraph.setDefaultEdgeLabel(() => ({})); dagreGraph.setGraph({rankdir: 方向 }); positionElements(元素、dagreGraph、方向); dagre.layout(dagreGraph); 返回 elements.map((el) => { if (isNode(el)) { const nodeWithPosition = dagreGraph.node(el.id); 垂直缩放 如果(方向== GraphLabels.leftToRight){ 做这个。 } 如果(方向== GraphLabels.topToBottom){ el.targetPosition = '底部'; el.sourcePosition = '顶部'; el.位置 = { x:一些值, y:一些其他值, }; } } 返回el; }); };
I am creating a component tree visualisation with Dagre and React-flow, and unfortunately I face some difficulties. The edges are correct, all have the right identifiers for the source and the target, but if I don't use the Handle
component provided by react-flow-renderer
, the handles (small dots, connecting points for edges) won't appear. Even when I set the element targetPosition
and sourcePosition
. I think el.targetPosition
and el.sourcePosition
don't do anything. Most of the implementation below is from React-flow official website, and they don't use Handle
components. Handle id is null.
You can also find a snapshot below.
Rendering the elements
{elements && ( <ReactFlowProvider> <ReactFlow elements={elems} nodeTypes={{ reactComponent: ComponentNode }} onNodeMouseEnter={(_e, node) => highlightComponent(node, false)} onNodeMouseLeave={(_e, node) => removeHighlight(node)} onPaneClick={resetHighlight} > </ReactFlow> </ReactFlowProvider>
Rest of the code
const positionElements = (elements, dagreGraph, direction) => { return elements.forEach((el) => { if (isNode(el)) { if (direction === GraphLabels.topToBottom) { dagreGraph.setNode(el.id, { width: nodeWidth, height: baseNodeHeight + el.data.linesOfCode, }); } } else { dagreGraph.setEdge(el.source, el.target); } }); }; export const getLayoutedElements = (elements, direction) => { const dagreGraph = new dagre.graphlib.Graph(); // building the graph dagreGraph.setDefaultEdgeLabel(() => ({})); dagreGraph.setGraph({ rankdir: direction }); positionElements(elements, dagreGraph, direction); dagre.layout(dagreGraph); return elements.map((el) => { if (isNode(el)) { const nodeWithPosition = dagreGraph.node(el.id); Vertical scaling if (direction == GraphLabels.leftToRight) { do this. } if (direction == GraphLabels.topToBottom) { el.targetPosition = 'bottom'; el.sourcePosition = 'top'; el.position = { x: someValue, y: someOtherValue, }; } } return el; }); };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论