2017-12-03 16 views
0

最新のコードの試み:Bounded Force Layout(D3 v4)にノードラベルを追加する方法は?

var node = svg.selectAll("circle") 
     .data(graph.nodes) 
    .enter().append("circle") 
     .attr("r", radius - .75) 
     .style("fill", function(d) { return fill(d.group); }) 
     .style("stroke", function(d) { return d3.rgb(fill(d.group)).darker(); }) 
     .call(force.drag); 

    var label = svg.selectAll("text") 
    .data(graph.nodes) 
    .enter() 
    .append("text") 
    .text(function(d){return d.id; }); 
    .style("text-anchor", "middle") 
    .attr("font-family", "sans-serif") 
    .attr("font-size", "11px") 
    .attr("fill", "red"); 
}) 

    force 
     .nodes(graph.nodes) 
     .links(graph.links) 
     .on("tick", tick) 
     .start(); 

    function tick() { 
    node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); }) 
     .attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, 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; }); 

    label.attr("x", function(d){ return d.x; }) 
      .attr("y", function (d) {return d.y }); 

こんにちはみんな、

私はD3に初心者ですし、無駄にこれまでのところ、この問題にほぼ二日間過ごしてきました!

私はコピー/私は見つけることができるすべてのフォーラム応答からコードのスニペットを貼り付けてきたし、それは私のダイアグラム(実行に空の画面)を壊すか、持っているのいずれか https://bl.ocks.org/mbostock/1129492

のバージョンにノードラベルを追加しようとしています

効果はありません(かなり色のついた円がたくさんありますが、ラベルはありません)。私はラベルを別の力図にうまく追加できましたが、それは限界がありませんでした。境界線は私が持っていたい重要な機能です。この段階では、graph.jsonファイルを変更していません。私は単純にノードのテキストラベルを表示しようとしています。

ラベルを表示するためにはどのようなコードを書く必要がありますか、どこに挿入するのか教えてください。私はこの1つをあきらめないことに決めました!

は、ここに私のコードです:

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
circle { 
 
    stroke-width: 1.5px; 
 
} 
 
line { 
 
    stroke: #999; 
 
} 
 
text { 
 
    font: 12px "open sans"; 
 
    fill: #fff; 
 
} 
 

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

 
var width = 960, 
 
    height = 500, 
 
    radius = 6; 
 

 
var fill = d3.scaleOrdinal() 
 
.range(["#a02a65","#2aa02a","#2aa0a0","#a0652a","#a02a2a","#2a2aa0","#65a02a","#a0a02a"]) 
 

 
var simulation = d3.forceSimulation() 
 
    .velocityDecay(0.1) 
 
    .force("x", d3.forceX(width/2).strength(.05)) 
 
    .force("y", d3.forceY(height/2).strength(.05)) 
 
    .force("charge", d3.forceManyBody().strength(-240)) 
 
    .force("link", d3.forceLink().distance(50).strength(1)); 
 

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

 
d3.json("https://[URL HERE]/graph.json", function(error, graph) { 
 
    if (error) throw error; 
 

 
var link = svg.selectAll("line") 
 
     .data(graph.links) 
 
    .enter().append("line"); 
 
    var node = svg.selectAll("circle") 
 
     .data(graph.nodes) 
 
    .enter().append("circle") 
 
     .attr("r", radius - .75) 
 
     .style("fill", function(d) { return fill(d.group); }) 
 
     .style("stroke", function(d) { return d3.rgb(fill(d.group)).darker(); }) 
 
     .call(d3.drag() 
 
      .on("start", dragstarted) 
 
      .on("drag", dragged) 
 
      .on("end", dragended)); 
 
    
 
    var labels = node.append("text") 
 
     .text(function(d) { 
 
     return d.id; 
 
     }) 
 
     .attr('x', 6) 
 
     .attr('y', 3); 
 
    
 
    node.append("title") 
 
     .text(function(d) { return d.id; });simulation 
 
     .nodes(graph.nodes) 
 
     .on("tick", tick); 
 
    simulation.force('link') 
 
     .links(graph.links); 
 
    function tick() { 
 
    node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); }) 
 
     .attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, 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; }); 
 
    } 
 
}); 
 
function dragstarted(d) { 
 
    if (!d3.event.active) simulation.alphaTarget(0.3).restart(); 
 
    d.fx = d.x; 
 
    d.fy = d.y; 
 
} 
 
function dragged(d) { 
 
    d.fx = d3.event.x; 
 
    d.fy = d3.event.y; 
 
} 
 
function dragended(d) { 
 
    if (!d3.event.active) simulation.alphaTarget(0); 
 
    d.fx = null; 
 
    d.fy = null; 
 
} 
 
</script>

+0

変数ノードは、タイトルの円形要素の選択であると思われるが、タイトルは、一般的にレンダリングされません/要素/タイトル)。サークルデータ結合を繰り返すか、テキスト要素を繰り返すか、ノードごとにグループ要素を作成し、円とラベルの両方を追加することができます。 – John

+0

ちょっとジョン!応答してくれてありがとう。それはすばらしいことですが、私はコードワイズに示唆していることをどのように実装するのか考えていません。あなたはコードスニペットで正しい方向に私を指すことができます/ s?再度、感謝します!! – workinitout

+0

この回答を見てください:https://stackoverflow.com/a/37640083/7106086 –

答えて

0

私は以下のようにして、各ノードのラベルを含めるようにexample from Mike Bostockを変更した:各ノードの

  1. ラベルがありますgraph.nodesにあるので、最初に の選択labelsを作成します典型的なd3データ結合を実行してテキスト 要素を追加し、ラベルテキストを設定します。

  2. ラベルの位置は、tick()関数で発生します。 アニメーションの各ステップでは、graph.node要素のxとyの値に従って、 テキスト要素のxy属性を設定しました。

構文にいくつかの変更があるだろうように、この例では、D3 V3を使用していることに注意してください - 規模に使用主名など詳細はhere見つけることができます。

重複を避けるためにテキストを配置しようとはしていません。これは、合理的に関与し、this other SO postのような別の場所で議論されています。 (https://developer.mozilla.org/en-US/docs/Web/SVG

var width = 960, 
 
    height = 500, 
 
    radius = 6; 
 
    
 
var fill = d3.scale.category20(); 
 

 
var force = d3.layout.force() 
 
    .gravity(.05) 
 
    .charge(-240) 
 
    .linkDistance(50) 
 
    .size([width, height]); 
 
    
 
var svg = d3.select("body").append("svg") 
 
    .attr("width", width) 
 
    .attr("height", height); 
 
    
 
d3.json("https://gist.githubusercontent.com/mbostock/1129492/raw/9513c3fe193673a09e161d49d00a587fd806bdf5/graph.json", function(error, graph) { 
 
    if (error) throw error; 
 
    
 
    var link = svg.selectAll("line") 
 
     .data(graph.links) 
 
    .enter().append("line"); 
 
    
 
    var node = svg.selectAll("circle") 
 
     .data(graph.nodes) 
 
    .enter().append("circle") 
 
     .attr("r", radius - .75) 
 
     .style("fill", function(d) { return fill(d.group); }) 
 
     .style("stroke", function(d) { return d3.rgb(fill(d.group)).darker(); }) 
 
     .call(force.drag); 
 
    
 
    var labels = svg.selectAll("text") 
 
     .data(graph.nodes) 
 
    .enter().append('text') 
 
     .text(function (d){return d.name}); 
 
     
 
    force 
 
     .nodes(graph.nodes) 
 
     .links(graph.links) 
 
     .on("tick", tick) 
 
     .start(); 
 
     
 
    function tick() { 
 
    node.attr("cx", function(d) { return d.x = Math.max(radius, Math.min(width - radius, d.x)); }) 
 
     .attr("cy", function(d) { return d.y = Math.max(radius, Math.min(height - radius, 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; }); 
 
    
 
    labels.attr('x', function(d) { return d.x; }) 
 
      .attr('y', function(d) { return d.y; }); 
 
    } 
 
});
circle { 
 
    stroke-width: 1.5px; 
 
} 
 
line { 
 
    stroke: #999; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

+0

Hey John。ありがとうございました!!!私は明日これに固執するつもりです。私はあなたのことを私に知らせます。エイミー。 – workinitout

関連する問題