【问题标题】:Setting up D3 force directed graph建立 D3 力有向图
【发布时间】:2012-05-07 02:21:53
【问题描述】:

致尊敬的读者。我在 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>

【问题讨论】:

    标签: javascript d3.js force-layout


    【解决方案1】:

    d.source是一个对象,你不能用==来判断tested是不是一个类似的对象。 Have a look at this answer for more details on object equality.

    如果你想测试下面描述的d.source 对象的特定值,我假设你想要,你需要指定它。

    这是源对象架构:(我使用的是the example you pointed,所以数据来自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 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,我建议使用 console 函数和 Firebug 的控制台面板,如果您使用的是基于 Chromium 的浏览器,我建议您使用 Chrome Developer Tool 的控制台面板.它可以让您更轻松地调试代码。


    工作代码

    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 可能存在问题。帮忙??
    • 我已经用一些代码编辑了我的答案,这些代码与您针对 tested 测试的源对象有关。我认为问题出在那儿。
    • 感谢您的快速回复。也许我误解了一些东西,但你所指的对象是节点。我正在尝试更改节点之间连接线的颜色。该值在 miserables.json 文件的“链接”中定义。我完全误解了什么吗?
    • .style("stroke", function(d) { console.dir(d.source); return (tested == d.source) ? '#1f77b4' : '#707070'; }) 这将帮助您找出颜色没有变化的原因。使用 firefox + firebug 或 chrome 以及 chrome 开发者工具。我想我能帮到你的还不止这些。
    • 我上面打印的源对象正是我之前评论中console.dir(d.source) 的结果
    猜你喜欢
    • 2014-02-27
    • 2016-03-28
    • 2019-04-23
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2013-09-01
    • 2013-06-30
    相关资源
    最近更新 更多