【问题标题】:dagre d3 make edges from parent to all its childrendagre d3 从父级到所有子级创建边
【发布时间】:2014-11-26 16:52:34
【问题描述】:

我正在使用 dagre d3 来可视化数据,制作有序图表,但我一直在设置边缘。看看我卡住的这段代码

/*data has this template   
  level: type: label
  f.e.: 0: call: label1 //child of root
         1: call: label2 //is child of 0
          2: call: label3 //is child of 1
          2: exit: label4 //is child of 1
         1: exit: label5 //is child of 0, does not have children
        0: exit: label6 //is child of root, does not have children
*/
function makeGraph(data){
    //parsing data here, object from it looks like below saving to array named 'states'
    //object = { id: id, level: par[1], type: par[2], label: par[3] };

    var g = new dagreD3.graphlib.Graph().setGraph({});
    g.setNode("root", { label: " ", style: "fill: #AAAAAA" }); //root node

    states.forEach(function (state) {
        g.setNode(state.id, { label: state.label, style: "fill: " + state.color });
        if (state.level == 0) {
            g.setEdge("root", state.id, { label: state.type });
        } else {
            //I can't find out how to set edges to parent here
        }
    });

   //continuation of function with rendering
}

该图是有序的,并且在顶部有 Root。从根节点我设法制作从根到所有级别为 0 的节点的边。现在我想从所有级别为 0 且有子节点的边制作边,然后从级别为 1 且有子节点的边制作边,依此类推。我已经尝试过类似的方法并将这个结构保存到 C# 中的 json,但我无法将我的 C# 代码重写为 javascript,因为我是 javascript 菜鸟。

【问题讨论】:

    标签: javascript algorithm d3.js dagre-d3


    【解决方案1】:

    为什么不创建一个 DOT 字符串并加载它。

    我为您的对象创建了一个示例:

    http://jsfiddle.net/AydinSakar/sbna95u0/

    在你的 C# 代码中创建这个 DOT 字符串:

    digraph g {
    root [label = "", style= "fill: #AAAAAA"];
    
    label1 [label = "label1", style= "fill: #AAAAAA"]
    root -> label1 [label = "call"]
    
    label2 [label = "label2", style= "fill: #AAAAAA"]
    label1 -> label2 [label = "call"]
    
    label3 [label = "label3", style= "fill: #AAAAAA"]
    label2 -> label3 [label = "call"]
    
    label4 [label = "label4", style= "fill: #AAAAAA"]
    label2 -> label4 [label = "exit"]
    
    label5 [label = "label5", style= "fill: #AAAAAA"]
    label1 -> label5 [label = "exit"]
    
    label6 [label = "label6", style= "fill: #AAAAAA"]
    root -> label6 [label = "exit"]
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多