【问题标题】:Creating a D3.js Collapsible Tree from CSV data从 CSV 数据创建 D3.js 可折叠树
【发布时间】:2015-03-10 20:31:39
【问题描述】:

对于那些更熟悉 d3 的人来说,这可能是一个有点愚蠢的问题,但我对它相当陌生,我不太清楚如何让这个东西工作:

我想要实现的是:http://bl.ocks.org/robschmuecker/7880033

但我想向它提供来自平面 CSV 而不是 JSON 的数据。

问题是我拥有的 CSV 格式如下:

Parent Name | Child Name
-------------------------
Parent Name | Child Name
-------------------------
Parent Name | Child Name

so on...

有人可以指点我正确的方向吗?我知道 d3.csv 函数以某种方式工作,但我不知道如何将其“插入”到上面的示例中。

我很抱歉,我知道这听起来很像“为我做作业”,但老实说,我已经尽力了,我想我被卡住了。

谢谢。赞赏。

【问题讨论】:

    标签: javascript csv d3.js


    【解决方案1】:

    我以前没有看到您要查找的内容,但它是creating a tree from flat data(需要进行一些数据操作才能将其调整为正确的结构)和标准loading data from and external source 与d3 的组合。

    遗憾的是,我无法为您设置一个 bl.ock 来演示实时代码,编辑:bl.ocks.org 上正在运行的代码的Here is a live version,以下是两种技术结合的html文件;

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
    
        <title>Collapsible Tree Example</title>
    
        <style>
    
    	.node circle {
    	  fill: #fff;
    	  stroke: steelblue;
    	  stroke-width: 3px;
    	}
    
    	.node text { font: 12px sans-serif; }
    
    	.link {
    	  fill: none;
    	  stroke: #ccc;
    	  stroke-width: 2px;
    	}
    	
        </style>
    
      </head>
    
      <body>
    
    <!-- load the d3.js library -->	
    <script src="http://d3js.org/d3.v3.min.js"></script>
    	
    <script>
    
    // ************** Generate the tree diagram	 *****************
    var margin = {top: 20, right: 120, bottom: 20, left: 120},
    	width = 960 - margin.right - margin.left,
    	height = 500 - margin.top - margin.bottom;
    	
    var i = 0;
    
    var tree = d3.layout.tree()
    	.size([height, width]);
    
    var diagonal = d3.svg.diagonal()
    	.projection(function(d) { return [d.y, d.x]; });
    
    var svg = d3.select("body").append("svg")
    	.attr("width", width + margin.right + margin.left)
    	.attr("height", height + margin.top + margin.bottom)
      .append("g")
    	.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
    
    // load the external data
    d3.csv("treedata.csv", function(error, data) {
    
    // *********** Convert flat data into a nice tree ***************
    // create a name: node map
    var dataMap = data.reduce(function(map, node) {
    	map[node.name] = node;
    	return map;
    }, {});
    
    // create the tree array
    var treeData = [];
    data.forEach(function(node) {
    	// add to parent
    	var parent = dataMap[node.parent];
    	if (parent) {
    		// create child array if it doesn't exist
    		(parent.children || (parent.children = []))
    			// add node to child array
    			.push(node);
    	} else {
    		// parent is null or missing
    		treeData.push(node);
    	}
    });
    
      root = treeData[0];
      update(root);
    });
    
    function update(source) {
    
      // Compute the new tree layout.
      var nodes = tree.nodes(root).reverse(),
    	  links = tree.links(nodes);
    
      // Normalize for fixed-depth.
      nodes.forEach(function(d) { d.y = d.depth * 180; });
    
      // Declare the nodes…
      var node = svg.selectAll("g.node")
    	  .data(nodes, function(d) { return d.id || (d.id = ++i); });
    
      // Enter the nodes.
      var nodeEnter = node.enter().append("g")
    	  .attr("class", "node")
    	  .attr("transform", function(d) { 
    		  return "translate(" + d.y + "," + d.x + ")"; });
    
      nodeEnter.append("circle")
    	  .attr("r", 10)
    	  .style("fill", "#fff");
    
      nodeEnter.append("text")
    	  .attr("x", function(d) { 
    		  return d.children || d._children ? -13 : 13; })
    	  .attr("dy", ".35em")
    	  .attr("text-anchor", function(d) { 
    		  return d.children || d._children ? "end" : "start"; })
    	  .text(function(d) { return d.name; })
    	  .style("fill-opacity", 1);
    
      // Declare the links…
      var link = svg.selectAll("path.link")
    	  .data(links, function(d) { return d.target.id; });
    
      // Enter the links.
      link.enter().insert("path", "g")
    	  .attr("class", "link")
    	  .attr("d", diagonal);
    
    }
    
    </script>
    	
      </body>
    </html>

    以下是我测试过的csv文件(html文件中命名为treedata.csv);

    name,parent
    Level 2: A,Top Level
    Top Level,null
    Son of A,Level 2: A
    Daughter of A,Level 2: A
    Level 2: B,Top Level
    

    感谢 nrabinowitz 描述数据转换here

    【讨论】:

    • 太棒了,谢谢。我还发现这个例子也使用了相同的原理:gist.github.com/mbostock/2949981
    • 啊……干得好。我可能已经猜到迈克在过去的某个时候会描述过这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-02
    • 1970-01-01
    相关资源
    最近更新 更多