2014-03-18 4 views
0

내가는 정의에 의해 노드를 연결하는 데이터 형식을 수정 mbostock의 Mobile Patent SuitD3 : 내가 대신 인덱스

떨어져 있어요 정의 된 키를 사용하여 노드를 연결하는 코드를 수정하면 링크 힘 레이아웃 그래프에 표시되지 않습니다 키 대신 (이름 및 유형) 색인을 사용하십시오. 진드기 기능을 수정하고 및 및 추가, 시도했지만 아무것도 작동하는 것.

여기에 어떤 통찰력을 크게 감상 할 수 JsFiddle

<!DOCTYPE html> 
<meta charset="utf-8"> 
<style> 

.node circle { 
    /*stroke: #fff;*/ 
    cursor: pointer; 
    stroke: #3182bd; 
    stroke-width: 1.5px; 
} 

.node text { 
    font: 10px sans-serif; 
    pointer-events: none; 
    text-anchor: middle; 
} 

.link { 
    stroke-opacity: .6; 
    stroke-width: 1.5px; 
    stroke: #666; 
    fill: none; 
} 

.link.deliverFor { 
    stroke: green; 
} 

.link.sentTo { 
    stroke-dasharray: 0,2 1; 
} 

/* color of nodes based on type */ 
circle.emailAddress { 
    fill: blue; 
} 

circle.ip { 
    fill: red; 
} 

circle.missing { 
    fill: "#3182bd"; 
} 

</style> 
<body> 
<script src="http://d3js.org/d3.v3.min.js"></script> 
<script> 

var width = 960, 
    height = 500; 

var color = d3.scale.category20(); 

var linkDistanceScale = d3.scale.linear() 
     .domain([10, 5000]) 
     .range([2, 300]) 
     .clamp(true); 

var force = d3.layout.force() 
    .linkDistance(30) 
    .charge(-120) 
    .size([width, height]); 

var svg = d3.select("body") 
    .append("svg") 
    .attr("width", width) 
    .attr("height", height); 

var graph = { 
    "nodes": [ 
     { 
      "name": "[email protected]", 
      "type": "emailAddress", 
      "size": 1234 
     }, 
     { 
      "name": "192.168.0.1", 
      "type": "ip", 
      "size": 870 
     }, 
     { 
      "name": "[email protected]", 
      "type": "emailAddress", 
      "size": 3423 
     } 
    ], 
    "undirectedNeighbors": [ 
     { 
      "label": "deliverFor", 
      "left": { 
       "name": "[email protected]", 
       "type": "emailAddress" 
      }, 
      "right": { 
       "name": "192.168.0.1", 
       "type": "ip" 
      }, 
      "weight": 366 
     }, 
     { 
      "left": { 
       "type": "emailAddress", 
       "name": "[email protected]" 
      }, 
      "right": { 
       "type": "emailAddress", 
       "name": "[email protected]" 
      }, 
      "label": "sentTo", 
      "weight": 777 
     } 
    ] 
}; 

    var my_nodes = graph.nodes.slice(), 
     my_neighbors = graph.undirectedNeighbors.slice(); 

    console.log("my_neighbors", my_neighbors); 

    var hash_lookup = []; 

    // make it so we can lookup nodes by entry (name: x, type, y) in O(1): 
    my_nodes.forEach(function(node) { 
     var key = {name: node.name, type: node.type}; 
     hash_lookup[key] = node; 
    }); 

    // tell d3 where to find nodes info 
    my_neighbors.forEach(function(link) { 

     var left = link.left; 
     var right = link.right; 

     var leftKey = {name: left.name, type: left.type}; 
     var rightKey = {name: right.name, type: right.type}; 

     link.source = hash_lookup[leftKey]; 
     link.target = hash_lookup[rightKey]; 
    }); 

    console.log("my_neighbors", my_neighbors); 
    console.log("my_nodes", my_nodes); 

    console.log("neighobrs", graph.undirectedNeighbors); 

/************** SETUP FORCE LAYOUT ****************/ 

    force 
     .nodes(my_nodes) 
     .links(my_neighbors) 
     .linkDistance(function(d) { 
      console.log("linkDistance", d); 
      return linkDistanceScale(d.weight); 
     }) 
     .start(); 

console.log("force.links()", force.links()); 
/************** DRAW PATHS ****************/ 

    var link = svg 
     .append("svg:g") // the "curves" is drawn inside this container 
     .selectAll("path") 
     .data(force.links()) 
     .enter() 
     .append("svg:path") 
     // .attr("class", "link") 
     .attr("class", function(d) { 
      return "link " + d.type; 
     }); 


    /************** DRAW CIRCLES ****************/ 
    var node = svg 
     .selectAll(".node") 
     .data(force.nodes()); 

    var circle = node 
     .enter() 
     .append("circle") 
     .attr("class", "node") 
     .attr("r", function(d) { 
      console.log("circle d", d); 
      return Math.sqrt(d.size)/10 || 4.5; 
     }) 
     .attr("class", function(d) { 
      return d.type || "missing";  // color circle based on circl.* defined above 
     }) 
     .call(force.drag); 

    /************** DISPLAY NAME WHEN CURVER HOVER OVER CIRCLE ****************/ 
    var text = circle.append("title") 
     .text(function(d) { return d.name; }); 

    force.on("tick", function() { 
     link.attr("d", function(d) { 
     var dx = d.target.x - d.source.x, 
      dy = d.target.y - d.source.y, 
      dr = Math.sqrt(dx * dx + dy * dy); 

     return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y; 

     }); 

     node 
     .attr("transform", function(d) { 
      return "translate(" + d.x + "," + d.y + ")"; }); 
    }); 


</script> 

에 내 코드입니다.

답변

0

이 방법을 사용하는 것이 좋은지 여부를 떠나서 개체의 키 (hash_lookup)로 개체를 사용하려고하는 것이 문제입니다. JavaScript에서 작동하는 것은 아닙니다. 지도 키를 사용하기 전에지도 키를 문자열로 변환하기 만하면됩니다.

var hash_lookup = {}; 

// make it so we can lookup nodes by entry (name: x, type, y) in O(1): 
my_nodes.forEach(function(node) { 
    var key = JSON.stringify({name: node.name, type: node.type}); 
    hash_lookup[key] = node; 
}); 

// tell d3 where to find nodes info 
my_neighbors.forEach(function(link) { 

    var left = link.left; 
    var right = link.right; 

    var leftKey = JSON.stringify({name: left.name, type: left.type}); 
    var rightKey = JSON.stringify({name: right.name, type: right.type}); 

    link.source = hash_lookup[leftKey]; 
    link.target = hash_lookup[rightKey]; 
}); 
관련 문제