script_d3js.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var $container = $('#links');
  2. var width = $container.outerWidth(true);
  3. var height = $container.outerHeight(true);
  4. var svg = d3.select("#links svg");
  5. Addwidth = svg.attr("width", '100%'),
  6. Addheight = svg.attr("height", '100%');
  7. var color = d3.scaleOrdinal(d3.schemeCategory20);
  8. var simulation = d3.forceSimulation()
  9. .force("link", d3.forceLink().distance(20).id(function(d) { return d.id; }))
  10. .force("charge", d3.forceManyBody())
  11. .force("center", d3.forceCenter(width + (width /2)- 20, height + (height/2) - 30));
  12. d3.json("user/themes/r2c/json/miserables.json", function(error, graph) {
  13. if (error) throw error;
  14. var link = svg.append("g")
  15. .attr("class", "links")
  16. // .attr("left", "auto")
  17. .selectAll("line")
  18. .data(graph.links)
  19. .enter().append("line")
  20. .attr("stroke-width", function(d) { return Math.sqrt(d.value); });
  21. var node = svg.append("g")
  22. .attr("class", "nodes")
  23. .selectAll("text")
  24. .data(graph.nodes)
  25. .enter().append("text")
  26. .text(function(d) { return d.id; })
  27. // .attr("fill", function(d) { return color(); })
  28. .attr("class",function(d) { return "group" + d.group ;})
  29. .attr("id",function(d) { return d.identifiant; })
  30. .attr("href",function(d) { return d.cible; })
  31. .call(d3.drag()
  32. .on("start", dragstarted)
  33. .on("drag", dragged)
  34. .on("end", dragended));
  35. simulation
  36. .nodes(graph.nodes)
  37. .on("tick", ticked);
  38. simulation.force("link")
  39. .links(graph.links);
  40. function ticked() {
  41. link
  42. .attr("x1", function(d) { return d.source.x; })
  43. .attr("y1", function(d) { return d.source.y; })
  44. .attr("x2", function(d) { return d.target.x; })
  45. .attr("y2", function(d) { return d.target.y; });
  46. node
  47. .attr("x", function(d) { return d.x; })
  48. .attr("y", function(d) { return d.y; });
  49. }
  50. });
  51. function dragstarted(d) {
  52. if (!d3.event.active) simulation.alphaTarget(0.3).restart();
  53. d.fx = d.x;
  54. d.fy = d.y;
  55. }
  56. function dragged(d) {
  57. d.fx = d3.event.x;
  58. d.fy = d3.event.y;
  59. }
  60. function dragended(d) {
  61. if (!d3.event.active) simulation.alphaTarget(0);
  62. d.fx = null;
  63. d.fy = null;
  64. }