简体   繁体   English

Cytoscape.js-layout()在Firefox中无效,在Chrome中有效

[英]Cytoscape.js - layout() doesn't work in Firefox, works in Chrome

I have a page showing a graph with Cytoscape.js, but .layout() does not work for me in Firefox, however it does in Chrome. 我有一个页面显示了Cytoscape.js的图形,但是.layout()在Firefox中不适用于我,但是在Chrome中却适用。 Chrome shows the graph with the layout. Chrome显示带有布局的图形。 Firefox shows the graph but all nodes are cluttered together. Firefox显示该图,但所有节点都杂乱无章。 It looks the same in Chrome if I use no layout, so it seems the layout does not work in Firefox for some reason. 如果我不使用布局,则在Chrome中看起来相同,因此,由于某种原因,布局似乎在Firefox中不起作用。

I tried a few layouts from the Cytoscape.js website ( http://js.cytoscape.org/#layouts ) and they work in Chrome, but not in Firefox. 我在Cytoscape.js网站( http://js.cytoscape.org/#layouts )上尝试了几种布局,它们在Chrome中可用,但在Firefox中不可用。 I currently use the cose layout ( http://js.cytoscape.org/#layouts/cose ). 我目前使用的是cose布局( http://js.cytoscape.org/#layouts/cose )。

Firefox version is 52.0.2 Firefox版本为52.0.2

Chrome is Version 57.0.2987.133 Chrome是57.0.2987.133版

Cytoscape.js is version 3.0.0 Cytoscape.js版本是3.0.0

My Javascript: 我的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); 
});

My page: 我的页面:

{% 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 is valid JSON I get from Python. graphElements是我从Python获取的有效JSON。

Example (the whole thing is too big to post it all here): {"grabbable": true, "data": {"label-size": 9, "width": 0.4, "color": "#6699CC", "id": "something_about_boy"}, "group": "nodes", "classes": "node-class"}, 示例(整个东西太大了,无法在此处全部张贴): {"grabbable": true, "data": {"label-size": 9, "width": 0.4, "color": "#6699CC", "id": "something_about_boy"}, "group": "nodes", "classes": "node-class"},

The graph works and is correct in Chrome, but the problem is the layout in Firefox does not work. 该图形有效且在Chrome中是正确的,但问题是Firefox中的布局不起作用。 What can I do to fix that? 我该如何解决?

EDIT: 编辑:

I have tried Internet Explorer too, it does not work. 我也尝试过Internet Explorer,它不起作用。

The problem was that I have been following the introduction: 问题是我一直在关注介绍:

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

Which does not tell you the following (from http://js.cytoscape.org/#notation/position ): 哪个不告诉您以下内容(来自http://js.cytoscape.org/#notation/position ):

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

I don't care about the initial position of my nodes, so I did not add it. 我不在乎节点的初始位置,因此没有添加它。 Note that I load the nodes into the graph and have no initial nodes. 请注意,我将节点加载到图中,并且没有初始节点。 The initial nodes do not need the position, ones you load in do. 初始节点不需要位置,而您加载的位置则需要。

I added position to my Nodes (so it is now included in the JSON), and changed the Javascript (this is probably not necessary but I will add it just in case it is): 我在节点上添加了position (现在它已包含在JSON中),并更改了Javascript(这可能不是必需的,但为防万一,我会添加它):

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

It now works in both Firefox and Chrome. 现在,它可以在Firefox和Chrome中使用。

If you run into this problem, add 如果遇到此问题,请添加

position {
    x: 1,
    y: 1
}

to your nodes. 到您的节点。 If you run into this problem you either don't care about the initial position (like me) so you can use whatever or forgot to add the positions to your nodes. 如果遇到此问题,您要么不在乎初始位置(例如我),要么可以使用任何方法,也可以忘记将位置添加到节点中。

After that, the layout will work. 之后,布局将起作用。

If someone can explain why it works in Chrome (even though as I understand it shouldn't) I will accept that answer instead. 如果有人可以解释为什么它可以在Chrome中运行(即使据我所知也不行),我也会接受该答案。

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

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