【问题标题】:Cytoscape.js - layout() doesn't work in Firefox, works in ChromeCytoscape.js - layout() 在 Firefox 中不起作用,在 Chrome 中起作用
【发布时间】:2017-04-13 12:34:14
【问题描述】:

我有一个显示 Cytoscape.js 图表的页面,但 .layout() 在 Firefox 中对我不起作用,但在 Chrome 中却可以。 Chrome 显示带有布局的图形。 Firefox 显示图表,但所有节点都杂乱无章。如果我不使用布局,它在 Chrome 中看起来是一样的,所以由于某种原因,布局在 Firefox 中似乎不起作用。

我从 Cytoscape.js 网站 (http://js.cytoscape.org/#layouts) 尝试了一些布局,它们在 Chrome 中工作,但在 Firefox 中不工作。我目前使用cose 布局(http://js.cytoscape.org/#layouts/cose)。

Firefox 版本为 52.0.2

Chrome 版本为 57.0.2987.133

Cytoscape.js 是 3.0.0 版

我的 Javascript:

$( document ).ready(function() {
    $("#cy").attr("foo", "foo");
    var cy = cytoscape({
          container: $("#cy"),
          layout: {
            name: 'preset'
          },
          style: [
            {
              selector: 'node',
              style: {
                'content': 'data(id)'
              }
            },
            {
                selector: "edge",
                style: {
                    "line-color": "data(color)"
                }
            },
            {
              selector: ':parent',
              style: {
                'background-opacity': 0.6
              }
            }
          ]
        });
    var options = {
              name: 'cose',

              // Called on `layoutready`
              ready: function(){},

              // Called on `layoutstop`
              stop: function(){},

              // Whether to animate while running the layout
              animate: true,

              // The layout animates only after this many milliseconds
              // (prevents flashing on fast runs)
              animationThreshold: 250,

              // Number of iterations between consecutive screen positions update
              // (0 -> only updated on the end)
              refresh: 20,

              // Whether to fit the network view after when done
              fit: true,

              // Padding on fit
              padding: 30,

              // Constrain layout bounds; { x1, y1, x2, y2 } or { x1, y1, w, h }
              boundingBox: undefined,

              // Randomize the initial positions of the nodes (true) or use existing positions (false)
              randomize: false,

              // Extra spacing between components in non-compound graphs
              componentSpacing: 100,

              // Node repulsion (non overlapping) multiplier
              nodeRepulsion: function( node ){ return 400000; },

              // Node repulsion (overlapping) multiplier
              nodeOverlap: 10,

              // Ideal edge (non nested) length
              idealEdgeLength: function( edge ){ return 10; },

              // Divisor to compute edge forces
              edgeElasticity: function( edge ){ return 100; },

              // Nesting factor (multiplier) to compute ideal edge length for nested edges
              nestingFactor: 5,

              // Gravity force (constant)
              gravity: 80,

              // Maximum number of iterations to perform
              numIter: 1000,

              // Initial temperature (maximum node displacement)
              initialTemp: 200,

              // Cooling factor (how the temperature is reduced between consecutive iterations
              coolingFactor: 0.95,

              // Lower temperature threshold (below this point the layout will end)
              minTemp: 1.0,

              // Pass a reference to weaver to use threads for calculations
              weaver: false
    };
    cy.add(graphElements);
    cy.layout(options); 
});

我的页面:

{% extends "queryapp/base.html" %}
{% block content %}
{% block title %}Default query page{% endblock %}
{% load static %}
<script>
var graphElements = {{ graph_elements|safe }};
</script>
<script src="{% static 'skimmr_django/js/jquery-3.1.1.min.js' %}"></script>
<script src="{% static 'queryapp/js/cytoscape.js' %}"></script>
<script src="{% static 'queryapp/js/result.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'queryapp/css/result.css' %}" />

<div id="cy"></div>
{% endblock %}

graphElements 是我从 Python 获得的有效 JSON。

示例(整个内容太大,无法在此处发布): {"grabbable": true, "data": {"label-size": 9, "width": 0.4, "color": "#6699CC", "id": "something_about_boy"}, "group": "nodes", "classes": "node-class"},

该图在 Chrome 中有效且正确,但问题是 Firefox 中的布局不起作用。我能做些什么来解决这个问题?

编辑:

我也尝试过 Internet Explorer,它不起作用。

【问题讨论】:

    标签: javascript jquery google-chrome firefox cytoscape.js


    【解决方案1】:

    问题是我一直在关注介绍:

    http://js.cytoscape.org/#getting-started/initialisation

    这并没有告诉您以下内容(来自http://js.cytoscape.org/#notation/position):

    position: { // the model position of the node (optional on init, mandatory after)

    我不关心我的节点的初始位置,所以我没有添加它。请注意,我将节点加载到图中并且没有初始节点。初始节点不需要位置,你加载的那些。

    我将position 添加到我的节点(因此它现在包含在 JSON 中),并更改了 Javascript(这可能不是必需的,但我会添加它以防万一):

    cy.add(graphElements);
    var layout = cy.elements().layout(options);
    layout.run();
    

    它现在可以在 Firefox 和 Chrome 中使用。

    如果遇到这个问题,添加

    position {
        x: 1,
        y: 1
    }
    

    到您的节点。如果你遇到这个问题,你要么不关心初始位置(像我一样),所以你可以使用任何东西,或者忘记将位置添加到你的节点。

    之后,布局将起作用。

    如果有人可以解释为什么它可以在 Chrome 中工作(尽管我理解它不应该),我会接受这个答案。

    【讨论】:

    • 除非您为节点指定唯一的初始位置,否则像 CoSE 这样的强制导向布局通常不起作用。至少有一个物理力与距离呈平方反比关系,因此在增量距离为零的情况下,您会得到无限/未定义的力。我认为 CoSE 有针对这种情况的解决方法,但在所有节点共享相同位置的情况下可能还不够。
    • @maxkfranz:“randomize: true”是否也能解决问题?
    • 如果布局支持randomize: true,那么您可以使用它为您设置随机初始位置。
    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2014-08-13
    相关资源
    最近更新 更多