testing-d3.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Testing D3</title>
  8. <script src="https://d3js.org/d3.v4.min.js"></script>
  9. </head>
  10. <body>
  11. <svg width="960" height="600"></svg>
  12. <script>
  13. //data = d3.json("https://gist.githubusercontent.com/mbostock/4062045/raw/5916d145c8c048a6e3086915a6be464467391c62/miserables.json");
  14. var svg = d3.select("svg"),
  15. width = +svg.attr("width"),
  16. height = +svg.attr("height");
  17. var color = d3.scaleOrdinal(d3.schemeCategory20);
  18. var simulation = d3.forceSimulation()
  19. .force("link", d3.forceLink().id(function(d) { return d.id; }))
  20. .force("charge", d3.forceManyBody())
  21. .force("center", d3.forceCenter(width / 2, height / 2));
  22. d3.json("https://gist.githubusercontent.com/mbostock/4062045/raw/5916d145c8c048a6e3086915a6be464467391c62/miserables.json", function(error, graph) {
  23. if (error) throw error;
  24. var link = svg.append("g")
  25. .attr("class", "links")
  26. .selectAll("line")
  27. .data(graph.links)
  28. .enter().append("line")
  29. .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
  30. var node = svg.append("g")
  31. .attr("class", "nodes")
  32. .selectAll("g")
  33. .data(graph.nodes)
  34. .enter().append("g")
  35. var circles = node.append("circle")
  36. .attr("r", 5)
  37. .attr("fill", function(d) { return color(d.group); })
  38. .call(d3.drag()
  39. .on("start", dragstarted)
  40. .on("drag", dragged)
  41. .on("end", dragended));
  42. var lables = node.append("text")
  43. .text(function(d) {
  44. return d.id;
  45. })
  46. .attr('x', 6)
  47. .attr('y', 3);
  48. node.append("title")
  49. .text(function(d) { return d.id; });
  50. simulation
  51. .nodes(graph.nodes)
  52. .on("tick", ticked);
  53. simulation.force("link")
  54. .links(graph.links);
  55. function ticked() {
  56. link
  57. .attr("x1", function(d) { return d.source.x; })
  58. .attr("y1", function(d) { return d.source.y; })
  59. .attr("x2", function(d) { return d.target.x; })
  60. .attr("y2", function(d) { return d.target.y; });
  61. node
  62. .attr("transform", function(d) {
  63. return "translate(" + d.x + "," + d.y + ")";
  64. })
  65. }
  66. });
  67. function dragstarted(d) {
  68. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  69. d.fx = d.x;
  70. d.fy = d.y;
  71. }
  72. function dragged(d) {
  73. d.fx = d3.event.x;
  74. d.fy = d3.event.y;
  75. }
  76. function dragended(d) {
  77. if (!d3.event.active) simulation.alphaTarget(0);
  78. d.fx = null;
  79. d.fy = null;
  80. }
  81. </script>
  82. </body>
  83. </html>