// create a network
var container = document.getElementById('network');
var data = getScaleFreeNetwork(3);
var nodeInfoCard = $('.node-info');
var nodeNumber = $("#nodeNumber");
var options = {
height: '100%',
width: '100%',
interaction: {
tooltipDelay: 100,
hover: true
}
};
var network = new vis.Network(container, data, options);
network.on('selectNode', showInfoCard);
network.on('deselectNode', hideInfoCard);
function setNodeInfo(nodeNum) {
nodeNumber.html(nodeNum);
}
function unsetNodeInfo() {
nodeNumber.html('');
}
function showInfoCard(params) {
setNodeInfo(params.nodes[0]);
nodeInfoCard.removeClass('hidden fadeOutDown');
nodeInfoCard.addClass('fadeInUp');
}
function hideInfoCard() {
nodeInfoCard.removeClass('hidden fadeInUp');
nodeInfoCard.addClass('fadeOutDown');
unsetNodeInfo()
}
function getScaleFreeNetwork(nodeCount) {
var nodes = [];
var edges = [];
var connectionCount = [];
// randomly create some nodes and edges
for (var i = 0; i < nodeCount; i++) {
nodes.push({
id: i,
title: 'Node ' + i,
label: String(i),
font: {
face: 'Roboto',
size: 16
},
borderWidth: 5,
color: {
color: '#FFFFFF',
background: '#282A36',
border: 'rgba(255,255,255, .85)'
}
});
connectionCount[i] = 0;
// create edges in a scale-free-network way
if (i == 1) {
var from = i;
var to = 0;
edges.push({
from: from,
to: to
});
connectionCount[from]++;
connectionCount[to]++;
} else if (i > 1) {
var conn = edges.length * 2;
var rand = Math.floor(Math.random() * conn);
var cumul = 0;
var j = 0;
while (j < connectionCount.length && cumul < rand) {
cumul += connectionCount[j];
j++;
}
var from = i;
var to = j;
edges.push({
from: from,
to: to,
title: 'Edge ' + i,
width: 2,
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.