简体   繁体   English

为 D3 树布局嵌套 CSV

[英]Nesting a CSV for D3 Tree Layout

I have a CSV file structured thus我有一个结构化的 CSV 文件

source, target, name, value1 , percentange
  A1      A1      T1    200        3%
  A1      A2      T2    340        4%
  A1      A3      T3    400        2%  

I would like to construct a tree diagram similar to this D3 Pedigre Diagram我想构建一个类似于这个 D3 Pedigre Diagram 的树状图

I have tried to flatten the file using this example and had some success, but I can't seem to get the array to carry the value and percentage fields so they will render on the node.我尝试使用此示例将文件展平并取得了一些成功,但我似乎无法让数组携带百分比字段,因此它们将在节点上呈现。

I have tried these examples, but they don't seem to allow the full nesting https://gist.github.com/phoebebright/3176159#file-index-html我已经尝试过这些示例,但它们似乎不允许完整的嵌套https://gist.github.com/phoebebright/3176159#file-index-html

D3: use nest function to turn flat data with parent key into a hierarchy D3:使用嵌套函数将带有父键的平面数据转换为层次结构

Please see my code below, the goal is to ensure value and percentage are displayed at the node.请参阅下面的代码,目标是确保在节点上显示值和百分比。

d3.csv("graph2.csv", function(error, links) {
  if (error) throw error;

  var nodesByName = {};

  // Create nodes for each unique source and target.
  links.forEach(function(link) {
    var parent = (link.source = nodeByName(link.source)),
      child = (link.target = nodeByName(link.target));
    if (parent.children) parent.children.push(child);
    else parent.children = [child];
  });
});

This is where I 'Lose' all the value and percentage data for labelling这是我“丢失”标签的所有值和百分比数据的地方

// Extract the root node and compute the layout.
var nodes = tree.nodes(links[0].source);

// Create the link lines.
var link = svg
  .selectAll(".link")
  .data(links)
  .enter()
  .append("path")
  .attr("class", "link")
  .attr("d", diagonal);

// Create the node circles.
var node = svg
  .selectAll(".node")
  .data(nodes)
  .enter()
  .append("g")
  .attr("class", "node")
  .attr("transform", function(d) {
    return "translate(" + d.y + "," + d.x + ")";
  });

node
  .append("circle")
  .attr("class", "node")
  .attr("r", 4.5);

Would like to have ALL the values from the CSV file here to append to the node想要将 CSV 文件中的所有值附加到节点

node
  .append("text")
  .attr("class", "source")
  .attr("x", 8)
  .attr("y", -6)
  .text(function(d) {
    return d.name;
  });

function nodeByName(name) {
  return nodesByName[name] || (nodesByName[name] = { name: name });
}

You can save the node to nodesByName :您可以将节点保存到nodesByName

var nodesByName = {};

// Create nodes for each unique source and target.
links.forEach(function(link) {
  var parent = (link.source = nodeByName(link.source, link)),
    child = (link.target = nodeByName(link.target, link));
  if (parent.children) parent.children.push(child);
  else parent.children = [child];
});

function nodeByName(name, node) {
  return nodesByName[name] || (nodesByName[name] = { name: name, node: node });
}

Then access them like this:然后像这样访问它们:

node
  .append("text")
  .attr("class", "source")
  .attr("x", 8)
  .attr("y", -6)
  .text(function(d) {
    var node = nodesByName[d.name].node;
    return d.name + " " + node.value1 + " " + node.percentage;
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM