在 JointJS 图表中的链接中,我尝试实现这个教程 (http://jointjs.com/tutorial/constraint-move-to-circle) 来移动链接上的标签,但是我不知道应该把ConstraintElementView
放在哪里。
- 我想使链接的标签可以在链接上移动。那么,我如何将链接定义为可移动标签的“路径”?
ConstraintElementView
var constraint = label; // ???
var ConstraintElementView = joint.dia.ElementView.extend({
pointerdown: function(evt, x, y) {
var position = this.model.get('position');
var size = this.model.get('size');
var center = g.rect(position.x, position.y, size.width, size.height).center();
var intersection = constraint.intersectionWithLineFromCenterToPoint(center);
joint.dia.ElementView.prototype.pointerdown.apply(this, [evt, intersection.x, intersection.y]);
},
pointermove: function(evt, x, y) {
var intersection = constraint.intersectionWithLineFromCenterToPoint(g.point(x, y));
joint.dia.ElementView.prototype.pointermove.apply(this, [evt, intersection.x, intersection.y]);
}
});
链接标签
paper.on({
/**
* Doubleclick on link: Add label for link
*/
'cell:pointerdblclick': function(cellView, event, x, y){
if (cellView.model.isLink()) {
cellView.model.label(0, {
position: .5,
attrs: {
rect: { fill: '#eeeeee' },
text: { text: 'text', 'font-size': 12, ref: 'rect' }
}
});
}
}
});
纸张
var paper = new joint.dia.Paper({
el: $('#canvas'),
width: 801,
height: 496,
model: graph,
gridSize: 10,
elementView: ConstraintElementView,
defaultLink: new joint.dia.Link({
router: { name: 'manhattan' },
connector: { name: 'rounded' },
attrs: {
'.marker-target': { d: 'M 10 0 L 0 5 L 10 10 z', fill: '#6a6c8a', stroke: '#6a6c8a' },
'.connection': { stroke: '#6a6c8a', 'stroke-width': 2 }
}
})
});
- 由于它可以在连接线上移动,所以应该将其捕捉到曼哈顿风格连接线的每个段落的中心。但我找不到获取每个段落中心值的机会。
round
未定义,而Math.round不是一个合适的替代品。然而,完全删除round
同样有效。2. 我必须使用paper.on()
来运行这段代码,这在答案中并不清楚。希望这能帮助其他人。 - Vitani