简体   繁体   中英

How can I create a new graph using output of an API (or the elements of another graph)?

I have a graph that I create an MST from using the Kruskal API. I'd like to visualise that MST but I don't see any immediately obvious way of doing so.

let originalGraph = cytoscape({
  ...
  elements: jsonData
});

let mst = originalGraph.elements().kruskal(e => e.data().weight);

let newGraph = cytoscape({
  ...
  elements: ???
});

How can I extract the elements from the MST (or indeed any other graph like originalGraph ) such that I can pass it into a new cytoscape instance to visualise? Currently my workaround is:

let newGraph = cytoscape({
  ...
  elements: mst.map(e => { return { position: e.position(), data: e.data() } })
});

But I can't help but feel like I'm missing a much easier way. On a potentially related note, I saw that the docs say that .data() will refer to just the first element of a collection, but is there a way to get a reference to the data the same way it was passed in (like jsonData above) without resorting to .map(...) ?

My apologies if the answer is obvious; I've only just started using Cytoscape.js.

I went through the documentation in more detail again and I figured it out. The format for jsonData is literally called the "plain JSON representation" in the docs and can be gotten with the .jsons() function, and passed to a new graph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM