Sigma.js выбирает и перетаскивает несколько узлов из проанализированного gexf

Я пытаюсь адаптировать ссылочный пример выбора и перетаскивания узлов, чтобы получить ту же функциональность при загрузке узлов и ребер графа из .gexfфайл. В примере для ссылки выше создается граф со случайными узлами и ребрами, ср. Пример кода ниже:

var i,
    s,
    N = 100,
    E = 500,
    g = {
      nodes: [],
      edges: []
    };

// Generate a random graph:
for (i = 0; i < N; i++)
  g.nodes.push({
    id: 'n' + i,
    label: 'Node ' + i,
    x: Math.random(),
    y: Math.random(),
    size: Math.random(),
    color: '#666'
  });

for (i = 0; i < E; i++)
  g.edges.push({
    id: 'e' + i,
    source: 'n' + (Math.random() * N | 0),
    target: 'n' + (Math.random() * N | 0),
    size: Math.random(),
    color: '#ccc'
  });

Но как мне получить фактические (и количество) узлов и ребер из моего файла gexf? Мой файл gexf minitext.gexf выглядит следующим образом:

<?xml version='1.0' encoding='utf-8'?>
<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
  <graph defaultedgetype="directed" mode="static">
    <nodes>
      <node id="center" label="center" />
      <node id="nhc" label="nhc" />
      <node id="nears" label="nears" />
      <node id="forecast" label="forecast" />
      <node id="maria" label="maria" />
      <node id="category" label="category" />
      <node id="strengthen" label="strengthen" />
      <node id="national" label="national" />
      <node id="monday" label="monday" />
      <node id="says" label="says" />
      <node id="caribbean" label="caribbean" />
      <node id="hurricane" label="hurricane" />
      <node id="leeward" label="leeward" />
      <node id="expected" label="expected" />
      <node id="islands" label="islands" />
      <node id="upgraded" label="upgraded" />
      <node id="hit" label="hit" />
    </nodes>
    <edges>
      <edge id="0" source="center" target="nhc" weight="3.29" />
      <edge id="1" source="center" target="hurricane" weight="0.59" />
      <edge id="2" source="center" target="national" weight="3.29" />
      <edge id="3" source="center" target="says" weight="3.29" />
      <edge id="4" source="nears" target="strengthen" weight="3.29" />
      <edge id="5" source="nears" target="expected" weight="3.29" />
      <edge id="6" source="nears" target="leeward" weight="3.29" />
      <edge id="7" source="nears" target="islands" weight="1.47" />
      <edge id="8" source="forecast" target="category" weight="3.29" />
      <edge id="9" source="forecast" target="islands" weight="1.47" />
      <edge id="10" source="forecast" target="hurricane" weight="0.59" />
      <edge id="11" source="forecast" target="hit" weight="3.29" />
      <edge id="12" source="category" target="caribbean" weight="3.29" />
      <edge id="13" source="category" target="upgraded" weight="3.29" />
      <edge id="14" source="category" target="hurricane" weight="0.59" />
      <edge id="15" source="category" target="forecast" weight="3.29" />
      <edge id="16" source="strengthen" target="leeward" weight="3.29" />
      <edge id="17" source="strengthen" target="expected" weight="3.29" />
      <edge id="18" source="strengthen" target="nears" weight="3.29" />
      <edge id="19" source="strengthen" target="maria" weight="3.29" />
      <edge id="20" source="national" target="center" weight="3.29" />
      <edge id="21" source="national" target="islands" weight="1.47" />
      <edge id="22" source="national" target="hurricane" weight="0.59" />
      <edge id="23" source="national" target="monday" weight="3.29" />
      <edge id="24" source="monday" target="hit" weight="3.29" />
      <edge id="25" source="monday" target="hurricane" weight="0.59" />
      <edge id="26" source="monday" target="national" weight="3.29" />
      <edge id="27" source="monday" target="islands" weight="1.47" />
      <edge id="28" source="caribbean" target="category" weight="3.29" />
      <edge id="29" source="caribbean" target="upgraded" weight="3.29" />
      <edge id="30" source="caribbean" target="leeward" weight="3.29" />
      <edge id="31" source="caribbean" target="islands" weight="1.47" />
      <edge id="32" source="hurricane" target="center" weight="0.59" />
      <edge id="33" source="hurricane" target="category" weight="0.59" />
      <edge id="34" source="hurricane" target="nhc" weight="0.59" />
      <edge id="35" source="hurricane" target="upgraded" weight="0.59" />
      <edge id="36" source="hurricane" target="forecast" weight="0.59" />
      <edge id="37" source="hurricane" target="hit" weight="0.59" />
      <edge id="38" source="hurricane" target="national" weight="0.59" />
      <edge id="39" source="hurricane" target="monday" weight="0.59" />
      <edge id="40" source="leeward" target="caribbean" weight="3.29" />
      <edge id="41" source="leeward" target="strengthen" weight="3.29" />
      <edge id="42" source="leeward" target="nears" weight="3.29" />
      <edge id="43" source="leeward" target="islands" weight="1.47" />
      <edge id="44" source="expected" target="hurricane" weight="0.59" />
      <edge id="45" source="expected" target="nears" weight="3.29" />
      <edge id="46" source="expected" target="maria" weight="3.29" />
      <edge id="47" source="expected" target="strengthen" weight="3.29" />
      <edge id="48" source="islands" target="caribbean" weight="1.47" />
      <edge id="49" source="islands" target="leeward" weight="1.47" />
      <edge id="50" source="islands" target="nears" weight="1.47" />
      <edge id="51" source="islands" target="upgraded" weight="1.47" />
      <edge id="52" source="islands" target="forecast" weight="1.47" />
      <edge id="53" source="islands" target="hit" weight="1.47" />
      <edge id="54" source="islands" target="national" weight="1.47" />
      <edge id="55" source="islands" target="monday" weight="1.47" />
      <edge id="56" source="upgraded" target="category" weight="3.29" />
      <edge id="57" source="upgraded" target="caribbean" weight="3.29" />
      <edge id="58" source="upgraded" target="hurricane" weight="0.59" />
      <edge id="59" source="upgraded" target="islands" weight="1.47" />
      <edge id="60" source="hit" target="monday" weight="3.29" />
      <edge id="61" source="hit" target="forecast" weight="3.29" />
      <edge id="62" source="hit" target="hurricane" weight="0.59" />
      <edge id="63" source="hit" target="islands" weight="1.47" />
    </edges>
  </graph>
</gexf>

Теперь, если я попытаюсь объявить переменную графа g как

var g = sigma.parsers.gexf('data/minitext.gexf',
    {container: 'graph-container'});

и примените к нему алгоритм ForceLink, в моем окне браузера ничего не визуализируется (используя Firefox). Когда я снова использую пример кода со случайно сгенерированным графом, он работает (также с макетом ForeLink). Пару часов я пытался заставить это работать, но все еще ничего не достиг. Я был бы очень признателен за помощь здесь.

Вот мой модифицированный код:

<!-- START SIGMA IMPORTS -->
<script src="../src/sigma.core.js"></script>
<script src="../src/conrad.js"></script>
<script src="../src/utils/sigma.utils.js"></script>
<script src="../src/utils/sigma.polyfills.js"></script>
<script src="../src/sigma.settings.js"></script>
<script src="../src/classes/sigma.classes.dispatcher.js"></script>
<script src="../src/classes/sigma.classes.configurable.js"></script>
<script src="../src/classes/sigma.classes.graph.js"></script>
<script src="../src/classes/sigma.classes.camera.js"></script>
<script src="../src/classes/sigma.classes.quad.js"></script>
<script src="../src/captors/sigma.captors.mouse.js"></script>
<script src="../src/captors/sigma.captors.touch.js"></script>
<script src="../src/renderers/sigma.renderers.canvas.js"></script>
<script src="../src/renderers/sigma.renderers.webgl.js"></script>
<script src="../src/renderers/sigma.renderers.svg.js"></script>
<script src="../src/renderers/sigma.renderers.def.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.nodes.def.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.nodes.fast.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.edges.def.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.edges.fast.js"></script>
<script src="../src/renderers/webgl/sigma.webgl.edges.arrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.labels.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.hovers.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.nodes.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.curve.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.arrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edges.curvedArrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.def.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.curve.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.arrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.edgehovers.curvedArrow.js"></script>
<script src="../src/renderers/canvas/sigma.canvas.extremities.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.utils.js"></script>
<script src="../src/renderers/svg/sigma.svg.nodes.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.edges.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.edges.curve.js"></script>
<script src="../src/renderers/svg/sigma.svg.edges.curvedArrow.js"></script>
<script src="../src/renderers/svg/sigma.svg.labels.def.js"></script>
<script src="../src/renderers/svg/sigma.svg.hovers.def.js"></script>
<script src="../src/middlewares/sigma.middlewares.rescale.js"></script>
<script src="../src/middlewares/sigma.middlewares.copy.js"></script>
<script src="../src/misc/sigma.misc.animation.js"></script>
<script src="../src/misc/sigma.misc.bindEvents.js"></script>
<script src="../src/misc/sigma.misc.bindDOMEvents.js"></script>
<script src="../src/misc/sigma.misc.drawHovers.js"></script>
<!-- END SIGMA IMPORTS -->
<script src="../plugins/sigma.renderers.halo/settings.js"></script>
<script src="../plugins/sigma.renderers.halo/sigma.renderers.halo.js"></script>

<script src="../plugins/sigma.plugins.dragNodes/sigma.plugins.dragNodes.js"></script>

<script src="../plugins/sigma.helpers.graph/sigma.helpers.graph.js"></script>
<script src="../plugins/sigma.plugins.keyboard/sigma.plugins.keyboard.js"></script>
<script src="../plugins/sigma.plugins.activeState/sigma.plugins.activeState.js"></script>
<script src="../plugins/sigma.plugins.select/sigma.plugins.select.js"></script>
<script src="../plugins/sigma.plugins.lasso/sigma.plugins.lasso.js"></script>

<script src="../plugins/sigma.renderers.linkurious/settings.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.labels.def.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.hovers.def.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.nodes.def.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.nodes.cross.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.nodes.diamond.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.nodes.equilateral.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.nodes.square.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.nodes.star.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.edges.def.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.edges.curve.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.edges.arrow.js"></script>
<script src="../plugins/sigma.renderers.linkurious/canvas/sigma.canvas.edges.curvedArrow.js"></script>
<script src="../plugins/sigma.parsers.gexf/gexf-parser.js"></script>
<script src="../plugins/sigma.parsers.gexf/sigma.parsers.gexf.js"></script>
<script src="../plugins/sigma.plugins.animate/sigma.plugins.animate.js"></script>
<script src="../plugins/sigma.layouts.forceLink/worker.js"></script>
<script src="../plugins/sigma.layouts.forceLink/supervisor.js"></script>
<div id="container">
  <style>
    #graph-container {
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      position: absolute;
    }


  kbd {
    background-color: #e7e7e7;
    background-image: -webkit-linear-gradient(#fefefe, #e7e7e7);
    background-image: linear-gradient(#fefefe, #e7e7e7);
    background-repeat: repeat-x;
    display: inline-block;
    padding: 3px 5px;
    font: 11px Consolas, "Liberation Mono", Menlo, Courier, monospace;
    line-height: 10px;
    color: #000;
    border: 1px solid #cfcfcf;
    border-radius: 2px;
  }

  .key.arrow {
     font-size: 15px;
     font-weight: bold
  }

  .container {
    position: absolute;
    bottom: 0;
    right: 0;
    line-height: 30px;
    border: 1px solid #ccc;
    padding: 10px 20px;
    background: white;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 13px;
  }
  .left {
    display: inline-block;
    width: 100px
  }
  </style>
  <div id="graph-container"></div>
  <div class="container">
    <div><span class="left"><kbd>spacebar</kbd> + <kbd>s</kbd></span> Enable lasso tool</div>
  </div>
</div>
<script>
/**
 * This example shows how to use the dragNodes plugin.
 */


/*var i,
    s,
    N = 100,
    E = 500,
    g = {
      nodes: [],
      edges: []
    };


// Generate a random graph:
for (i = 0; i < N; i++)
  g.nodes.push({
    id: 'n' + i,
    label: 'Node ' + i,
    x: Math.random(),
    y: Math.random(),
    size: Math.random(),
    color: '#666'
  });

for (i = 0; i < E; i++)
  g.edges.push({
    id: 'e' + i,
    source: 'n' + (Math.random() * N | 0),
    target: 'n' + (Math.random() * N | 0),
    size: Math.random(),
    color: '#ccc'
  });*/


var g = sigma.parsers.gexf('data/minitext.gexf',
    {container: 'graph-container'});


sigma.renderers.def = sigma.renderers.canvas;

s = new sigma({
  graph: g,
  renderer: {
    container: document.getElementById('graph-container'),
    type: 'canvas'
  },
  settings: {
    enableEdgeHovering: false,
    nodeActiveBorderSize: 2,
    nodeActiveOuterBorderSize: 3,
    defaultNodeActiveBorderColor: '#fff',
    defaultNodeActiveOuterBorderColor: 'rgb(236, 81, 72)',
    nodeHaloColor: 'rgba(236, 81, 72, 0.2)',
    nodeHaloSize: 50,
  }
});

/*s = refresh();*/


// Instanciate the ActiveState plugin:
var activeState = sigma.plugins.activeState(s);
var keyboard = sigma.plugins.keyboard(s, s.renderers[0]);

// Initialize the Select plugin:
var select = sigma.plugins.select(s, activeState);
select.bindKeyboard(keyboard);

// Initialize the dragNodes plugin:
var dragListener = sigma.plugins.dragNodes(s, s.renderers[0], activeState);

// Initialize the lasso plugin:
var lasso = new sigma.plugins.lasso(s, s.renderers[0], {
  'strokeStyle': 'rgb(236, 81, 72)',
  'lineWidth': 2,
  'fillWhileDrawing': true,
  'fillStyle': 'rgba(236, 81, 72, 0.2)',
  'cursor': 'crosshair'
});

select.bindLasso(lasso);
lasso.activate();

// halo on active nodes:
function renderHalo() {
  s.renderers[0].halo({
    nodes: activeState.nodes()
  });
}

s.renderers[0].bind('render', function(e) {
  renderHalo();
});


//"spacebar" + "s" keys pressed binding for the lasso tool
keyboard.bind('32+83', function() {
  if (lasso.isActive) {
    lasso.deactivate();
  } else {
    lasso.activate();
  }
});


// Listen for selectedNodes event
lasso.bind('selectedNodes', function (event) {
  setTimeout(function() {
    lasso.deactivate();
    s.refresh({ skipIdexation: true });
  }, 0);
});


// Configure the ForceLink algorithm:
var fa = sigma.layouts.configForceLink(s, {
  worker: true,
  autoStop: true,
  background: true,
  easing: 'cubicInOut'
});

// Bind the events:
fa.bind('start stop', function(e) {
  console.log(e.type);
});

// Start the ForceLink algorithm:
sigma.layouts.startForceLink();

</script>

0 ответов

Другие вопросы по тегам