Skip to content

Instantly share code, notes, and snippets.

@alx
Created April 8, 2013 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alx/5337474 to your computer and use it in GitHub Desktop.
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.
$(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