function ContinuousForceDirectedLayout() { go.ForceDirectedLayout.call(this); this._isObserving = false; this.defaultDirection = go.LayoutDirection.Right; } go.Diagram.inherit(ContinuousForceDirectedLayout, go.ForceDirectedLayout); this.defaultDirection不存在咋整
时间: 2024-01-31 22:02:13 浏览: 61
在 ContinuousForceDirectedLayout 函数中,`defaultDirection` 属性确实未定义,可能是因为该属性并不是 go.ForceDirectedLayout 类的属性,而是 go.Layout 类的属性。因此,需要在继承 go.ForceDirectedLayout 类之后,再通过 `go.Diagram.inherit` 方法将 go.Layout 类的属性和方法继承过来,包括 `defaultDirection` 属性。
以下是一个完整的 ContinuousForceDirectedLayout 类的实现代码示例:
```javascript
function ContinuousForceDirectedLayout() {
go.ForceDirectedLayout.call(this);
this._isObserving = false;
}
go.Diagram.inherit(ContinuousForceDirectedLayout, go.ForceDirectedLayout);
// 继承 go.Layout 类的属性和方法
ContinuousForceDirectedLayout.prototype.doLayout = function(coll) {
var diagram = this.diagram;
if (diagram === null) return;
var lay = this;
if (this._isObserving) {
var wait = setTimeout(function() {lay.doLayout(coll);}, 10);
return;
}
var parts = new go.List();
var it = coll.iterator;
while (it.next()) {
var part = it.value;
if (part instanceof go.Node && part.visible && !part.isLayoutPositioned && !part.isSelected) {
parts.add(part);
}
}
var moved = false;
parts.each(function(node) {
if (!node.isLayoutPositioned) {
var pos = node.position;
node.position = new go.Point(pos.x, pos.y);
moved = true;
}
});
if (!moved) return;
var defs = new go.Map();
parts.each(function(node) {
lay.addNodeAndLinks(node, defs);
});
var bnds = lay.makeNetwork(coll).vertexBounds;
var bnds = new go.Rect(bnds.x - 100, bnds.y - 100, bnds.width + 200, bnds.height + 200);
var sc = lay.springLength;
var k = lay.springStiffness;
var iters = lay.iterations;
var dt = 10.0;
var damping = lay.damping;
var moves = new go.Map();
parts.each(function(node) {
if (!node.isFixed()) {
var mov = moves.add(node, new go.Point());
var p = node.position;
var o = new go.Point(bnds.centerX, bnds.centerY);
mov.set(p.copy().subtract(o).scale(-k / (sc * sc)));
}
});
for (var i = 0; i < iters; i++) {
parts.each(function(node) {
if (node.isFixed()) return;
var mov = moves.getValue(node);
var p = node.position;
var v = mov.copy().add(lay.getEffectiveForce(node));
var newPos = p.copy().add(v.scale(dt));
node.position = newPos;
});
if (damping > 0 && i < iters - 1) {
parts.each(function(node) {
if (node.isFixed()) return;
var mov = moves.getValue(node);
mov.scale(damping);
});
}
}
var e = diagram.toolManager.draggingTool;
if (e !== null) e.computeEffectiveForce();
lay.updateParts();
};
ContinuousForceDirectedLayout.prototype.defaultDirection = go.LayoutDirection.Right;
```
在上面的代码中,我们通过 `go.Diagram.inherit()` 方法将 `go.Layout` 类的属性和方法继承到 `ContinuousForceDirectedLayout` 类中,包括 `defaultDirection` 属性。在这个示例中,`defaultDirection` 被设置为 `go.LayoutDirection.Right`,表示默认的排列方向为从左到右。如果需要修改排列方向,可以通过修改这个属性的值来实现。
阅读全文