Skip to content

Commit

Permalink
#1 - Use iwanthue for colors
Browse files Browse the repository at this point in the history
  • Loading branch information
jacomyal committed Jan 31, 2020
1 parent 91db61e commit fe85647
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const fs = require("fs");
const path = require("path");
const graphml = require("graphml-js");
const Graph = require("graphology");
const iwanthue = require("iwanthue");
const gexf = require("graphology-gexf");
const render = require("graphology-canvas");
const forceAtlas2 = require("graphology-layout-forceatlas2");
Expand Down Expand Up @@ -88,9 +89,18 @@ function processGraph({ graph }, callback) {
if (colorize) {
louvain.assign(graph);

const colors = {};
const communitiesSet = {};
graph.forEachNode((_, { community }) => {
communitiesSet[community] = true;
});
const communities = Object.keys(communitiesSet);

const colors = iwanthue(communities.length).reduce((iter, color, i) => {
iter[communities[i]] = color;
return iter;
}, {});

graph.forEachNode((node, { community }) => {
if (!colors[community]) colors[community] = getRandomColor();
graph.setNodeAttribute(node, "color", colors[community]);
});
}
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"graphology-communities-louvain": "^0.1.1",
"graphology-gexf": "^0.5.0",
"graphology-layout-forceatlas2": "^0.3.1",
"iwanthue": "^1.3.0",
"yargs": "^15.1.0"
}
}

0 comments on commit fe85647

Please sign in to comment.