用户移动节点后,如何在Dash / Cytoscape中找到节点的位置?

时间:2019-03-31 19:43:27

标签: cytoscape.js plotly-dash cytoscape

我在Plotly / Dash中使用Cytoscape,并且正在创建一个简单的图形。然后,我将几个节点移动到各个位置。然后,我尝试在Dash回调中读取这些位置-我已经尝试过:

  • State("cytoscape-graph", "stylesheets")
  • State("cytoscape-graph", "layout")
  • State("cytoscape-graph", "elements")
  • 或多或少我能找到的in the docs here

示例节点:

{'data': {'id': 'id0', 'label': 'Node 0'}, 'position': {'x': 50, 'y': 150}},
         {'data': {'id': 'node1', 'label': 'Node 1'}, 'position': {'x': 200, 'y': 100}},
         {'data': {'id': 'node2', 'label': 'Node 2'}, 'position': {'x': 200, 'y': 200}},
         {'data': {'source': 'node0', 'target': 'node1', 'id': '4300352b-9533-4fef-90ec-7a5cbff1f8c4'}},
         {'data': {'source': 'node1', 'target': 'node2', 'id': '33d4c841-bcfc-4a07-ac56-90d36982601a'}},
}

以上返回None,不包含相关信息的其他任何内容或初始节点位置。我尝试查看渲染的源代码,但似乎无法在其中找到信息。仅canvas元素。即使对于Javascript后端,我也希望能有指针。

1 个答案:

答案 0 :(得分:0)

编辑: 似乎Dash不允许直接访问... elements()。jsons(),因此问题的答案是,可能无法纯粹使用Dash进行操作。每次单击节点以在javascript中运行该回调并将其输出到某个隐藏的div,然后在Dash中读取它时,您可能都必须编写一个回调。 〜感谢OP回答这个问题

正如本文档的section所指出的,这很容易实现。方法cy.elements()/nodes()/edges().jsons()获取集合中所有指定元素的普通JavaScript对象表示形式的数组。

var json;
var cy = (window.cy = cytoscape({
  container: document.getElementById("cy"),

  boxSelectionEnabled: false,
  autounselectify: true,

  style: [{
      selector: "node",
      css: {
        content: "data(id)",
        "text-valign": "center",
        "text-halign": "center",
        height: "60px",
        width: "100px",
        shape: "rectangle",
        "background-color": "data(faveColor)"
      }
    },
    {
      selector: "edge",
      css: {
        "curve-style": "bezier",
        "control-point-step-size": 40,
        "target-arrow-shape": "triangle"
      }
    }
  ],

  elements: {
    nodes: [{
        data: {
          id: "Top",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "yes",
          faveColor: "#37a32d"
        }
      },
      {
        data: {
          id: "no",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Third",
          faveColor: "#2763c4"
        }
      },
      {
        data: {
          id: "Fourth",
          faveColor: "#56a9f7"
        }
      }
    ],
    edges: [{
        data: {
          source: "Top",
          target: "yes"
        }
      },
      {
        data: {
          source: "Top",
          target: "no"
        }
      },
      {
        data: {
          source: "no",
          target: "Third"
        }
      },
      {
        data: {
          source: "Third",
          target: "Fourth"
        }
      },
      {
        data: {
          source: "Fourth",
          target: "Third"
        }
      }
    ]
  },
  layout: {
    name: "dagre"
  }
}));
cy.ready(function() {
  console.log('Nodes:', cy.nodes().jsons());
  console.log('Edges:', cy.edges().jsons());
  console.log('Elements:', cy.elements().jsons());
});

document.getElementById('save').addEventListener('click', function() {
  json = cy.json();
});
document.getElementById('load').addEventListener('click', function() {
  cy.json(json);
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 85%;
  width: 100%;
  float: right;
  position: absolute;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://unpkg.com/cytoscape@3.3.0/dist/cytoscape.min.js">
  </script>
  <!-- cyposcape dagre -->
  <script src="https://unpkg.com/dagre@0.7.4/dist/dagre.js"></script>
  <script src="https://cdn.rawgit.com/cytoscape/cytoscape.js-dagre/1.5.0/cytoscape-dagre.js"></script>
</head>

<body>
  <b id="save" style="cursor: pointer; color: darksalmon">Save graph</b> / <b id="load" style="cursor: pointer; color: darkmagenta">Load graph</b>
  <div id="cy"></div>
</body>

</html>