首页 > 解决方案 > mxgraph 如何获取和设置边缘的标签位置?

问题描述

我想保存边缘标签位置,我发现mxEdgeHandler.prototype.moveLabel可以得到标签位置,但是之后如何设置标签位置graph.insertEdge

标签: mxgraph

解决方案


你可以看看mxGeometry offset

mxGeometry JS API中提取

For edges, this holds the offset (in pixels) from the position defined
by <x> and <y> on the edge.  For relative geometries (for vertices), this
defines the absolute offset from the point defined by the relative coordinates.
For absolute geometries (for vertices), this defines the offset for the label.
Default is null.

如果使用insertEdge函数,不能直接设置偏移量,但是可以在函数返回的边上更新

const mxEdge = graph.insertEdge(parent, id, 'label', source, target, style);
mxCell.geometry.offset = new this.mxPoint(relativeLabelX, relativeLabelY);

请注意,您可能还必须设置边缘几何形状的 x 和 y 值,因为它们似乎未在创建 egde 时设置。

您也可以先创建边然后将其添加到图形中(这就是 insertEdge 函数为您所做的)。创建边缘 mxCell 时,您可以在将其添加到图形之前设置其几何形状,或者在添加它之后像调用 insertEdge 时一样。

另请参阅mxGraph 4.1.1 insertEdge 代码


推荐阅读