D3 Javascript-仅在强制布局中显示一个链接

D3 Javascript-仅在强制布局中显示一个链接,javascript,svg,d3.js,Javascript,Svg,D3.js,我正在尝试创建一些东西,允许用户滚动浏览一系列不同的链接。节点保持不变,但当用户单击以查看下一个节点时,链接会发生变化 但是,它在每个数组中只显示一个链接。有些阵列只有一个链接,所以这没关系,但是需要正确显示具有多个链接的阵列 <script src="http://d3js.org/d3.v3.min.js"></script> <script> //var links = <?php echo json_encode(array_slice(

我正在尝试创建一些东西,允许用户滚动浏览一系列不同的链接。节点保持不变,但当用户单击以查看下一个节点时,链接会发生变化

但是,它在每个数组中只显示一个链接。有些阵列只有一个链接,所以这没关系,但是需要正确显示具有多个链接的阵列

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

  //var links =  <?php echo json_encode(array_slice($data["links"], 0, 1)); ?>;
  var links =  <?php echo json_encode($links); ?>;
  var nodes =  <?php echo json_encode($nodes); ?>;
  var count = <?php echo count($data); ?>;
  var data_set = <?php echo json_encode($data); ?>;
  //console.log(data_set);

  //document.getElementById("demo").innerHTML = links;
  max_count = links.length;

function draw()
{

  function update_clique_view(direction)
  {
    geo_long_12_index += direction; 
    if(geo_long_12_index > max_count)
      geo_long_12_index = 0;
    else if(geo_long_12_index < 0)
      geo_long_12_index = max_count - 1;
    current_clique = links[geo_long_12_index]; 
    d3.select("#link_number").remove();
    svg.append("text")
      .attr("id", "link_number")
      .attr("x", width/2)
      .attr("y", 20)
      .attr("dy", ".35em")
      .attr("opacity", 0.7)
      .style("fill", "#FFFFFF")
      .text(geo_long_12_index);
    console.log(current_clique);
    link.data(current_clique);
    force.links(current_clique);
    force.start();
  }

  var width = 800,
      height = 600;

  geo_long_12_index = 0;
  if(links.length > 0)
  {
    current_clique = links[geo_long_12_index]; 
  }

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

  svg.append("rect")
      .attr("width", "100%")
      .attr("height", "100%")
      .attr("rx", "20")
      .attr("ry", "20")
      .style("fill", "#000022")
      .attr("class", "background");

  svg.append("text")
    .attr("id", "link_number")
    .attr("x", width/2)
    .attr("y", 20)
    .attr("dy", ".35em")
    .attr("opacity", 0.7)
    .style("fill", "#FFFFFF")
    .text(geo_long_12_index);

  // Navigation arrows
  var leftarrow = svg.append("svg:image")
      .attr("xlink:href", "/cgi-bin/images/left_arrow.svg")
      .attr("width", 20)
      .attr("height", 20)
      .attr("x", (width / 2) - 25)
      .attr("y",20)
      .attr("opacity", 0.7)
      .on("click", function(d,i) {update_clique_view(-1);})
      .on("mouseover", function(d) {d3.select(this).attr("opacity", 1);})             
      .on("mouseout", function(d) {d3.select(this).attr("opacity", 0.7);});
  var rightarrow = svg.append("svg:image")
      .attr("xlink:href","/cgi-bin/images/right_arrow.svg")
      .attr("width", 20)
      .attr("height", 20)
      .attr("x", (width / 2) + 35)
      .attr("y",20)
      .attr("opacity", 0.7)
      .on("click", function(d,i) {update_clique_view(1);})
      .on("mouseover", function(d) {d3.select(this).attr("opacity", 1);})             
      .on("mouseout", function(d) {d3.select(this).attr("opacity", 0.7);});

  var position_label = svg.append("text")
                  .attr("x", 10)
                  .attr("y", 30);


  var force = d3.layout.force()
      .gravity(.05)
      .distance(100)
      .charge(-100)
      .size([width, height]);


  force
      .nodes(nodes)
      .links(current_clique)
      .start();

  var link = svg.selectAll(".link")
      .data(current_clique)
      .enter().append("line")
      .style("stroke", function(d) { return d.colour; })
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(nodes)
      .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("circle")
      .attr("r", 5)
      .style("fill", function(d) { return d.colour; })
      .attr("class", "planet")
      .on("mouseover", function(d) {d3.select(this).attr("r", 10);})                  
      .on("mouseout", function(d) {d3.select(this).attr("r", 5);});

  node.append("text")
      .attr("dx", 12)
      .attr("dy", ".35em")
      .text(function(d) { return d.name });

  force.on("tick", function() {
    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; });

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



  });
}
  draw();


</script>
var links=[{来源:17,目标:22,目标:0.058311111109,颜色:灰色}],{来源:20,目标:21,目标:0.2079,颜色:灰色}],{来源:12,目标:15,目标:0.70274999998,颜色:灰色}],{来源:3,目标:21,目标:1.0416277777778,颜色:灰色}],{来源:8,目标:12,目标:0.463227777775,颜色:灰色},{来源:23,目标:2390,灰色},{source:12,target:15,orb:0.702749999998,color:grey}],{source:0,target:17,orb:0.71342777777778,color:grey},{source:0,target:22,orb:0.7717388888888,color:grey},{source:17,target:22,orb:0.05831111111109,color:grey}],{source:11,target:14,orb:-1.6950111111,color:grey},{source:8,target:773275,color:7775,{来源:7,目标:8,球体:0.4157999999996,颜色:灰色},{来源:7,目标:12,球体:-0.0474277777784,颜色:灰色},{来源:7,目标:15,球体:0.655322222222,颜色:灰色},{来源:8,目标:15,球体:0.239522223,颜色:灰色},{来源:12,目标:15,球体:0.70274999999998,颜色:灰色},{来源:8,目标:灰色},{来源:8,目标:55552,目标:灰色},{source:2,target:18,orb:0.2764,color:grey},{source:16,target:18,orb:-1.05325555556,color:grey}],{source:8,target:12,orb:0.4632277777775,color:grey},{source:4,target:7,orb:-0.538627777772,color:-grey},{source:4,target:8,orb:-0.1228277776,color:grey},{source:4,target:12,color:605,color:-5555},{source:4,target:15,orb:0.11669444448,color:grey},{source:7,target:8,orb:0.41579999996,color:grey},{source:7,target:12,orb:-0.047427777777784,color:grey},{source:7,target:15,orb:0.655322222222222222222,color},{source:8,target:15,orb:0.239522223,color:grey},color:12,color:2749998},color:[{source:2,target:13,orb:0.947627777777,color:grey},{source:2,target:16,orb:1.32965555555,color:grey},{source:2,target:18,orb:0.2764,color:grey},{source:13,target:16,orb:-0.3820277778,color:grey},{source:13,target:18,orb:-0.67122777,color:grey},color:16,target:18,orb:-0.6712277777,color:-5556},{[{source:2,target:13,orb:0.947627777777,color:grey},{source:2,target:19,orb:-1.89746666667,color:grey},{source:13,target:19,orb:2.8450944444444,color:grey},{source:11,target:20,orb:-2.9160166666667,color:grey},{source:11,target:21,orb:-2.708116667,color:grey},color:20,color:2079,orb}[{来源:3,目标:9,球体:-2.106438888889,颜色:灰色},{来源:3,目标:20,球体:0.833727777777,颜色:灰色},{来源:9,目标:20,球体:-2.9401666666667,颜色:灰色},{来源:2,目标:9,球体:2.967261111111,颜色:灰色},{来源:2,目标:13,球体:0.94762777777777,颜色:灰色},{来源:2,目标:9,目标:灰色},目标:5555555,灰色},{source:2,target:18,orb:0.2764,color:grey},{source:9,target:13,orb:2.0196333333333,color:grey},{source:9,target:16,target:16,orb:16,orb:1.6376055555556,color:grey},{source:9,target:18,orb:2.69086111111111,color},{source:13,target:16,orb:-0.3820277778,color:grey},{source:13,target:18,orb:77777,color:-777},{来源:16,目标:18,球体:-1.05325555556,颜色:灰色}],{来源:2,目标:5,球体:-2.414461111111,颜色:灰色},{来源:2,目标:6,球体:1.380372222222,颜色:灰色},{来源:2,目标:18,球体:0.2764,颜色:灰色},{来源:2,目标:19,球体:-1.8974666666667,颜色:灰色},{来源:5,目标:6,球体:880389,灰色},{source:5,target:18,orb:2.69086111111,color:grey},{source:5,target:19,orb:-0.51699444445,color:grey},{source:6,target:18,orb:1.65677222222,color:grey},{source:6,target:19,orb:-0.51709444444444,color},{source:18,target:19,orb:2.17386666667,color:grey},{source:1,target:127772,orb:-778,grey},{source:1,target:6,orb:-2.6075,color:grey},{source:1,target:13,orb:-0.279500000001,color:grey},{source:1,target:16,orb:0.10252777777777,color:grey},{source:1,target:18,orb:-0.9507277777777779,color:grey},{source:2,target:6,orb:1.3803722222222,color:grey},{source:2,target:13,orb:77770,color:grey},{来源:2,目标:16,圆球:1.329655555,颜色:灰色},{来源:2,目标:18,圆球:0.2764,颜色:灰色},{来源:6,目标:13,圆球:2.328,颜色:灰色},{来源:6,目标:16,圆球:2.71002777777777778,颜色:灰色},{来源:6,目标:18,圆球:1.65677222222222,颜色:灰色},{来源:13,目标:16,圆球:770.3877778,灰色},{来源:13,目标:18,圆球:-0.6712277777,颜色:灰色},{来源:16,目标:18,圆球:-1.05325555556,颜色:灰色},{来源:1,目标:2,圆球:-1.2271277777778,颜色:灰色},{来源:1,目标:9,圆球:1.740133333333333,颜色:灰色},{来源:1,目标:13,圆球:-0.27950000001,颜色:灰色},{来源:1,目标:16,目标:灰色},圆球:102777,{来源:1,目标:18,球体:-0.9507277779,颜色:灰色},{来源:2,目标:9,o rb:2.967261111111,颜色:灰色},{来源:2,目标:13,目标:0.947627777777,颜色:灰色},{来源:2,目标:16,目标:1.3296555555555,颜色:灰色},{来源:2,目标:18,目标:0.2764,颜色:灰色},{来源:9,目标:13,目标:球:2.0196333333333333333,颜色:灰色},{来源:9,目标:16,目标:1.635556,颜色:灰色},目标:1.63555556,颜色:灰色},目标:690196333333333,{来源:13,目标:16,球体:-0.3820277778,颜色:灰色},{来源:13,目标:18,球体:-0.6712277777,颜色:灰色},{来源:16,目标:18,球体:-1.05325555556,颜色:灰色}]

var节点=[{名称:地球太阳,颜色:暗绿色},{名称:水星,颜色:青色},{名称:金星,颜色:洋红色},{名称:火星,颜色:红色},{名称:木星,颜色:金色},{名称:土星,颜色:暗灰色},{名称:天王星,颜色:蓝色},{名称:海王星,颜色:蓝紫色},{名称:冥王星,颜色:栗色},{名称:凯龙,颜色:橙色},{名称:塞勒斯,颜色:桑迪本},{姓名:朱诺,颜色:兰花},{姓名:帕拉斯,颜色:皇家蓝},{姓名:维斯塔,颜色:粉红},{姓名:萨福,颜色:李子},{姓名:塞德纳,颜色:石板灰},{姓名:马萨图恩,颜色:空},{姓名:朱庇塔伦,颜色:棕色},{姓名:土星凯龙,颜色:印度红},{姓名:土星天王星,颜色:空},{姓名:土星海王星,颜色:空},{姓名:土星海王星,颜色:空},{姓名:土星冥王星,颜色:空},{名称:SATURNSEDNA,颜色:null}]


谢谢你看这个。

嘿,你能写下当前团队中的内容吗?谢谢。嗨,数据会根据用户的输入而变化,但我为问题添加了一些示例数据。第一个当前团队=[{来源:17,目标:22,orb:0.058311111109,颜色:灰色}]。只有一个链接。后续的当前团队=[{来源:8,目标:12,球体:0.4632277777775,颜色:灰色},{来源:8,目标:15,球体:0.239522223,颜色:灰色},{来源:12,目标:15,球体:0.70274999998,颜色:灰色}]-这就是问题所在。当存在多个链接时,程序不会显示所有链接。Mhh您是否可以创建一个JSBin示例。这将帮助我理解您的问题。谢谢。谢谢!这是您想要的吗?