使用csv文件

时间:2015-04-22 16:00:24

标签: javascript json csv d3.js bundle-layout

我正在尝试制作捆绑布局,我设法做到了。但是,我希望能够使用简单的CSV字段而不是json。我试图用d3.csv函数替换d3.json但是我无法使它工作。

有什么想法吗?

我的代码:http://plnkr.co/edit/0hwlJmEhNyjqhuCPdW5w

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.node {
  font: 300 11px "Helvetica Neue", Helvetica, Arial, sans-serif;
  fill: #bbb;
}

.node:hover {
  fill: #000;
}

.link {
  stroke: steelblue;
  stroke-opacity: .4;
  fill: none;
  pointer-events: none;
}

.node:hover,
.node--source,
.node--target {
  font-weight: 700;
}

.node--source {
  fill: #2ca02c;
}

.node--target {
  fill: #d62728;
}

.link--source,
.link--target {
  stroke-opacity: 1;
  stroke-width: 2px;
}

.link--source {
  stroke: #d62728;
}

.link--target {
  stroke: #2ca02c;
}

</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div>
<script>


var diameter = 480, // diameter = 960 ou 480
    radius = diameter / 2,
    innerRadius = radius - 120;

var cluster = d3.layout.cluster()
    .size([360, innerRadius])
    .sort(null)
    .value(function(d) { return d.size; });

var bundle = d3.layout.bundle();

var line = d3.svg.line.radial()
    .interpolate("bundle")
    .tension(.85)
    .radius(function(d) { return -d.y; })
    .angle(function(d) { return d.x / 180 * Math.PI; });

var svg = d3.select("body").append("svg")
    .attr("width", diameter)
    .attr("height", diameter)
  .append("g")
    .attr("transform", "translate(" + radius + "," + radius + ")");

var link = svg.append("g").selectAll(".link"),
    node = svg.append("g").selectAll(".node");

d3.json("test.json", function(error, classes) {
  var nodes = cluster.nodes(packageHierarchy(classes)),
      links = packageLinks(nodes); // links = packageImports(nodes);

  link = link
      .data(bundle(links))
    .enter().append("path")
      .each(function(d) { d.source = d[0], d.target = d[d.length - 1]; })
      .attr("class", "link")
      .attr("d", line); 


  node = node
      .data(nodes.filter(function(n) { return !n.children; }))
  // add an URL on each link
    .enter().append('a')
    .attr("xlink:href", function(d){return d.url;})
    .append("text")
      .attr("class", "node")
      .attr("dy", ".31em")
      // put the parents on the left rather than on the right
      .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + ((-d.y) - 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
      //.attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
      .style("text-anchor", function(d) { return d.x < 180 ? "end" : "start"; })
      .text(function(d) { return d.key })
      .on("mouseover", mouseovered)
      .on("mouseout", mouseouted);
});

function mouseovered(d) {
  node
      .each(function(n) { n.target = n.source = false; });

  link
      .classed("link--target", function(l) { if (l.target === d) return l.source.source = true; })
      .classed("link--source", function(l) { if (l.source === d) return l.target.target = true; })
    .filter(function(l) { return l.target === d || l.source === d; })
      .each(function() { this.parentNode.appendChild(this); });

  node
      .classed("node--target", function(n) { return n.target; })
      .classed("node--source", function(n) { return n.source; });
}

function mouseouted(d) {
  link
      .classed("link--target", false)
      .classed("link--source", false);

  node
      .classed("node--target", false)
      .classed("node--source", false);
}

d3.select(self.frameElement).style("height", diameter + "px");

// Lazily construct the package hierarchy from class names.
function packageHierarchy(classes) {
  var map = {};

  function find(name, data) {
    var node = map[name], i;
    if (!node) {
      node = map[name] = data || {name: name, children: []};
      if (name.length) {
        node.parent = find(name.substring(0, i = name.lastIndexOf(".")));
        node.parent.children.push(node);
        node.key = name.substring(i + 1);
      }
    }
    return node;
  }

  classes.forEach(function(d) {
    find(d.name, d);
  });

  return map[""];
}

// Return a list of imports/links for the given array of nodes.
function packageLinks(nodes) { //function packageImports(nodes) {
  var map = {},
      links = []; // imports = [];

  // Compute a map from name to node.
  nodes.forEach(function(d) {
    map[d.name] = d;
  });

  // For each import/link, construct a link from the source to target node.
  nodes.forEach(function(d) {
    if (d.links) d.links.forEach(function(i) { //if (d.imports) d.imports.forEach(function(i) {
      links.push({source: map[d.name], target: map[i]}); //imports.push({source: map[d.name], target: map[i]});
    });
  });

  return links; //return imports;
}

</script>
</div>

test.json文件:

[
{"name":"brut.smith.Grandpa","url":"http://www.google.com","tooltip":"Lorem","links":["brut.smith.Grandpa"]},
{"name":"brut.smith.Daddy","url":"http://www.gmail.com","tooltip":"Ipsum","links":["brut.smith.Grandpa"]},
{"name":"brut.smith.Mummy","url":"http://www.yahoo.com","tooltip":"Dolor","links":["brut.smith.Mummy"]},
{"name":"pmu.smithKid.Peter","url":"http://www.microsoft.com","tooltip":"Sit","links":["brut.smith.Daddy", "brut.smith.Mummy"]},
{"name":"pmu.smithKid.Simon","url":"http://www.google.fr","tooltip":"Amet","links":["brut.smith.Daddy", "brut.smith.Mummy"]},
{"name":"brut.martin.Uncle","url":"https://github.com/mbostock/d3/wiki/CSV","tooltip":"Consectetur","links":["brut.martin.Uncle"]},
{"name":"brut.martin.Aunt","url":"http://bl.ocks.org/mbostock/7607999","tooltip":"Elit","links":["brut.martin.Aunt"]},
{"name":"pmu.martinKid.Mathis","url":"http://stackoverflow.com/","tooltip":"Lorem","links":["brut.martin.Uncle", "brut.martin.Aunt"]},
{"name":"pmu.martinKid.Adrien","url":"htpp://www.microsoft.com","tooltip":"Ipsum","links":["brut.martin.Uncle", "brut.martin.Aunt"]},
{"name":"pmu.martinKid.Chloé","url":"http://www.d3js.org","tooltip":"Dolor","links":["brut.martin.Uncle", "brut.martin.Aunt"]}
]

test.csv文件:

name;url;tooltip;links
parent.smith.Grandpa;http://www.google.com;Lorem;[parent.smith.Grandpa]
parent.smith.Daddy;http://www.gmail.com;Ipsum;[parent.smith.Grandpa]
parent.smith.Mummy;http://www.yahoo.com;Dolor;[parent.smith.Mummy]
child.smithKid.Peter;http://www.microsoft.com;Sit;[parent.smith.Daddy, parent.smith.Mummy]
child.smithKid.Simon;http://www.google.fr;Amet;[parent.smith.Daddy, parent.smith.Mummy]
parent.martin.Uncle;https://github.com/mbostock/d3/wiki/CSV;Consectetur;[parent.martin.Uncle]
parent.martin.Aunt;http://bl.ocks.org/mbostock/7607999;Adipiscing;[parent.martin.Aunt]
child.martinKid.Mathis;http://stackoverflow.com/;Elit;[parent.martin.Uncle, parent.martin.Aunt]
child.martinKid.Adrian;htpp://www.microsoft.com;Lorem;[parent.martin.Uncle, parent.martin.Aunt]
child.martinKid.Chloé;http://www.d3js.org;Ipsum;[parent.martin.Uncle, parent.martin.Aunt]

0 个答案:

没有答案