Draw a circle around nodes groups
Solution 1:
I am in the middle of finishing the script, but I have to leave... will be back to finish.
//The nodeGraph variable should probably be ran through a transitive
//closure algorithm to simiplify it, so its not an overly complicated
//network
nodeGraph = {};
//create nodes and the beginnings of a dictionary for a directed graph
//to later be used to adjust the positions of nodes - note this is not
//the most efficient algorithm.
nodeCount = 10;
for (var i = 0; i < nodeCount; i++)
{
var div = document.createElement('div');
div.id = "node" + i;
div.className = "node";
div.setAttribute("group", (randomInt(1, 2) == 1) ? "A" : "B")
nodeGraph["node" + i] = [];
document.getElementsByClassName('container')[0].append(div);
}
//here I randomly create a relationship amongst nodes - but I limit it to 5 relationships just so its not too resource heavy.
//loop through each node
for (var i = 0; i < nodeCount; i++)
{
//generate number of relationships
randInt = randomInt(1, 5);
//generate random relationships
for (var j = 0; j < randInt; j++)
{
ranNum = randomInt(0, nodeCount - 1);
//console.log(ranNum);
while (nodeGraph["node" + i].includes(ranNum))
{
ranNum = randomInt(0, nodeCount - 1);
}
//console.log(ranNum);
nodeGraph["node" + i].push("node" + ranNum);
}
}
//outputs the random relationship amongst nodes
console.log(nodeGraph);
//the above code sets up the problem for what we want to achieve
//which is to essentially sort the nodes into the two "cells"
//lets get the location of the parent cells and a reference to them
groupABox = document.getElementById('GroupA');
groupABBox = groupABox.getBoundingClientRect();
groupBBox = document.getElementById('GroupB');
groupBBBox = groupBBox.getBoundingClientRect();
//then loop through every node and stick them into their respective groups
for (var i = 0; i < nodeCount; i++)
{
currentNode = document.getElementById("node" + i);
group = currentNode.getAttribute('group');
if (group == 'A')
{
relationships = nodeGraph['node' + i];
for (var j = 0; j < relationships.length; j++)
{
comparedNode = document.getElementById(relationships[j]);
if (comparedNode.getAttribute('group') == 'A')
{
}
else
{
}
}
}
}
function randomInt(min, max)
{
return Math.floor(Math.random() * (max - min + 1) + min);
}
.parentNode
{
border-radius: 100px;
border: solid black 5px;
height: 500px;
width: 200px;
position: relative;
background-color: lightblue;
}
#GroupA
{
float: left;
}
#GroupB
{
float: right;
}
.node
{
height: 20px;
width: 20px;
position: absolute;
float: none;
background-color: green;
}
<div class="container">
<div id="GroupA" class="parentNode">
</div>
<div id="GroupB" class="parentNode">
</div>
</div>
https://jsfiddle.net/Shmac/x1wf52ba/1/
Solution 2:
Using the visEvents and passing a Javascript code was able to generate the circle around the node groups.
graph %>%
visNetwork::visEvents(type = "on", beforeDrawing = "function(ctx) {
ctx.fillStyle = 'rgba(255, 0, 255, 0.1)';
ctx.ellipse(-180 , 25, 150, 280 , 0, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = 'rgba(64, 255, 255,0.1)';
ctx.ellipse(180 , 25, 150, 280, 0, 0, 2 * Math.PI);
ctx.fill();
}")