index.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <link rel="stylesheet" href="styles/styles.css">
  4. <svg width="960" height="600"></svg>
  5. <script src="script/d3js/d3.js"></script>
  6. <script src="script/script.js"></script>
  7. <!--
  8. <script>
  9. var svg = d3.select("svg"),
  10. width = +svg.attr("width"),
  11. height = +svg.attr("height");
  12. var color = d3.scaleOrdinal(d3.schemeCategory20);
  13. var simulation = d3.forceSimulation()
  14. .force("link", d3.forceLink().id(function(d) { return d.id; }))
  15. .force("charge", d3.forceManyBody())
  16. .force("center", d3.forceCenter(width / 2, height / 2));
  17. d3.json("miserables.json", function(error, graph) {
  18. if (error) throw error;
  19. var link = svg.append("g")
  20. .attr("class", "links")
  21. .selectAll("line")
  22. .data(graph.links)
  23. .enter().append("line")
  24. .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
  25. var node = svg.append("g")
  26. .attr("class", "nodes")
  27. .selectAll("circle")
  28. .data(graph.nodes)
  29. .enter().append("circle")
  30. .attr("r", 5)
  31. .attr("fill", function(d) { return color(d.group); })
  32. .call(d3.drag()
  33. .on("start", dragstarted)
  34. .on("drag", dragged)
  35. .on("end", dragended));
  36. node.append("title")
  37. .text(function(d) { return d.id; });
  38. simulation
  39. .nodes(graph.nodes)
  40. .on("tick", ticked);
  41. simulation.force("link")
  42. .links(graph.links);
  43. function ticked() {
  44. link
  45. .attr("x1", function(d) { return d.source.x; })
  46. .attr("y1", function(d) { return d.source.y; })
  47. .attr("x2", function(d) { return d.target.x; })
  48. .attr("y2", function(d) { return d.target.y; });
  49. node
  50. .attr("cx", function(d) { return d.x; })
  51. .attr("cy", function(d) { return d.y; });
  52. }
  53. });
  54. function dragstarted(d) {
  55. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  56. d.fx = d.x;
  57. d.fy = d.y;
  58. }
  59. function dragged(d) {
  60. d.fx = d3.event.x;
  61. d.fy = d3.event.y;
  62. }
  63. function dragended(d) {
  64. if (!d3.event.active) simulation.alphaTarget(0);
  65. d.fx = null;
  66. d.fy = null;
  67. }
  68. </script> -->