简体   繁体   English

cytoscape.js中的动态节点内容(标签)

[英]dynamic node content (label) in cytoscape.js

I have node labels mapped to the node "name" property, and I need the label to update on the cy canvas when the name is changed. 我有节点标签映射到节点“name”属性,我需要在更改名称时在cy画布上更新标签。 I've been using the style 我一直在使用这种风格

style: cytoscape.stylesheet()
.selector('node')
  .css({
    'color': '#000000',
    'content': 'data(name)',
    'text-valign': 'center',
    'background-color': '#FFFFFF',
    'border-width': 1,
    'border-color': '#707070'
  })

and a node 和一个节点

cy.add(
    { group: "nodes", data: { id: "n0", name: 'name' }, position: { x: 100, y: 100 } }
);

and updating the node with 并用。更新节点

cy.$('#n0').data('name', 'newName')

Using 2.2.10, the node label (content) is updated on the canvas as expected. 使用2.2.10,节点标签(内容)在画布上按预期更新。 Since upgrading to version 2.3.1, this no longer works. 从升级到2.3.1版本开始,这不再有效。 Any suggestions for how to achieve this again would be greatly appreciated! 任何关于如何再次实现这一点的建议将不胜感激!

Edit I don't know why that doesn't work, but for anyone else having this problem, for the time being I'm using eles.flashClass() to very briefly remove the node label for the node. 编辑我不知道为什么这不起作用,但对于有这个问题的其他人,暂时我正在使用eles.flashClass()来非常简单地删除节点的节点标签。 When the temporary class is removed, the correct label is rendered. 删除临时类时,将呈现正确的标签。 Eg 例如

in the css style set on init 在init上设置的css样式

.selector('node.nolabel')
    .css({
        'content': ''
})

then to rename a node 然后重命名一个节点

cy.$('#n0').data('name', 'newName').flashClass('nolabel',1) //class applied for 1ms then removed

This works but it doesn't seem like it should be necessary, I'd really like to know why 这有效,但似乎不应该是必要的,我真的很想知道为什么

content: 'data(name)'

isn't working - I don't know if it's a bug or I'm doing something wrong, only that it works below version 2.3.0 不工作 - 我不知道这是一个错误还是我做错了什么,只是它在版本2.3.0以下工作

Please follow this ticket: https://github.com/cytoscape/cytoscape.js/issues/678 请关注此票: https//github.com/cytoscape/cytoscape.js/issues/678

From the ticket: 从机票:

This is probably due to the performance enhancements on style for 2.3. 这可能是由于2.3样式的性能增强。 Now, instead of style being applied cumulatively, a highly performant diff of matching selector contexts is done. 现在,不是累积地应用样式,而是完成匹配选择器上下文的高性能差异。 And only the properties that need to be applied as a result of the diff are applied. 并且仅应用作为diff的结果需要应用的属性。 I suspect that because the matching contexts are the same etc, the style is not updated. 我怀疑因为匹配的上下文是相同的等,所以样式不会更新。

I'm not really sure about the quality of my solution, and if it is what you were looking for, but I did this (version >=2.5) 我不太确定我的解决方案的质量,如果它是你想要的,但我做了这个(版本> = 2.5)

cy.nodes([filter]).on('eventname').css({content:[newLabelvalue]})
example: 
var node = cy.$([selector]);
node.on('grab', function () {
            var field = $("input[id="+ nodeId + "]");
            this.css({content: field.val()});
            field.hide();
        });

I didn't declare an init 'content'-property in cytoscape style 我没有在cytoscape风格中声明一个init'content'-property

hope this helps 希望这可以帮助

This was added in version 2.5.0, see this ticket: https://github.com/cytoscape/cytoscape.js/issues/1041 这是在2.5.0版本中添加的,请参阅此票证: https//github.com/cytoscape/cytoscape.js/issues/1041

To use it, set 'width': 'label' , and to get extra space around the text use the padding property, for example 'padding': 5 for 5 pixels. 要使用它,设置'width': 'label' ,并在文本周围获得额外的空间使用padding属性,例如'padding': 5表示5像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM