如何动态计算vis.js

时间:2017-01-05 10:18:12

标签: javascript vis.js

我正在与正确的方法斗争如何从查询结果中正确定义图表的数据。

不幸的是,我仍然只在结果中得到一个空心圆圈。

obj = JSON.parse(response);
  $.each(obj.aggregations.system.buckets, function(s, system) {
    if (system.doc_count != 0) {
      var nodes_tmp = [],
      edges_tmp = [],
      firs_id = '',
      prev_id = '',
      last_id = '';                     

      $.each(system.platformSteps.buckets, function(st, step) {
        id = st+1;
        nodes_tmp.push("{id: " + id + ", label: '" + step.key +"'}");

        if (st != 0) {
            edges_tmp.push("{from: " + prev_id + ", to: " + id +"}");
        } else {
            edges_tmp.push("{from: " + id + ", to: '" + id +"'}");
        }

        prev_id = id;
      }); // Loop platform steps

      nodes = new vis.DataSet([nodes_tmp]);
      edges = new vis.DataSet([edges_tmp]),

      var container = document.getElementById('mynetwork');
      var data = {
           nodes: nodes,
           edges: edges
      };
      var options = {};
      var network = new vis.Network(container, data, options);
    }; // If system.doc_count != 0
  }); // Loop systems

当我调试生成的对象时,我得到不同的节点数据结构:

  
      
  • 节点      
        
    • _data      
          
      • 3925b97c-cc43-45f4-8b2c-99773a0960e6      
            
        • 0:“{id:1,label:'AIBFULLTRACE'}”
        •   
        • 1:“{id:2,label:'euCyclone'}”
        •   
        • id:“3925b97c-cc43-45f4-8b2c-99773a0960e6”
        •   
      •   
    •   
  •   

但是在演示中,架构是:

  
      
  • 节点      
        
    • _data      
          
      • 1:对象      
            
        • ID:1
        •   
        • label:“节点1”
        •   
      •   
      • 2:对象      
            
        • ID:2
        •   
        • label:“节点2”
        •   
      •   
    •   
  •   

那里的作业我做错了什么?

谢谢,雷迪

使用eval()和.add()方法修复:

                        var node_tmp = {},
                                edge_tmp = {},
                                node_txt = '',
                                edge_txt = '',
                            firs_id = '',
                                prev_id = '',
                                last_id = '';       
                        var nodes = new vis.DataSet(node_tmp);
                        var edges = new vis.DataSet(edge_tmp);                                                      

                        $.each(system.platformSteps.buckets, function(st, step) {
                            id = st+1;

                            if (st == 0) {
                                node_txt = '({id: ' + id + ', label: \'' + step.key +'\',shape: \'box\', first: true})';
                        } else {
                                node_txt = '({id: ' + id + ', label: \'' + step.key +'\',shape: \'box\'})';
                            }
                            node_tmp = eval(node_txt);
                            nodes.add(node_tmp);

                            if (st != 0) {
                                edge_txt = '({from: ' + prev_id + ', to: ' + id +',arrows:\'to\', label:\'to\'})';
                                edge_tmp = eval(edge_txt);
                                edges.add(edge_tmp);
                            } else {
                                //edges_tmp.push("{from: " + id + ", to: '" + id +"'}");
                            }
                            //console.log(edges_tmp.toString());

                            prev_id = id;
                        }); // Loop platform steps

1 个答案:

答案 0 :(得分:0)

使用eval()和.add()方法修复:

                        var node_tmp = {},
                                edge_tmp = {},
                                node_txt = '',
                                edge_txt = '',
                            firs_id = '',
                                prev_id = '',
                                last_id = '';       
                        var nodes = new vis.DataSet(node_tmp);
                        var edges = new vis.DataSet(edge_tmp);                                                      

                        $.each(system.platformSteps.buckets, function(st, step) {
                            id = st+1;

                            if (st == 0) {
                                node_txt = '({id: ' + id + ', label: \'' + step.key +'\',shape: \'box\', first: true})';
                        } else {
                                node_txt = '({id: ' + id + ', label: \'' + step.key +'\',shape: \'box\'})';
                            }
                            node_tmp = eval(node_txt);
                            nodes.add(node_tmp);

                            if (st != 0) {
                                edge_txt = '({from: ' + prev_id + ', to: ' + id +',arrows:\'to\', label:\'to\'})';
                                edge_tmp = eval(edge_txt);
                                edges.add(edge_tmp);
                            } else {
                                //edges_tmp.push("{from: " + id + ", to: '" + id +"'}");
                            }
                            //console.log(edges_tmp.toString());

                            prev_id = id;
                        }); // Loop platform steps