gpt4 book ai didi

javascript - Cytoscape JS 布局加载微调器

转载 作者:行者123 更新时间:2023-11-29 21:11:18 25 4
gpt4 key购买 nike

我想在 Cytoscape JS 中加载和布局一个巨大的图形时显示一个加载微调器。但是即使布局未完成,加载微调器也会消失。我想知道是否有一种方法可以收听布局完成并显示微调器直到达到最终布局?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/2.7.14/cytoscape.js"></script>


<title></title>

<style media="screen">
#cy {
position: absolute;
width:100%;
height:100%;
/*height:500px;*/
z-index: 0;
margin-left: auto;
margin-right: auto;
}


#loading {
position: absolute;
background: #ffffff;
display: block;
left: 0;
top: 50%;
width: 100%;
text-align: center;
margin-top: -0.5em;
font-size: 2em;
color: #000;
}

#loading.loaded {
display: none;
}

</style>
</head>
<body>

<div id="cy">
</div>
<div id="loading">
<span class="fa fa-refresh fa-spin"></span>
</div>

<script type="text/javascript">

$(document).ready(function(){
// Create random JSON object
var maximum = 500;
var minimum = 1;

function getRandNumber(){
var min = 1;
var max = 1000;
var randNumber = Math.floor(Math.random() * (max - min + 1)) + min;
return randNumber;
}

nodes = [];
geneIds = [];
edges = [];
for(var i = 0; i < 2000; i++){
var source = getRandNumber();
var target = getRandNumber();
edges.push({"data": {"id":i.toString(),"source":source.toString(),"target":target.toString()}});
if ($.inArray(source, geneIds) === -1) {
nodes.push({"data": {"id":source.toString(),"name":source.toString()}});
geneIds.push(source);
}
if ($.inArray(target, geneIds) === -1) {
nodes.push({"data":{"id":target.toString(),"name":target.toString()}});
geneIds.push(target);
}
}

var networkData = {"nodes":nodes,"edges":edges};
// console.log(networkData);

///////////////// Create the network
var coseLayoutParams = {
name: 'cose',
// padding: 10,
randomize: false,
};


var cy = window.cy = cytoscape({
container: document.getElementById('cy'),
// elements: networkData,
minZoom: 0.1,
// maxZoom: 10,
wheelSensitivity: 0.2,
style: [
{
selector: 'node',
style: {
'content': 'data(name)',
'text-valign': 'center',
'text-halign': 'center',
'font-size': 8
}
}],
layout: coseLayoutParams
});


cy.add(networkData);
var layout = cy.makeLayout(coseLayoutParams);
layout.run();
$("#loading").addClass("loaded");

});
</script>
</body>
</html>

最佳答案

您可以向您的布局对象添加一个监听器,等待触发 'layoutstop' 事件:

layout.on('layoutstop', function() {
//... unload spinner here
});

请看这里:http://js.cytoscape.org/#layout.on

这里:https://github.com/cytoscape/cytoscape.js/blob/master/documentation/md/events.md

或者,你可以在布局选项中指定一个回调函数,比如

var coseLayoutParams = {
name: 'cose',
// padding: 10,
randomize: false,

// Called on `layoutstop`
stop: function() {
//... unload spinner here
},
};

请看这里:http://js.cytoscape.org/#layouts/cose

关于javascript - Cytoscape JS 布局加载微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com