Skip to content

Commit

Permalink
Hopefully a performance improvement to tracebacks
Browse files Browse the repository at this point in the history
 I feel like this way is probably more efficient and faster but I haven't tested
  • Loading branch information
controversial committed Apr 6, 2016
1 parent 8fa1417 commit dfc633e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
39 changes: 22 additions & 17 deletions js/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ function lightenHex(hex,percent) {
});
return rgbToHex(newRgb); //and back to hex
}
// Get the color for a node, lighten an orange based on level. Subtle.
// Get the color for a node, lighten a blue based on level. Subtle.
function getColor(level) {
return lightenHex("#40C4FF",5*level); //Gets 10% lighter for each level
return lightenHex("#40C4FF",5*level); // Gets 5% lighter for each level
}
// Get the highlighted color for a node, lighten a blue based on level. Subtle.
function getBlueColor(level) {
return lightenHex("#FFC400",5*level); //Gets 10% lighter for each level
// Get the highlighted color for a node, lighten a yellow based on level. Subtle.
function getYellowColor(level) {
return lightenHex("#FFC400",5*level); // Gets 5% lighter for each level
}
// Get the color that an edge should be pointing to a certain level
function getEdgeColor(level) {
Expand Down Expand Up @@ -86,21 +86,26 @@ function getNeutralId(id) {

// == NETWORK SHORTCUTS == //

// Color a node
function colorNode(node,color) {
node.color=color;
// Prevent snapping
delete node.x;
delete node.y;

nodes.update(node);
// Color nodes from a list based on their level. If color=1, highlight color will be used.
function colorNodes(ns,color) {
var colorFunc = color ? getYellowColor : getColor;

for (var i=0; i<ns.length; i++) {
ns[i].color=colorFunc(ns[i].level);
// Prevent snapping
delete ns[i].x;
delete ns[i].y
}
nodes.update(ns);
isReset = false;
}

// Set the width of an edge
function edgeWidth(edge,width) {
edge.width = width;
edges.update(edge);
// Set the width of some edges.
function edgesWidth(es,width) {
for (var i=0; i<es.length; i++) {
es[i].width = width;
}
edges.update(es);
isReset = false;
}

Expand Down
46 changes: 21 additions & 25 deletions js/main_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,47 +75,43 @@ function getTraceBackEdges(tbnodes) {

//Reset the color of all nodes, and width of all edges.
function resetProperties() {
//console.time("reset");
if (!isReset) {
selectedNode = null;
//Reset node color
var nodeids = tracenodes;
for (var i=0; i<nodeids.length; i++) {
var node = nodes.get(nodeids[i]);
var level = node.level;
colorNode(node,getColor(level));
}
var modnodes = tracenodes.map(function(i){return nodes.get(i)});
colorNodes(modnodes, 0);
//Reset edge width and color
var edgeids = traceedges;
for (var i=0; i<edgeids.length; i++) {
var edge = edges.get(edgeids[i]);
edge.color = getEdgeColor(nodes.get(edge.to).level);
edgeWidth(edge,1);
}
var modedges = traceedges.map(function(i){
var e=edges.get(i);
e.color=getEdgeColor(nodes.get(e.to).level);
return e;
});
edgesWidth(modedges, 1);
tracenodes = [];
traceedges = [];
}
//console.timeEnd("reset");
}

//Highlight the path from a given node back to the central node.
function traceBack(node) {
if (node != selectedNode) {
//console.time("trace");
selectedNode = node;
resetProperties();
tracenodes = getTraceBackNodes(node);
traceedges = getTraceBackEdges(tracenodes);
//Color nodes blue
for (var i=0; i<tracenodes.length; i++) {
var pagename = tracenodes[i];
var node = nodes.get(pagename); //The node we're iterating on
var level = node.level;
colorNode(node, getBlueColor(level));
}
//Color nodes yellow
var modnodes = tracenodes.map(function(i){return nodes.get(i)});
colorNodes(modnodes, 1);
//Widen edges
for (var i=0; i<traceedges.length; i++) {
var edgeid = traceedges[i];
var edge = edges.get(edgeid); //The node we're iterating on
edge.color = {inherit:"to"}
edgeWidth(edge,5);
}
var modedges = traceedges.map(function(i){
var e=edges.get(i);
e.color={inherit:"to"};
return e;
});
edgesWidth(modedges, 5);
// console.timeEnd("trace");
}
}

0 comments on commit dfc633e

Please sign in to comment.