graphview.erb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <center><svg width="900" height="600"></svg></center>
  2. <script src="http://d3js.org/d3.v4.min.js"></script>
  3. <script>
  4. var svg = d3.select("svg"),
  5. width = +svg.attr("width"),
  6. height = +svg.attr("height"),
  7. g = svg.append("g").attr("transform", "translate(40,0)");
  8. var tree = d3.cluster()
  9. .size([height, width - 160]);
  10. var stratify = d3.stratify()
  11. .parentId(function(d) { return d.id.substring(0, d.id.lastIndexOf(".")); });
  12. d3.csv("/graph/<%= graph_type %>", function(error, data) {
  13. if (error) throw error;
  14. var root = stratify(data)
  15. .sort(function(a, b) { return (a.height - b.height) || a.id.localeCompare(b.id); });
  16. tree(root);
  17. var link = g.selectAll(".link")
  18. .data(root.descendants().slice(1))
  19. .enter().append("path")
  20. .attr("class", "link")
  21. .attr("d", function(d) {
  22. return "M" + d.y + "," + d.x
  23. + "C" + (d.parent.y + 100) + "," + d.x
  24. + " " + (d.parent.y + 100) + "," + d.parent.x
  25. + " " + d.parent.y + "," + d.parent.x;
  26. });
  27. var node = g.selectAll(".node")
  28. .data(root.descendants())
  29. .enter().append("g")
  30. .attr("class", function(d) { return "node" + (d.children ? " node--internal" : " node--leaf"); })
  31. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
  32. node.append("circle")
  33. .attr("r", 2.5);
  34. node.append("text")
  35. .attr("dy", 3)
  36. .attr("x", function(d) { return d.children ? -8 : 8; })
  37. .style("text-anchor", function(d) { return d.children ? "end" : "start"; })
  38. .text(function(d) { return d.id.substring(d.id.lastIndexOf(".") + 1); });
  39. });
  40. </script>