0

私は D3 に非常に慣れていないので、基本的にはたくさんの例と問題を試してみます。私はこのようなjsonを持っています

そしてd3のものはこのようになります。

<div id="body">
      <div id="chart"></div>
    </div>
 var inNodes = getNodes();

var w = 600,
    h = 400,
    z = d3.scale.category20c();

var force = d3.layout.force()
         .linkDistance(100)
        .size([w, h])
        .charge(-430);

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

svg.append("svg:rect")
    .attr("width", w)
    .attr("height", h);

  var nodes = flatten(inNodes),
      links = d3.layout.tree().links(nodes);

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

  var link = svg.selectAll("line")
      .data(links)
    .enter().insert("svg:line")
      .style("stroke", "#999")
      .style("stroke-width", "1px");

  var node = svg.selectAll("circle.node")
      .data(nodes)
    .enter().append("svg:circle")
      .attr("r", function(d){return d.size*2})
      .style("fill", function(d) { return z(d.parent && d.parent.name); })
      .style("stroke", "#000")
      .call(force.drag);  

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


function flatten(root) {
  var nodes = [];
  function recurse(node, depth) {
    if (node.children) {
      node.children.forEach(function(child) {
        child.parent = node;
        recurse(child, depth + 1);
      });
    }
    node.depth = depth;
    nodes.push(node);
  }
  recurse(root, 1);
  return nodes;
}

ここの構文で少し迷っています。だから、これについていくつかの助けが必要です。ノード内の各ノードの名前を出力しようとしています。

function getNodes() {

var inNodes = {
    "name": "APP",
    "children": [
        {
        "name": "API 1",
        "size": 10
        },
        {
        "name": "API 2",
        "size": 10
        },
        {
        "name": "API 3",
        "size": 10,
        }
    ],
    "size":20
};

return inNodes;
}

私はたくさんの例を調べましたが、物事がどのように起こっているのか本当に理解していません.

4

1 に答える 1

0

参照: http://tributary.io/inlet/5809527

データ ポイントごとappendに svg要素が必要です。text次に、tick()関数で、テキストの ( "dx"and ) 属性を円の(and ) 属性と"dy"同じになるように設定します。"cx""cy"

于 2013-06-18T21:23:12.877 に答える