2017-08-16 83 views
0

我使用vis.js显示二叉树。我拥有关于数据库的所有信息,并按照我试图将它复制到我的需求而没有结果的文档。使用查询结果中的vis.js

,我使用的文档是:

<style type="text/css"> 
    #mynetwork { 
     width: 600px; 
     height: 400px; 
     border: 1px solid lightgray; 
    } 
</style> </head> <body> <div id="mynetwork"></div> 

//创建节点的数组 变种节点=新vis.DataSet([{ ID:1,标签:'节点1'}, {id:2,label:'Node 2'}, {id:3,label:'Node 3'}, {id:4,label:'Node 4'}, {id:5,label:'Node 5'} ]);

// create an array with edges 
var edges = new vis.DataSet([ 
    {from: 1, to: 3}, 
    {from: 1, to: 2}, 
    {from: 2, to: 4}, 
    {from: 2, to: 5} 
]); 

// create a network 
var container = document.getElementById('mynetwork'); 

// provide the data in the vis format 
var data = { 
    nodes: nodes, 
    edges: edges 
}; 
var options = {}; 

// initialize your network! 
var network = new vis.Network(container, data, options); </script> </body> </html> 

居住的地方工作得很好,但是当我改变,它看起来像这样:

褐VA LA画报

$阿贾克斯({头:{'X-CSRF- TOKEN': $('meta [name =“csrf-token”]')。attr('content')},
url:“obtenerNodos”, data:{id:'1'}, method: 'post', 成功:function(data){ console.log(data); /////////////////////////////////////////

  //console.log(data[0]); 
      //var res = JSON.parse(data); 

      var res = data.split('"*"'); 

      var nd = res[0]; 
      var gd = res[1]; 

      //console.log(nd);/////////////////////////////////////// 
      console.log(nd); 
      var nodes = new vis.DataSet(nd); 
      var edges = new vis.DataSet(gd); 

      // create a network 
      var container = document.getElementById('mynetwork'); 

      // provide the data in the vis format 
      var data = { 
       nodes: nodes, 
       edges: edges 
      }; 

      console.log(nodes);/////////////////////////////////////// 
      //console.log(nd); 

      var options = { 
       nodes:{ 
        borderWidth: 1, 
        borderWidthSelected: 2, 
        brokenImage:'http://binario.com/admin/img/profile1.jpg',    
        color: { 
        border: '#2B7CE9', 
        background: '#97C2FC' 
        }, 

        font: { 
        color: '#343434', 
        size: 14, // px 
        face: 'arial', 
        background: 'none', 
        strokeWidth: 0, // px 
        strokeColor: '#ffffff', 
        align: 'center', 
        multi: false, 
        vadjust: 0, 
        bold: { 
         color: '#343434', 
         size: 14, // px 
         face: 'arial', 
         vadjust: 0, 
         mod: 'bold' 
        }, 
        ital: { 
         color: '#343434', 
         size: 14, // px 
         face: 'arial', 
         vadjust: 0, 
         mod: 'italic', 
        }, 
        boldital: { 
         color: '#343434', 
         size: 14, // px 
         face: 'arial', 
         vadjust: 0, 
         mod: 'bold italic' 
        }, 
        mono: { 
         color: '#343434', 
         size: 15, // px 
         face: 'courier new', 
         vadjust: 2, 
         mod: '' 
        }, 
        },    
        heightConstraint: false, 
        hidden: false,    
        image: 'http://binario.com/admin/img/profile1.jpg', 
        level: undefined, 
        shape: 'circularImage', 

        size: 25, 
        //title: undefined, 
        //value: undefined, 
        widthConstraint: false 
       }, 

       edges:{ 
        arrows: { 
        to:  {enabled: true, scaleFactor:1, type:'arrow'}, 
        middle: {enabled: true, scaleFactor:1, type:'arrow'}, 
        from: {enabled: true, scaleFactor:1, type:'arrow'} 
        }, 
        color: { 
        color:'#161616', 
        highlight:'#848484', 
        hover: '#848484', 
        inherit: 'from', 
        opacity:1.0 
        } 
       }, 

       layout: { 
        randomSeed: undefined, 
        improvedLayout:true, 
        hierarchical: { 
         enabled:true, 
         levelSeparation: 150, 
         nodeSpacing: 100, 
         treeSpacing: 200, 
         blockShifting: true, 
         edgeMinimization: false, 
         parentCentralization: true, 
         direction: 'UD',  // UD, DU, LR, RL 
         sortMethod: 'directed' // hubsize, directed 
        } 
       } 
      }; 

      // initialize your network! 
      var network = new vis.Network(container, data, options); 
      //do something 

     },error: function(xhr, ajaxOptions, thrownError){ 
       console.log(xhr.status+" ,"+" "+ajaxOptions+", "+thrownError); 
      } 

}); 


当控制台告诉我 “的console.log(数据);”,我得到这个:

{ID:1,标签: 'Carlos Corrales'},{id:2,label:'Carlos Jaramillo'},{id:3,label:'Carlos Jaramillo Corra LES'} “*”{由:1, 到:2},{从:1,:3}

当控制台告诉我 “的console.log(ND);”,我得到此:

{ID:1,标签: '卡洛斯科拉莱斯'},{ID:2,标签: '卡洛斯 贾拉米洛'},{ID:3,标签: '卡洛斯贾拉米洛科拉莱斯'}

我很好,但是当控制台显示“控制台。日志(节点); “我得到这个:

N {_options:”{ID:1,标签: '卡洛斯·科拉莱斯'},{ID:2,标签: '... LLO'},{ ID:3,标签:'Carlos Jaramillo Corrales'}“,_data:{...}, 长度:0,_fieldId:”id“,_type:{...},...}长度:0 _data: proto:构造:ƒ对象()hasOwnProperty:ƒhasOwnProperty()isPrototypeOf:ƒisPrototypeOf() propertyIsEnumerable:ƒpropertyIsEnumerable()的toLocaleString:ƒ 的toLocaleString()的toString:ƒ的toString()的valueOf:ƒ的valueOf() defineGetter :ƒdefineGetter() defineSetter:ƒdefineSetter() lookupGetter:ƒlookupGetter() lookupSetter:ƒlookupSetter()得到:ƒ( )set proto:ƒproto() _fieldId:“id” _options:“{id:1,label:'Carlos Corrales'},{id:2,label:'Carlos Jaramillo'},{id:3,label:'Carlos Jaramillo Corrales'}” _subscribers:{add:阵列(1),更新:阵列(1),删除:阵列(1)} _type: :对象 :对象

所以这是说THA节点数组的长度是0.

我用来获取数据的代码是:

公共职能obtenerNodos(请求$要求){

$id = $request->id; 

    $nodos = ""; 
    $edges = ""; 

    $senal = 0; 

    //while ($senal == 0) 
    //{ 
     $res = panelController::getNodos($id); 
     $valores = explode("**",$res); 

     $nodos .= $valores[0]; 

     if($valores[1] != "-99"){ 
      $edges .= $valores[2]; 
      $nodos .= ",".$valores[3]; 
     } 

     if($valores[4] != "-99"){ 
      $edges .= ",".$valores[5]; 
      $nodos .= ",".$valores[6]; 
     } 

     //$senal = 1; 
    //} 

    $nodos .= ""; 
    $edges .= ""; 

    //return $edges; 

    $regreso = $nodos.'"*"'.$edges; 


    return $regreso; 
} 

所以问题是数据不进入节点和恩格斯阵列中,这样,我不能得到的图形。

enter image description here

回答

0

你可以尝试这样的事情弄清楚这个问题,因为我觉得你有问题,你的第二GD格式,你把对数据集

你的目标是要得到这样的对象数组:

Array [Object, Object, ... etc] 
where Object {id: 0, label: "node"} - for example 

您可以直接当你执行查询得到这个格式,并把结果在一个变量

$data = $connection->createCommand("YOUR_SQL")->queryAll(); 

它转换成JSON,然后你可以如隐藏它像一个HTML标签:

<p class="no_display" id="myId"><?= json_encode($data) ?></p> 

把数据给JS变量,如:

var network_nodes = JSON.parse(document.getElementById("myId").innerText); 

现在只需将其分配给DataSet:

var nodes = new vis.DataSet(network_nodes); 

,你说得对。

这总是适合我,希望它有帮助!