anvaka / ngraph

Beautiful Graphs
MIT License
1.43k stars 131 forks source link

"Cannot create graph without links and nodes" #17

Closed Robrechtc closed 9 years ago

Robrechtc commented 9 years ago

Hey again! I've been playing with ngraph a bit and I'm currently trying out the fromJSON package. This is the JSON data I'm retrieving:

{
    "nodes": [
        {
            "id": "BBA1"
       },
        {
            "id": "Bank and stock"
        },
        {
            "id": "Economic history"
        },
        {
            "id": "Principles of law"
        },
        {
            "id": "Statistics I"
        },
        {
            "id": "Managerial economics B"
        },
        { 
            "id": "Mathematics I-B"
        },
        {
            "id": "Accounting B"
        }
   ],
    "links": [
        {
            "toId": "Bank and stock",
           "fromId": "BBA1"
        },
        {
            "toId": "Economic history",
            "fromId": "BBA1"
        },
        {
            "toId": "Principles of law",
            "fromId": "BBA1"
        },
        {
            "toId": "Statistics I",
            "fromId": "BBA1"
        },
        {
            "toId": "Managerial economics B",
            "fromId": "BBA1"
        },
       {
            "toId": "Mathematics I-B",
            "fromId": "BBA1"
        },
        {
            "toId": "Accounting B",
            "fromId": "BBA1"
        }
    ]
}

My test.html file is this:

``` Ngraph testing

And this is my index.js: 

module.exports.main = function () { var jsonString = $.ajax({ 'url': '/coursedata', 'datatype': 'json', 'type': 'GET', 'data': { 'path': '-BBA1-Semester 2', } })

var fromJSON = require('ngraph.fromjson')
var graph = fromJSON(jsonString)
var createFabric = require('ngraph.fabric')
var fabricGraphics = createFabric(graph);

fabricGraphics.run();

}



As I am passing my nodes and links, I have no clue what's causing this error :p many thanks in advance for reading and replying! 
anvaka commented 9 years ago

This doesn't look correct:

   var jsonString = $.ajax({
            'url': '/coursedata',
            'datatype': 'json',
            'type': 'GET',
            'data': {
                'path': '-BBA1-Semester 2',
            }
        });

I think you are using jquery to perform ajax request. If so, refer to the documentation to see how to handle ajax response. Should probably be something like

$.ajax({
     'url': '/coursedata',
     'datatype': 'json',
     'type': 'GET',
     'data': {
         'path': '-BBA1-Semester 2',
     }
}).done(function(jsonString) {
 // ...
});