graphview.erb 1.6 KB

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