设置D3力导向图

5
尊敬的读者们,我在学习JavaScript时遇到了一个问题。我正试图实现这个修改版的力导向图:http://mbostock.github.com/d3/ex/force.html。JSON数据是通过PHP脚本动态生成的。我的想法是将与PHP脚本中定义的特定节点相连的所有线条都着为一种颜色,并将其他节点的线条着成灰色。我尝试通过匹配JSON文件中的源变量和PHP脚本中的变量,当它们相等时改变颜色,像这样:
  var link = svg.selectAll("line.link")
  .data(json.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value);})
  .style("stroke-opacity", function(d) { return d.value/10;})
  .style("stroke", function(d) { 
  x = (tested == d.source) ?  return '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.
  })

然而,这并不起作用。如果我只是这样做,脚本可以正常工作,但没有颜色变化。
  var link = svg.selectAll("line.link")
  .data(json.links)
  .enter().append("line")
  .attr("class", "link")
  .style("stroke-width", function(d) { return Math.sqrt(d.value);})
  .style("stroke-opacity", function(d) { return d.value/10;})
  .style("stroke", function(d) { 
  return '#707070';
  })

我已经盯着这个问题几天了,试图弄清楚如何完成它,但我卡住了。非常感谢任何帮助!
以下是我的完整脚本:
<script type="text/javascript">

var width = 1200,
    height = 1200;

var color = d3.scale.category20();

var tested=<?php echo $tested_source;?>; //<-- the variable from php

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

d3.json("data.json", function(json) {

var force = d3.layout.force()
    .charge(-130)
    .linkDistance(function(d) { return 500-(50*d.value);})
    .size([width, height]);

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

  var link = svg.selectAll("line.link")
      .data(json.links)
      .enter().append("line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value);})
      .style("stroke-opacity", function(d) { return d.value/10;})
      .style("stroke", function(d) { 
      x = (tested == d.source) ?  return '#1f77b4' : '#707070'; //<-- Attempt to change the color of the link when this is true. But is is not working...  :(
      })


  var node = svg.selectAll("circle.node")
      .data(json.nodes)
    .enter().append("circle")
      .attr("class", "node")
      .attr("r", 12)
      .style("fill", function(d) { return color(d.group); })
      .call(force.drag);

  node.append("title")
      .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("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

</script>
1个回答

8

d.source 是一个对象,你不能使用 == 来判断 tested 是否是相似的对象。点击此处查看更多关于对象相等性的细节。

如果你想要测试 d.source 对象中特定的值(我猜你是这样想的),你需要指定它。

以下是源对象的架构:(我使用了 你提供的示例 中的数据,所以数据来自于 miserables.json

source: Object
    group: 4
    index: 75
    name: "Brujon"
    px: 865.6440689638284
    py: 751.3426708796574
    weight: 7
    x: 865.9584580575608
    y: 751.2658636251376

现在,这是你代码中出问题的部分:
x = (tested == d.source) ?  return '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.

它不起作用是因为 return 放错了位置。你混淆了三元运算符和 return 语句,但没有按正确顺序放置它们:

return test ? value_if_true : value_if_false;

如果您仍然想将值分配给 x,可以执行以下操作
x = test ? value_if_true : value_if_false;
return x;

你应该像这样做:
return (tested == d.source) ? '#1f77b4' : '#707070';// <-- Attempt to change the color of the link when this is true.

这是一般语法,但不能直接使用。你需要为测试选择一个值,例如:
return (tested === d.source.name) ? '#1f77b4' : '#707070';

此外,如果PHP中的变量是字符串,您应该执行
var tested="<?php echo $tested_source;?>"; //<-- the variable from php

在大多数情况下,您应该使用 json_encode 将PHP变量映射到javascript变量中。
最后,我建议结合Firefox的Firebug控制台面板或基于Chromium浏览器的Chrome Developer Tool控制台面板,使用console函数。这将使您更轻松地调试代码。

工作中的代码

var width = 960,
  height = 500;

var color = d3.scale.category20();

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

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

var tested = 20;

d3.json("miserables.json", function (json) {
  force.nodes(json.nodes).links(json.links).start();

  var link = svg.selectAll("line.link")
  .data(json.links)
  .enter()
  .append("line")
  .attr("class", "link")
  .style("stroke-width", function (d) {
    return Math.sqrt(d.value);
  }).style("stroke-opacity", function (d) {
    return d.value / 10;
  }).style("stroke", function (d) {
    return (tested == d.source.index) ? '#ee3322' : '#707070'; //'#1f77b4'
  });

  var node = svg.selectAll("circle.node")
  .data(json.nodes)
  .enter()
  .append("circle")
  .attr("class", "node")
  .attr("r", 5)
  .style("fill", function (d) {
    return color(d.group);
  }).call(force.drag);

  node.append("title").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("cx", function (d) {
      return d.x;
    }).attr("cy", function (d) {
      return d.y;
    });
  });
});

谢谢!当涉及到JavaScript时,我是一个完全的新手。建议确实使脚本工作,但它仍然不能改变连接线的颜色。我认为从JSON数组中检索d.source可能存在问题。能帮忙吗? - user1378824
我已经编辑了我的答案,并附上了一些关于源对象的代码,你正在对tested进行测试。我认为问题就出在那里。 - Jérémie Parker
.style("stroke", function(d) { console.dir(d.source); return (tested == d.source) ? '#1f77b4' : '#707070'; })这段代码可以帮助你找到为什么颜色没有改变。请使用Firefox + Firebug或Chrome和Chrome开发者工具。我认为除此之外,我无法再帮助你了。 - Jérémie Parker
顺便提一下,我在之前的评论中打印的源对象正是console.dir(d.source)的结果。 - Jérémie Parker
我想说的是,你得到的对象是 d,它是一个源。如果你想操作链接,那么使用 this,但我不知道你试图针对 tested 进行什么测试,所以很难进行有意义的测试。你把 tested = 20,但应该等于 20 的是什么? - Jérémie Parker
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接