Example #1
0
test('it can layout graph', function(t) {
  var graph = generate.grid(10, 10);

  var layout = createLayout(graph);
  // just for the sake of the test we will be splitting graph into two clusters
  var cluster1 = [];
  var cluster2 = [];
  graph.forEachNode(function(node) {
    if (node.id % 2 === 0) cluster1.push(node.id);
    else cluster2.push(node.id);
  });

  layout.addCluster(cluster1);
  layout.addCluster(cluster2);

  layout.run();

  layout.forEachCluster(function(cluster, idx) {
    t.ok(cluster.positions, idx + ' has defined position');
    testBounds(cluster, 'Cluster ' + idx + ' has correct bounds');
  });

  t.end();

  function testBounds(cluster, message) {
    var b = cluster.bounds;
    Object.keys(cluster.positions).forEach(function(nodeId) {
      var node = cluster.positions[nodeId];
      t.ok(b.x1 <= node.x && node.x <= b.x2 &&
           b.y1 <= node.y && node.y <= b.y2, message + '\n' + 
          'for node ' + nodeId + ' ' + JSON.stringify(node) + ' is outside of bounds: ' + JSON.stringify(b))
    });
  }
});
test('complete graph has 0 betweenness', function(t) {
  var completeGraph = generator.complete(5);
  var betweenness = centrality.betweenness(completeGraph);
  completeGraph.forEachNode(function (node) {
    t.equals(betweenness[node.id], 0, 'Complete graph should have 0 betweenness');
  });

  t.end();
});
Example #3
0
test('Layout is created', function () {
  var graph = createGraph.path(2);
  var pixiGraphics = createPixiGraphics(graph);

  var layout = pixiGraphics.layout;

  assert.ok(layout, 'Layout is present');
  assert.ok(typeof layout.step === 'function', 'Layout is iterative');
});
Example #4
0
test('Get all nodes', function (t) {
  t.plan(10);

  var pathGraph = generators.path(10),
      g = shremlin(pathGraph);

  g.V().forEach(function (vertex) {
    t.ok(vertex, "Vertex should be present");
  });
});
test('circle should have 1 betweenness', function (t) {
  var circle = generator.path(5);
  circle.addLink(4, 0); // convert path to circle
  var betweenness = centrality.betweenness(circle);
  circle.forEachNode(function (node) {
    t.equals(betweenness[node.id], 1, 'Circle graph should have 1 betweenness');
  });

  t.end();
});
Example #6
0
test('Get multiple nodes', function(t) {
  var pathGraph = generators.path(10),
      startNodeIds = [0, 1, 2],
      g = shremlin(pathGraph);

  t.plan(startNodeIds.length);

  g.V(startNodeIds).forEach(function (vertex) {
    t.ok(startNodeIds.indexOf(vertex.id) > -1, "Start vertext should be valid");
  });
});
Example #7
0
test('Get specific node', function(t) {
  t.plan(1);

  var pathGraph = generators.path(10),
      startNodeId = 0,
      g = shremlin(pathGraph);

  g.V(startNodeId).forEach(function (vertex) {
    t.equal(vertex.id, startNodeId, "Start vertext should be valid");
  });
});
Example #8
0
test('It finds complete graph centrality', function(t) {
  var g = generator.complete(6);

  var degreeCentrality = centrality.degree(g);
  g.forEachNode(verifyDegree);
  t.end();

  function verifyDegree(node) {
    t.equals(degreeCentrality[node.id], 5);
  }
});
Example #9
0
test('Get nodes matching predicate', function(t) {
  var pathGraph = generators.path(10),
      vertexFilter = function (v) {
        return v.id < 2; // it's only 0, 1 and 2
      },
      g = shremlin(pathGraph);

  g.V(vertexFilter).forEach(function (vertex) {
    t.ok(vertexFilter(vertex), "Start vertext should be valid");
  });
  t.end();
});
Example #10
0
test('Render inside custom dom element', function () {
  var graph = createGraph.path(2);
  var container = document.createElement('div');

  var pixiGraphics = createPixiGraphics(graph, {
    container: container
  });

  assert.ok(container.childNodes.length > 0 &&
            container.childNodes[0].nodeName.match(/^canvas$/i),
            'Pixi attached itself');
});
Example #11
0
test('Respects physics settings', function () {
  var graph = createGraph.path(2);

  var physics = {
        springLength: 42,
        springCoeff: 1,
        gravity: -9.8,
        theta: 0.8,
        dragCoeff: 0,
        timeStep : 20
      };

  var pixiGraphics = createPixiGraphics(graph, {
    physics: physics
  });

  var simulator = pixiGraphics.layout.simulator;
  assert.equal(simulator.springLength(), physics.springLength, 'Spring length');
  assert.equal(simulator.springCoeff(), physics.springCoeff, 'Spring coeff');
  assert.equal(simulator.gravity(), physics.gravity, 'Gravity');
  assert.equal(simulator.theta(), physics.theta, 'Theta');
  assert.equal(simulator.dragCoeff(), physics.dragCoeff, 'Drag');
  assert.equal(simulator.timeStep(), physics.timeStep, 'TimeStep');
});
Example #12
0
function getGraphFromQueryString(query) {
  var graphGenerators = require('ngraph.generators');
  return graphGenerators.grid(3,3);
}
Example #13
0
var create = require('ngraph.generators');
var getMatrix = require('ngraph.matrix');
var graph = create.complete(5);

printMatrix('degree', getMatrix.degree(graph));
printMatrix('adjacency', getMatrix.adjacency(graph));
printMatrix('laplacian', getMatrix.laplacian(graph));

function printMatrix(name, d) {
  console.log(name);
  for (var i = 0; i < d.shape[0]; ++i) {
    var str = '';
    for (var j = 0; j < d.shape[1] ; ++j) {
      str += d.get(i, j) + ' ';
    }
    console.log(str);
  }
}