Created
April 8, 2013 15:03
-
-
Save alx/5337474 to your computer and use it in GitHub Desktop.
Sigma.js Modularity coloration plugin. Depends on d3js for d3.scale.category20() call.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
(function(){ | |
var Modularity = function(sig) { | |
sigma.classes.Cascade.call(this); | |
var self = this, | |
domains = []; | |
this.p = {}; | |
function getIndexOfDomain(domain) { | |
var index = domains.indexOf(domain); | |
if(index == -1) { | |
index = domains.length; | |
domains.push(domain); | |
} | |
return index; | |
} | |
function getAttributeIndex(node, attr) { | |
for ( var i = node.attr.attributes.length - 1; i > -1; i--) { | |
if(node.attr.attributes[i].attr == attr) | |
return i | |
} | |
} | |
function getAttributeValue(node, attr) { | |
var attrIndex = getAttributeIndex(node, attr); | |
return node['attr']['attributes'][attrIndex]['val']; | |
} | |
this.computeModularity = function() { | |
var colors = d3.scale.category20(); | |
sig.graph.nodes.forEach(function(node) { | |
var category = getAttributeValue(node, 'topic'), | |
index = getIndexOfDomain(category); | |
node.color = index < 19 ? colors(index) : colors(20); | |
}); | |
} | |
} | |
sigma.publicPrototype.modulate = function() { | |
if(!this.modularity) { | |
var sigmaInstance = this; | |
var mod = new Modularity(sigmaInstance._core); | |
sigmaInstance.modularity = mod; | |
} | |
this.modularity.computeModularity(); | |
return this; | |
} | |
})() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment