Newer
Older
indexation / public / js / force-atlas2-graph.js
"use strict";

/**
 * Just a simple example to show how to use the sigma.layouts.fruchtermanReingold
 * plugin:
 *
 * A random graph is generated, such that its nodes are separated in some
 * distinct clusters. Each cluster has its own color, and the density of
 * links is stronger inside the clusters. So, we expect the algorithm to
 * regroup the nodes of each cluster.
 */
function loadJSON(path, callback) {

  var xobj = new XMLHttpRequest();
  xobj.overrideMimeType("application/json");
  xobj.open('GET', path, true);
  xobj.onreadystatechange = function() {
    if (xobj.readyState == 4 && xobj.status == "200") {
      // .open will NOT return a value but simply returns undefined in async mode so use a callback
      callback(xobj.responseText);
    }
  }
  xobj.send(null);
}

function initGraph(url, corpus) {
  var loc = window.location.pathname,
    dir = loc.substring(0, loc.lastIndexOf('/'));

  // Call to function with anonymous callback
  loadJSON(url, function(response) {
    var graph = JSON.parse(response);

    var edges = [],
      nodes = [];
    for (var i = 0; i < graph.nodes.length; i++) {
      nodes.push(i);
    }
    for (var i = 0; i < graph.links.length; i++) {
      var edge = graph.links[i];
      edge.weight = edge.value;
      edges.push(edge);
    }

    // Create the "community"
    var community = jLouvain().nodes(nodes).edges(edges),
      res = community();

    // Affect community for each node
    for (var key in res) {
      graph.nodes[nodes[key]].group = res[key];
    }

    var deleted_links = {},
      selectedLinks = graph.links.filter(function(d) {
        var res = (d.value >= 3);
        if (!res) {
          deleted_links[d.source] = deleted_links[d.source] + 1 || 1;
          deleted_links[d.target] = deleted_links[d.target] + 1 || 1;
        }
        return res;
      }),
      selectedNodes = graph.nodes.filter(function(d) {
        d.value = d.value - (deleted_links[d.id] || 0);
        var res = (d.value > 0);
        return res;
      });

    var i,
      s,
      o,
      N = selectedNodes.length,
      E = selectedLinks.length,
      C = {},
      d = 0.5,
      colors = {},
      g = {
        nodes: [],
        edges: []
      };

    // Clusters
    for (var i = 0; i < N; i++) {
      C[selectedNodes[i].group] = "";
    }

    for (var k in C) {
      C[k] = '#' + (Math.floor(Math.random() * 16777215).toString(16) + '000000').substr(0, 6);
    }

    for (i = 0; i < N; i++) {
      g.nodes.push({
        id: selectedNodes[i].id,
        label: selectedNodes[i].data.id,
        x: 100 * Math.cos(2 * i * Math.PI / N),
        y: 100 * Math.sin(2 * i * Math.PI / N),
        size: 1,
        color: C[selectedNodes[i].group]
      });
    }

    for (i = 0; i < E; i++) {
      g.edges.push({
        id: 'e' + i,
        source: selectedLinks[i].source,
        target: selectedLinks[i].target,
        color: "#546e7a"
      });
    }

    s = new sigma({
      graph: g,
      container: 'container'
    });

    s.startForceAtlas2({
      linLogMode: false,
      outboundAttractionDistribution: false,
      adjustSizes: false,
      edgeWeightInfluence: 0,
      scalingRatio: 1,
      strongGravityMode: false,
      gravity: 1,
      barnesHutOptimize: false,
      barnesHutTheta: 0.5,
      slowDown: 1,
      startingIterations: 1,
      iterationsPerRender: 1
    });

    var timeout = setTimeout(function() {
      if (s.isForceAtlas2Running()) {
        s.stopForceAtlas2()
      } else {
        clearTimeout(timeout);
      };
    }, 240000);
  });
}