Sigmajs.js 4.17 KB
Newer Older
Abinaya Sudhir's avatar
Abinaya Sudhir committed
1 2
'use strict';

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
var dummyClass = 'DummyClass';
exports.edgeShapesClass = dummyClass;
exports.filterClass = dummyClass;
exports.forceAtlas2Class = dummyClass;
exports.loadGEXFClass = dummyClass;
exports.loadJSONClass = dummyClass;
exports.nOverlapClass = dummyClass;
exports.neoCypherClass = dummyClass;
exports.neoGraphItemsProducersClass = dummyClass;
exports.nodeShapesClass = dummyClass;
exports.randomizeNodePositionsClass = dummyClass;
exports.relativeSizeClass = dummyClass;
exports.sigmaClass = dummyClass;
exports.sigmaEnableSVGClass = dummyClass;
exports.sigmaEnableWebGLClass = dummyClass;
exports.forceLinkClass = dummyClass;

if (typeof window !== 'undefined') {

Abinaya Sudhir's avatar
Abinaya Sudhir committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
const SJS = require('react-sigma');
const FL = require('react-sigma/lib/ForceLink');

exports.edgeShapesClass = SJS.EdgeShapes;
exports.filterClass = SJS.Filter;
exports.forceAtlas2Class = SJS.ForceAtlas2;
exports.loadGEXFClass = SJS.LoadGEXF;
exports.loadJSONClass = SJS.LoadJSON;
exports.nOverlapClass = SJS.NOverlap;
exports.neoCypherClass = SJS.NeoCypher;
exports.neoGraphItemsProducersClass = SJS.NeoGraphItemsProducers;
exports.nodeShapesClass = SJS.NodeShapes;
exports.randomizeNodePositionsClass = SJS.RandomizeNodePositions;
exports.relativeSizeClass = SJS.RelativeSize;
exports.sigmaClass = SJS.Sigma;
exports.sigmaEnableSVGClass = SJS.SigmaEnableSVG;
exports.sigmaEnableWebGLClass = SJS.SigmaEnableWebGL;
exports.forceLinkClass = FL.default;
40 41

}
42

43
exports.setSigmaRef = function(props) {
Alexandre Delanoë's avatar
Alexandre Delanoë committed
44
  if (props && props.sigma) {
45 46 47 48 49 50 51 52 53 54
    window.sigmaGargInstance = props.sigma;
  }
};
exports.getSigmaRef = function() {
  return window.sigmaGargInstance;
};
exports.goToImpl = function(cam) {
  return function(props) {
    return cam.goTo(props);
  };
55
};
56 57 58 59 60 61 62 63 64 65 66
exports.pauseForceAtlas2 = function() {
  var s = window.sigmaGargInstance;
  if (s) {
    if (s.isForceAtlas2Running()) {
      s.stopForceAtlas2()
    }
    else {
      s.startForceAtlas2()
    }
  }
};
67

68
var trackMouse = function(cursorSize, e) {
69

70
    if(!e.shiftKey) {
Alexandre Delanoë's avatar
Alexandre Delanoë committed
71
      var partialGraph = window.sigmaGargInstance;
Alexandre Delanoë's avatar
Alexandre Delanoë committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85
      // new sigma.js 2D mouse context
      var ctx = partialGraph.renderers[0].contexts.mouse;
      ctx.globalCompositeOperation = "source-over";

      // clear zone each time to prevent repeated frame artifacts
      ctx.clearRect(50, 50,
                    partialGraph.renderers[0].container.offsetWidth,
                    partialGraph.renderers[0].container.offsetHeight);

      // classic mousemove event or other similar non-sigma events

      var coord = window.sigma.utils.mouseCoords(e)
      var x = (coord.x + coord.clientX) / 2 // ; // sigma.utils.getX(e);
      var y = (coord.y + coord.clientY) /2 // ; // sigma.utils.getY(e);
86
      console.log('trackMouse', coord);
Alexandre Delanoë's avatar
Alexandre Delanoë committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
      // optional: make more labels appear on circle hover (/!\ costly /!\ esp. on large graphs)
//      if (partialGraph.conf.moreLabelsUnderArea) {
//        // convert screen => mouse => cam
//        var mouseCoords = (50,50); // sigma.utils.mouseCoords(e)
//        var camCoords = partialGraph.cam.cameraPosition(mouseCoords.x, mouseCoords.y)
//
//        var exactNodeset = circleGetAreaNodes(camCoords.x,camCoords.y)
//        // console.log("nodes under circle:", exactNodeset)
//
//        // we'll use labelThreshold / 3 as the "boosted" cam:size threshold
//        var pfx = partialGraph.cam.readPrefix
//        var toRedraw = []
//        for (var k in exactNodeset) {
//          var n = partialGraph.graph.nodes(exactNodeset[k])
//          if(!n.hidden && n[pfx+'size'] > (partialGraph.customSettings.labelThreshold / 3)) {
//            toRedraw.push(n)
//          }
//        }
//        redrawNodesInHoverLayer(toRedraw, "hovers")
//      }

      // draw the circle itself
      ctx.strokeStyle = '#000';
      ctx.lineWidth = 1;
      ctx.fillStyle = "#71C3FF";
      ctx.globalAlpha = 0.5;
      ctx.beginPath();
114
      ctx.arc(x, y, cursorSize, 0, Math.PI * 2, true);
Alexandre Delanoë's avatar
Alexandre Delanoë committed
115 116 117 118 119
      ctx.closePath();
      ctx.fill();
      ctx.stroke();
      ctx.globalAlpha = 1

120
    }
Alexandre Delanoë's avatar
Alexandre Delanoë committed
121

122 123
};

124 125
exports.sigmaOnMouseMove = function(props) {
  return function(e) {
126 127
    return function() {
      if(typeof(window.sigmaGargInstance) !== "undefined") {
128
          if(props.cursorSize > 0) trackMouse(props.cursorSize, e);
129 130
      }
    };
131
  };
132
};