index.html 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Mestura Puerto. Enredómetro</title>
<style>
body { background-color: white; }
.node {
	font: 300 12px "Helvetica Neue", Helvetica, Arial, sans-serif;
	fill: #666;
}
.node:hover {
	  fill: #000;
}
.link {
	stroke: black;
	stroke-opacity: 0.4;
	fill: none;
	pointer-events: none;
}
.node:hover,
.node--source,
.node--target {
}
.node--source {
	/*fill: #2ca02c;*/
	fill: blue;
}
.node--target {
	/*fill: #d62728;*/
	fill: red;
}
.link--source,
.link--target {
	stroke-opacity: 1;
	stroke-width: 2px;
}
.link--source {
	/*stroke: #d62728;*/
	stroke: red;
}
.link--target {
	/*stroke: #2ca02c;*/
	stroke: blue;
}
</style>
</head>
<body>
<script src="js/d3.v4.min.js"></script>
<script>
var diameter = 900,
    radius = diameter / 2,
    innerRadius = radius - 320;
var cluster = d3.cluster()
    .size([360, innerRadius]);
var line = d3.radialLine()
    .curve(d3.curveBundle.beta(0.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("data/flare.redux.json", function(error, classes) {
d3.csv("data/mestura.enredometro.csv", function(error, rels) {
  if (error) throw error;
	var nodesByName = [];

	// Create nodes for each unique source and target.
	rels.forEach(function(link) {
		var parent = link.name;
		var child = link.imports;
		// var parent = link.name = nodeByName(link.name),
		// child = link.imports = nodeByName(link.imports);
		// if (parent.imports) parent.imports.push(child);
		// else parent.imports = [child];
		if (parent in nodesByName) {
			nodesByName[parent].imports.push(link.imports);
		} else {
			nodesByName[parent] = {};
			nodesByName[parent].name = link.name;
			nodesByName[parent].imports = [];
			nodesByName[parent].imports.push(link.imports);
		}
		if (child in nodesByName) {} else {
			nodesByName[child] = {};
			nodesByName[child].name = child;
			nodesByName[child].imports = [];
		}
	});
	// function nodeByName(value) {
	//	return nodesByName[value] || (nodesByName[value] = {name: value});
	// }
	var relsBvalues = d3.values(nodesByName);

  var root = packageHierarchy(relsBvalues)
      .sum(function(d) { return d.size; });
  cluster(root);
  link = link
    .data(packageImports(root.leaves()))
    .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(root.leaves())
    .enter().append("text")
      .attr("class", "node")
      .attr("dy", "0.31em")
      .attr("transform", function(d) { return "rotate(" + (d.x - 90) + ")translate(" + (d.y + 8) + ",0)" + (d.x < 180 ? "" : "rotate(180)"); })
      .attr("text-anchor", function(d) { return d.x < 180 ? "start" : "end"; })
      .text(function(d) { return d.data.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; })
      .raise();
  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);
}
// 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 d3.hierarchy(map[""]);
}
// Return a list of imports for the given array of nodes.
function packageImports(nodes) {
  var map = {},
      imports = [];
  // Compute a map from name to node.
  nodes.forEach(function(d) {
    map[d.data.name] = d;
  });
  // For each import, construct a link from the source to target node.
  nodes.forEach(function(d) {
    if (d.data.imports) d.data.imports.forEach(function(i) {
      imports.push(map[d.data.name].path(map[i]));
    });
  });
  return imports;
}
</script>
</body>
</html>