Commit e013a462 authored by Romain Loth's avatar Romain Loth

project explorer test: shuffled array seems more logical than sorting function

parent a0d7fd82
......@@ -359,12 +359,13 @@ function colorsBy(daclass) {
}
var v_nodes = getVisibleNodes();
colorList.sort(function(){ return Math.random()-0.5; });
// shuffle on entire array is better than random sorting function on each element
var randomColorList = shuffle(colorList)
for(var i in v_nodes) {
var the_node = TW.Nodes[ v_nodes[i].id ]
var attval = ( isUndef(the_node.attributes) || isUndef(the_node.attributes[daclass]) )? v_nodes[i][daclass]: the_node.attributes[daclass];
TW.partialGraph._core.graph.nodesIndex[v_nodes[i].id].color = colorList[ attval ]
TW.partialGraph._core.graph.nodesIndex[v_nodes[i].id].color = randomColorList[ attval ]
}
TW.partialGraph.draw();
......@@ -395,3 +396,25 @@ function makeEdgeWeightUndef() {
TW.partialGraph._core.graph.edges[e].weight=1;
}
}
// shuffle algo from stackoverflow.com/a/6274398/2489184
function shuffle(array) {
var counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment