我有一个显示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,但它不起作用。
答案 0 :(得分: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中有用(尽管据我所知不应该),我会接受这个答案。