2016-05-05 4 views
2

は、私はこのようなカップルのデータ配列を持つD3に言うことができます配列とD3力にマッピングノード

var links = [ 
{source: nodes[0], target: nodes[1]}, 
{source: nodes[0], target: nodes[2]}, 
{source: nodes[1], target: nodes[3]} 
] 

をその方法があります番号の代わりに名前を使うことができますか?

var links = [ 
{source: nodes["abc"], target: nodes["def"]}, 
{source: nodes["abc"], target: nodes["ghi"]}, 
{source: nodes["def"], target: nodes["jkl"]} 
] 

が、このエラーを取得する:

キャッチされない例外TypeError:プロパティを読み取ることができません 'X' ここでは、未定義の

の:ここ

link.attr("x1", function(d) { return d.source.x; }) 

は完全なコードです:

このような
<!doctype html> 
<html> 

<head> 

    <title>D3 tutorial</title> 
    <script src="https://d3js.org/d3.v3.min.js"></script> 

</head> 


<body> 

<script> 

var w = 4000, 
    h = 4000; 

var circleWidth = 5; 

var fontFamily = 'Bree Serif', 
    fontSizeHighlight = '1.5em', 
    fontSizeNormal = '1em'; 

var palette = { 
     "lightgray": "#819090", 
     "gray": "#708284", 
     "mediumgray": "#536870", 
     "darkgray": "#475B62", 

     "darkblue": "#0A2933", 
     "darkerblue": "#042029", 

     "paleryellow": "#FCF4DC", 
     "paleyellow": "#EAE3CB", 
     "yellow": "#A57706", 
     "orange": "#BD3613", 
     "red": "#D11C24", 
     "pink": "#C61C6F", 
     "purple": "#595AB7", 
     "blue": "#2176C7", 
     "green": "#259286", 
     "yellowgreen": "#738A05" 
    } 

    var nodes = [ 
    {"name": "abc", "type": "Db"}, 
    {"name": "def", "type": "Db"}, 
    {"name": "ghi", "type": "Db"}, 
    {"name": "jkl", "type": "Db"} 
    ] 

    var links = [ 
    {source: nodes["abc"], target: nodes["def"]}, 
    {source: nodes["abc"], target: nodes["ghi"]}, 
    {source: nodes["def"], target: nodes["jkl"]} 
    ] 



var vis = d3.select("body") 
    .append("svg:svg") 
     .attr("class", "stage") 
     .attr("width", w) 
     .attr("height", h); 

var force = d3.layout.force() 
    .nodes(nodes) 
    .links([]) 
    .gravity(0.1) 
    .charge(-1000) 
    .size([w, h]); 

var link = vis.selectAll(".link") 
     .data(links) 
     .enter().append("line") 
      .attr("class", "link") 
      .attr("stroke", "#CCC") 
      .attr("fill", "none"); 

var node = vis.selectAll("circle.node") 
     .data(nodes) 
     .enter().append("g") 
     .attr("class", "node") 

     //MOUSEOVER 
     .on("mouseover", function(d,i) { 
     if (i>0) { 
      //CIRCLE 
      d3.select(this).selectAll("circle") 
      .transition() 
      .duration(250) 
      .style("cursor", "none")  
      .attr("r", circleWidth+3) 
      .attr("fill",palette.black); 

      //TEXT 
      d3.select(this).select("text") 
      .transition() 
      .style("cursor", "none")  
      .duration(250) 
      .style("cursor", "none")  
      .attr("font-size","1.5em") 
      .attr("x", 15) 
      .attr("y", 5) 
      .text(function(d) { return d.name + "_" + d.type; }) 
     } else { 
      //CIRCLE 
      d3.select(this).selectAll("circle") 
      .style("cursor", "none")  

      //TEXT 
      d3.select(this).select("text") 
      .style("cursor", "none")  
     } 
     }) 

     //MOUSEOUT 
     .on("mouseout", function(d,i) { 
     if (i>0) { 
      //CIRCLE 
      d3.select(this).selectAll("circle") 
      .transition() 
      .duration(250) 
      .attr("r", circleWidth) 
      .attr("fill",palette.pink); 

      //TEXT 
      d3.select(this).select("text") 
      .transition() 
      .duration(250) 
      .attr("font-size","1em") 
      .attr("x", 8) 
      .attr("y", 4) 
      .text(function(d) { return d.name; }) 
     } 
     }) 

     .call(force.drag); 


    //CIRCLE 
    node.append("svg:circle") 
     .attr("cx", function(d) { return d.x; }) 
     .attr("cy", function(d) { return d.y; }) 
     .attr("r", circleWidth) 
     .attr("fill", function(d, i) { if (i>0) { return palette.pink; } else { return palette.black } }) 

    //TEXT 
    node.append("text") 
     .text(function(d, i) { return d.name; }) 
     .attr("x",   function(d, i) { if (i>0) { return circleWidth + 5; } else { return -10 } }) 
     .attr("y",   function(d, i) { if (i>0) { return circleWidth + 0 } else { return 8 } }) 
     .attr("font-family", "Bree Serif") 
     .attr("fill",   function(d, i) { if (i>0) { return palette.black; }  else { return palette.black } }) 
     .attr("font-size", function(d, i) { if (i>0) { return "1em"; }   else { return "1.8em" } }) 
     .attr("text-anchor", function(d, i) { if (i>0) { return "beginning"; }  else { return "end" } }) 



force.on("tick", function(e) { 
    node.attr("transform", function(d, i) {  
     return "translate(" + d.x + "," + d.y + ")"; 
    }); 

    link.attr("x1", function(d) { return d.source.x; }) 
     .attr("y1", function(d) { return d.source.y; }) 
     .attr("x2", function(d) { return d.target.x; }) 
     .attr("y2", function(d) { return d.target.y; }) 
}); 

force.start(); 


    </script> 

</body> 




</html> 
+0

[D3強制レイアウト - インデックスの代わりに名前でノードをリンクする]の重複の可能性があります(http://stackoverflow.com/questions/23986466/d3-force-layout-linking-nodes-by-name-instead-of- index) – thatOneGuy

答えて

0

これは、undefinedを返すので、角括弧内の数字は配列内のオブジェクト(この場合)のインデックスです。 nodes[0]を使用すると、配列nodesの中の最初のオブジェクトを参照しています(インデックスは0から始まり、つまり0が最初のオブジェクト、1が2番目など)。

キーnameの値を持つオブジェクトを参照する場合は、最初に見つけなければなりません。プロパティ/キーとしてそれぞれの名前で、

How to get index of object by its property in javascript

1

メイクノードオブジェクト:この回答を参照してください。

var nodes = { 
    abc: {"name": "abc", "type": "Db"}, 
    def: {"name": "def", "type": "Db"}, 
    ghi: {"name": "ghi", "type": "Db"}, 
    jkl: {"name": "jkl", "type": "Db"} 
} 

配列は、インデックスのみがアクセスすることができ(すなわち配列[3] )のオブジェクトは、プロパティ(すなわち、object["propertyName"]またはobject.propertyNameによってアクセスできます。

+0

残念ながら、 'nodes'は配列でなければならないので、あなたの解決策は動作しません(D3 API:https://github.com/mbostock/d3/wiki/Force-Layoutを読んでください)。 –

関連する問題