Cytsocape.js can't create edges in for loop












0















I'm creating chart with nodes and edges. Once I created nodes, I can't create the associated edges without getting those kind of errors :




Can not create edge 5134fb65-b30f-4947-9870-cc909e293e21 with nonexistant source Peter




My code :



var myJSONdata = info;
var cy = window.cy = cytoscape({
container: document.getElementById('cy'),

boxSelectionEnabled: false,
autounselectify: true,

style: [
{
selector: 'node',
style: {
'content': 'data(id)',
'text-opacity': 0.5,
'text-valign': 'center',
'text-halign': 'right',
'shape': 'hexagon',
'label': 'data(label)',
'background-color': '#11479e'
}
},
{
selector: 'edge',
style: {
'curve-style': 'bezier',
'width': 4,
'target-arrow-shape': 'triangle',
'line-color': '#9dbaea',
'target-arrow-color': '#9dbaea'
}
}
],

// elements: {
// nodes: [
// { data: { id: 'Peter' } },
// { data: { id: 'Claire' } },
// { data: { id: 'Mike' } },
// { data: { id: 'Rosa' } }
// ],
// edges: [
// { data: { source: 'Peter', target: 'Claire' } },
// { data: { source: 'Claire', target: 'Mike' } },
// { data: { source: 'Mike', target: 'Rosa' } }
// ]
// }
});

var array = ;
// Create nodes
for (var i = 0; i <= myJSONdata.length - 1; i++) {
array.push({
group: 'nodes',
data: {
id: i,
label: myJSONdata[i].name
}
}
);
}
// Create edges
for (var i = 0; i <= myJSONdata.length - 1; i++) {
var source = myJSONdata[i].name;
array.push({
group: 'edges',
data: {
source: source,
target: myJSONdata[i].next_op_name
}
});
}

cy.add(array);

cy.layout({
name: 'circle'
}).run();


The "Create nodes" part is working, but the "Create edges" is not.



I tried the solution here but it does not work.



Actually I want to read data from JSON file to create the chart.
I can do it with :



 elements: {
nodes: [
{ data: { id: 'Peter' } },
{ data: { id: 'Claire' } },
{ data: { id: 'Mike' } },
{ data: { id: 'Rosa' } }
],
edges: [
{ data: { source: 'Peter', target: 'Claire' } },
{ data: { source: 'Claire', target: 'Mike' } },
{ data: { source: 'Mike', target: 'Rosa' } }
]
}


But I want to automate it according to the JSON file in input.



This is my JSON file :



info = [
{
"name": "Peter",
"next_op_name": "Claire",
}, {
"name": "Claire",
"next_op_name": "Mike",
}, {
"name": "Mike",
"next_op_name": "Rosa",
}, {
"name": "Rosa",
"next_op_name": "Peter",
}
];


I can't understand what is wrong.










share|improve this question



























    0















    I'm creating chart with nodes and edges. Once I created nodes, I can't create the associated edges without getting those kind of errors :




    Can not create edge 5134fb65-b30f-4947-9870-cc909e293e21 with nonexistant source Peter




    My code :



    var myJSONdata = info;
    var cy = window.cy = cytoscape({
    container: document.getElementById('cy'),

    boxSelectionEnabled: false,
    autounselectify: true,

    style: [
    {
    selector: 'node',
    style: {
    'content': 'data(id)',
    'text-opacity': 0.5,
    'text-valign': 'center',
    'text-halign': 'right',
    'shape': 'hexagon',
    'label': 'data(label)',
    'background-color': '#11479e'
    }
    },
    {
    selector: 'edge',
    style: {
    'curve-style': 'bezier',
    'width': 4,
    'target-arrow-shape': 'triangle',
    'line-color': '#9dbaea',
    'target-arrow-color': '#9dbaea'
    }
    }
    ],

    // elements: {
    // nodes: [
    // { data: { id: 'Peter' } },
    // { data: { id: 'Claire' } },
    // { data: { id: 'Mike' } },
    // { data: { id: 'Rosa' } }
    // ],
    // edges: [
    // { data: { source: 'Peter', target: 'Claire' } },
    // { data: { source: 'Claire', target: 'Mike' } },
    // { data: { source: 'Mike', target: 'Rosa' } }
    // ]
    // }
    });

    var array = ;
    // Create nodes
    for (var i = 0; i <= myJSONdata.length - 1; i++) {
    array.push({
    group: 'nodes',
    data: {
    id: i,
    label: myJSONdata[i].name
    }
    }
    );
    }
    // Create edges
    for (var i = 0; i <= myJSONdata.length - 1; i++) {
    var source = myJSONdata[i].name;
    array.push({
    group: 'edges',
    data: {
    source: source,
    target: myJSONdata[i].next_op_name
    }
    });
    }

    cy.add(array);

    cy.layout({
    name: 'circle'
    }).run();


    The "Create nodes" part is working, but the "Create edges" is not.



    I tried the solution here but it does not work.



    Actually I want to read data from JSON file to create the chart.
    I can do it with :



     elements: {
    nodes: [
    { data: { id: 'Peter' } },
    { data: { id: 'Claire' } },
    { data: { id: 'Mike' } },
    { data: { id: 'Rosa' } }
    ],
    edges: [
    { data: { source: 'Peter', target: 'Claire' } },
    { data: { source: 'Claire', target: 'Mike' } },
    { data: { source: 'Mike', target: 'Rosa' } }
    ]
    }


    But I want to automate it according to the JSON file in input.



    This is my JSON file :



    info = [
    {
    "name": "Peter",
    "next_op_name": "Claire",
    }, {
    "name": "Claire",
    "next_op_name": "Mike",
    }, {
    "name": "Mike",
    "next_op_name": "Rosa",
    }, {
    "name": "Rosa",
    "next_op_name": "Peter",
    }
    ];


    I can't understand what is wrong.










    share|improve this question

























      0












      0








      0


      0






      I'm creating chart with nodes and edges. Once I created nodes, I can't create the associated edges without getting those kind of errors :




      Can not create edge 5134fb65-b30f-4947-9870-cc909e293e21 with nonexistant source Peter




      My code :



      var myJSONdata = info;
      var cy = window.cy = cytoscape({
      container: document.getElementById('cy'),

      boxSelectionEnabled: false,
      autounselectify: true,

      style: [
      {
      selector: 'node',
      style: {
      'content': 'data(id)',
      'text-opacity': 0.5,
      'text-valign': 'center',
      'text-halign': 'right',
      'shape': 'hexagon',
      'label': 'data(label)',
      'background-color': '#11479e'
      }
      },
      {
      selector: 'edge',
      style: {
      'curve-style': 'bezier',
      'width': 4,
      'target-arrow-shape': 'triangle',
      'line-color': '#9dbaea',
      'target-arrow-color': '#9dbaea'
      }
      }
      ],

      // elements: {
      // nodes: [
      // { data: { id: 'Peter' } },
      // { data: { id: 'Claire' } },
      // { data: { id: 'Mike' } },
      // { data: { id: 'Rosa' } }
      // ],
      // edges: [
      // { data: { source: 'Peter', target: 'Claire' } },
      // { data: { source: 'Claire', target: 'Mike' } },
      // { data: { source: 'Mike', target: 'Rosa' } }
      // ]
      // }
      });

      var array = ;
      // Create nodes
      for (var i = 0; i <= myJSONdata.length - 1; i++) {
      array.push({
      group: 'nodes',
      data: {
      id: i,
      label: myJSONdata[i].name
      }
      }
      );
      }
      // Create edges
      for (var i = 0; i <= myJSONdata.length - 1; i++) {
      var source = myJSONdata[i].name;
      array.push({
      group: 'edges',
      data: {
      source: source,
      target: myJSONdata[i].next_op_name
      }
      });
      }

      cy.add(array);

      cy.layout({
      name: 'circle'
      }).run();


      The "Create nodes" part is working, but the "Create edges" is not.



      I tried the solution here but it does not work.



      Actually I want to read data from JSON file to create the chart.
      I can do it with :



       elements: {
      nodes: [
      { data: { id: 'Peter' } },
      { data: { id: 'Claire' } },
      { data: { id: 'Mike' } },
      { data: { id: 'Rosa' } }
      ],
      edges: [
      { data: { source: 'Peter', target: 'Claire' } },
      { data: { source: 'Claire', target: 'Mike' } },
      { data: { source: 'Mike', target: 'Rosa' } }
      ]
      }


      But I want to automate it according to the JSON file in input.



      This is my JSON file :



      info = [
      {
      "name": "Peter",
      "next_op_name": "Claire",
      }, {
      "name": "Claire",
      "next_op_name": "Mike",
      }, {
      "name": "Mike",
      "next_op_name": "Rosa",
      }, {
      "name": "Rosa",
      "next_op_name": "Peter",
      }
      ];


      I can't understand what is wrong.










      share|improve this question














      I'm creating chart with nodes and edges. Once I created nodes, I can't create the associated edges without getting those kind of errors :




      Can not create edge 5134fb65-b30f-4947-9870-cc909e293e21 with nonexistant source Peter




      My code :



      var myJSONdata = info;
      var cy = window.cy = cytoscape({
      container: document.getElementById('cy'),

      boxSelectionEnabled: false,
      autounselectify: true,

      style: [
      {
      selector: 'node',
      style: {
      'content': 'data(id)',
      'text-opacity': 0.5,
      'text-valign': 'center',
      'text-halign': 'right',
      'shape': 'hexagon',
      'label': 'data(label)',
      'background-color': '#11479e'
      }
      },
      {
      selector: 'edge',
      style: {
      'curve-style': 'bezier',
      'width': 4,
      'target-arrow-shape': 'triangle',
      'line-color': '#9dbaea',
      'target-arrow-color': '#9dbaea'
      }
      }
      ],

      // elements: {
      // nodes: [
      // { data: { id: 'Peter' } },
      // { data: { id: 'Claire' } },
      // { data: { id: 'Mike' } },
      // { data: { id: 'Rosa' } }
      // ],
      // edges: [
      // { data: { source: 'Peter', target: 'Claire' } },
      // { data: { source: 'Claire', target: 'Mike' } },
      // { data: { source: 'Mike', target: 'Rosa' } }
      // ]
      // }
      });

      var array = ;
      // Create nodes
      for (var i = 0; i <= myJSONdata.length - 1; i++) {
      array.push({
      group: 'nodes',
      data: {
      id: i,
      label: myJSONdata[i].name
      }
      }
      );
      }
      // Create edges
      for (var i = 0; i <= myJSONdata.length - 1; i++) {
      var source = myJSONdata[i].name;
      array.push({
      group: 'edges',
      data: {
      source: source,
      target: myJSONdata[i].next_op_name
      }
      });
      }

      cy.add(array);

      cy.layout({
      name: 'circle'
      }).run();


      The "Create nodes" part is working, but the "Create edges" is not.



      I tried the solution here but it does not work.



      Actually I want to read data from JSON file to create the chart.
      I can do it with :



       elements: {
      nodes: [
      { data: { id: 'Peter' } },
      { data: { id: 'Claire' } },
      { data: { id: 'Mike' } },
      { data: { id: 'Rosa' } }
      ],
      edges: [
      { data: { source: 'Peter', target: 'Claire' } },
      { data: { source: 'Claire', target: 'Mike' } },
      { data: { source: 'Mike', target: 'Rosa' } }
      ]
      }


      But I want to automate it according to the JSON file in input.



      This is my JSON file :



      info = [
      {
      "name": "Peter",
      "next_op_name": "Claire",
      }, {
      "name": "Claire",
      "next_op_name": "Mike",
      }, {
      "name": "Mike",
      "next_op_name": "Rosa",
      }, {
      "name": "Rosa",
      "next_op_name": "Peter",
      }
      ];


      I can't understand what is wrong.







      javascript cytoscape.js cytoscape






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 14:06









      MajesticMajestic

      3821419




      3821419
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The source and target fields in the edge are the IDs of nodes, not labels.



          When you create the nodes, you need to set id to myJSONdata[i].name as well.






          share|improve this answer
























          • You're right ! Thanks a lot :)

            – Majestic
            Nov 16 '18 at 7:52











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321251%2fcytsocape-js-cant-create-edges-in-for-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The source and target fields in the edge are the IDs of nodes, not labels.



          When you create the nodes, you need to set id to myJSONdata[i].name as well.






          share|improve this answer
























          • You're right ! Thanks a lot :)

            – Majestic
            Nov 16 '18 at 7:52
















          1














          The source and target fields in the edge are the IDs of nodes, not labels.



          When you create the nodes, you need to set id to myJSONdata[i].name as well.






          share|improve this answer
























          • You're right ! Thanks a lot :)

            – Majestic
            Nov 16 '18 at 7:52














          1












          1








          1







          The source and target fields in the edge are the IDs of nodes, not labels.



          When you create the nodes, you need to set id to myJSONdata[i].name as well.






          share|improve this answer













          The source and target fields in the edge are the IDs of nodes, not labels.



          When you create the nodes, you need to set id to myJSONdata[i].name as well.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 19:16









          user3140972user3140972

          50729




          50729













          • You're right ! Thanks a lot :)

            – Majestic
            Nov 16 '18 at 7:52



















          • You're right ! Thanks a lot :)

            – Majestic
            Nov 16 '18 at 7:52

















          You're right ! Thanks a lot :)

          – Majestic
          Nov 16 '18 at 7:52





          You're right ! Thanks a lot :)

          – Majestic
          Nov 16 '18 at 7:52




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321251%2fcytsocape-js-cant-create-edges-in-for-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python