简体   繁体   English

在 Cytoscape.js 中设置节点 position

[英]Set node position in Cytoscape.js

I am using the excellent Cytoscape.js for graphing.我正在使用出色的Cytoscape.js进行绘图。

I have previously been using the Cola option for this as it uses force direction.我以前一直在为此使用可乐选项,因为它使用力方向。

However I now want to visualize multiple graphs that have no connections and realize that fCose is much better at doing this.然而,我现在想可视化多个没有连接的图表,并意识到fCose在这方面做得更好。

The problem I am now faced with is I am now unable to set node positions when I already have coordinates to do so.我现在面临的问题是,当我已经有坐标可以设置节点位置时,我现在无法设置节点位置。

I was able to achive this using Cola by doing the following in the layout options -通过在布局选项中执行以下操作,我能够使用Cola实现这一目标 -

    name: 'cola',
    padding: layoutPadding,
    nodeSpacing: 40,
    edgeLengthVal: 20,
    animate: false,              // setting this to false
    randomize: false,            // and this to false allowed me to manually set the node position coordinates
    maxSimulationTime: maxLayoutDuration,
    boundingBox: { 
        x1: 0,
        y1: 0,
        x2: 10000,
        y2: 10000
    },
    edgeLength: function (e) {
        let w = e.data('weight');
        return w;
    }

Also in the defaults of Cola positions is set to undefined , which I assume means that if coordinates are passed in they will be set accordingly.同样在Cola的默认positions设置为undefined ,我假设这意味着如果传入坐标,它们将被相应地设置。

However my problem is in fCose is, no matter what I do, when passing the coordinates of the nodes in it is having zero affect when rendered.然而,我的问题在于fCose ,无论我做什么,当传递其中的节点坐标时,渲染时的影响为零。

In the defaults of fCose I see three variables I thought if I altered would have an effect -fCose的默认值中,我看到了三个变量,我认为如果我改变了会产生影响 -

    quality: "default",
    // Use random node positions at beginning of layout
    // if this is set to false, then quality option must be "proof"
    randomize: true,
    // Whether or not to animate the layout
    animate: true,

So I set quality to "proof" and both randomize and animate to false .所以我将quality设置为“proof”,并将randomizeanimatefalse

I also tried adding positions: undefined , however none of the above changes had any affect on the node positioning.我也尝试添加positions: undefined ,但是上述更改都没有对节点定位产生任何影响。 How can I achive this with fCose ?如何使用fCose实现这一目标?

In fCoSE, if randomize is true, nodes are spread to random positions initially and then algorithm tries to find a good layout of the graph.在 fCoSE 中,如果 randomize 为真,则节点最初会散布到随机位置,然后算法会尝试找到图的良好布局。

If randomize is false, algorithm starts from the current positions of the nodes.如果 randomize 为 false,则算法从节点的当前位置开始。 From current position, I mean the position attribute found in each cytoscape node.从当前的 position 来看,我的意思是在每个 cytoscape 节点中找到的 position 属性。 If you want to start the layout from the desired positions, first you need to assign those positions to nodes by using node.position() or nodes.positions() and then start the layout with randomize: false.如果要从所需位置开始布局,首先需要使用node.position()nodes.positions()将这些位置分配给节点,然后使用 randomize: false 开始布局。 Otherwise, layout currently doesn't accept initial positions directly.否则,布局当前不直接接受初始位置。

I had to solve the same problem with cytoscape js and I achieved it by using the 'preset' layout options.我必须用 cytoscape js 解决同样的问题,我通过使用“预设”布局选项实现了它。

If you want to specify your node positions yourself in your elements JSON, you >can use the preset layout — by default it does not set any positions, leaving >your nodes in their current positions (ie specified in options.elements at >initialisation time).如果您想在元素 JSON 中自己指定节点位置,您 > 可以使用预设布局 - 默认情况下它不设置任何位置,将 > 您的节点留在当前位置(即在 >initialisation time 在 options.elements 中指定)。

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

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