graph.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* GNU Guix --- Functional package management for GNU
  2. Copyright (C) 2021 Ludovic Courtès <ludo@gnu.org>
  3. This file is part of GNU Guix.
  4. GNU Guix is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Guix is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. */
  14. // Inspired by these examples:
  15. // https://observablehq.com/@d3/force-directed-graph
  16. // https://observablehq.com/@d3/mobile-patent-suits
  17. // Visibility of the "undo" button.
  18. var undoVisibility = "hidden";
  19. // ID of a highlighted node.
  20. var highlightedNodeID = null;
  21. chart = function(links, nodes) {
  22. console.log("chart with " + nodes.length + " nodes and "
  23. + links.length + " edges");
  24. // Return the radius used for the "collision" force.
  25. function collisionRadius(node) {
  26. if (node.label == "system") return 10;
  27. else if (node.category == "essential") return 5;
  28. else if (node.category == "base") return 3;
  29. else return 1;
  30. }
  31. const simulation = d3.forceSimulation(nodes)
  32. .force("link", d3.forceLink(links).id(d => d.id))
  33. .force("charge", d3.forceManyBody()) //.strength(-100)
  34. // Center the mean position of all nodes.
  35. .force("center", d3.forceCenter(width / 2, height / 2))
  36. // Force "important" nodes further apart.
  37. .force("collision", d3.forceCollide(collisionRadius));
  38. const textArea = d3.select("div#container")
  39. .append("div").classed("text-area", true)
  40. .attr("x", 4).attr("y", 4);
  41. const controlArea = d3.select("div#container")
  42. .append("div").classed("control-area", true);
  43. function redraw() {
  44. d3.selectAll(".svg-content").remove();
  45. d3.selectAll(".text-area").remove();
  46. d3.selectAll(".control-area").remove();
  47. drawGraph();
  48. }
  49. const svg = d3.select("div#container")
  50. .append("svg")
  51. .attr("preserveAspectRatio", "xMinYMin meet")
  52. .attr("viewBox", [0, 0, width, height])
  53. .classed("svg-content", true)
  54. .on("click", x => {
  55. // Clicking in the blank area of the SVG deletes the
  56. // currently displayed value.
  57. d3.selectAll(".service-value").remove();
  58. });
  59. // build the arrow.
  60. svg.append("svg:defs").selectAll("marker")
  61. .data(["arrow"]) // Different link/path types can be defined here
  62. .enter().append("marker") // This section adds in the arrows
  63. .attr("id", String)
  64. .attr("viewBox", "0 -5 10 10")
  65. .attr("refX", 15)
  66. .attr("refY", -1.5)
  67. .attr("markerWidth", 6)
  68. .attr("markerHeight", 6)
  69. .attr("orient", "auto")
  70. .attr("fill", "#999")
  71. .append("svg:path")
  72. .attr("d", "M0,-5L10,0L0,5");
  73. const link = svg.append("g")
  74. .attr("stroke", "#999")
  75. .attr("stroke-opacity", 0.6)
  76. .selectAll("line")
  77. .data(links)
  78. .join("line")
  79. .classed("edge", true)
  80. .attr("marker-end", "url(#arrow)")
  81. .on("click", event => {
  82. // Display the service value.
  83. const edge = event.target.__data__;
  84. d3.selectAll(".service-value").remove();
  85. d3.text("/edge/" + edge.source.id + "/" + edge.target.id)
  86. .then(element => {
  87. textArea.append("div").classed("service-value", true)
  88. .html(element);
  89. });
  90. });
  91. function nodeColor (node) {
  92. if (node.category == "essential") return "#ff4466";
  93. else if (node.category == "base") return "#bb3344";
  94. else if (node.category == "desktop") return "#3344bb";
  95. else return "#bbb";
  96. }
  97. function nodeRadius(node) {
  98. if (node.label == "system") return 6;
  99. else if (node.category == "essential") return 3;
  100. else return 1;
  101. }
  102. function labelColor(node) {
  103. if (node.category == "essential") return "#000";
  104. else return "#422";
  105. }
  106. const node = svg.append("g")
  107. .attr("fill", "currentColor")
  108. .attr("stroke-linecap", "round")
  109. .attr("stroke-linejoin", "round")
  110. .selectAll("g")
  111. .data(nodes)
  112. .join("g")
  113. .call(drag(simulation));
  114. node.append("circle")
  115. .attr("stroke", "none")
  116. .attr("r", nodeRadius)
  117. .attr("fill", nodeColor)
  118. .append("title")
  119. .text(d => d.label);
  120. node.append("text")
  121. .attr("x", 0)
  122. .attr("y", 0)
  123. .attr("font-size", "4px")
  124. .attr("color", labelColor)
  125. .attr("text-align", "center")
  126. .classed("highlighted-node", n => {
  127. return n.id == highlightedNodeID;
  128. })
  129. .text(d => d.label)
  130. .on("mouseover", event => {
  131. // Display service type documentation.
  132. d3.select(event.target).attr("font-weight", "bold");
  133. // XXX: Is there a better way than accessing __data__?
  134. const node = event.target.__data__;
  135. const description = textArea
  136. .append("div").classed("node-description", true)
  137. description.append("div").classed("node-category", true)
  138. .text("category: " + node.category);
  139. description.append("div").html(node.description);
  140. })
  141. .on("click", event => {
  142. // Display the service value.
  143. const node = event.target.__data__;
  144. d3.selectAll(".service-value").remove();
  145. d3.text("/value/" + node.id)
  146. .then(element => {
  147. textArea.append("div").classed("service-value", true)
  148. .html(element);
  149. });
  150. })
  151. .on("mouseout", event => {
  152. d3.select(event.target).attr("font-weight", "normal");
  153. d3.selectAll(".node-description").remove();
  154. })
  155. .on("dblclick", event => {
  156. const node = event.target.__data__;
  157. console.log("folding " + node.id);
  158. d3.json("/fold/" + node.id).then(folded => {
  159. undoVisibility = "visible";
  160. highlightedNodeID = folded.id;
  161. redraw();
  162. });
  163. })
  164. .clone(true).lower()
  165. .attr("fill", "none")
  166. .attr("stroke", "white")
  167. .attr("stroke-width", 3);
  168. simulation.on("tick", () => {
  169. link
  170. .attr("x1", d => d.source.x)
  171. .attr("y1", d => d.source.y)
  172. .attr("x2", d => d.target.x)
  173. .attr("y2", d => d.target.y);
  174. node
  175. .attr("transform", d => `translate(${d.x},${d.y})`);
  176. });
  177. // invalidation.then(() => simulation.stop());
  178. controlArea.append("div").classed("button", true)
  179. .attr("id", "undo-button")
  180. .style("visibility", undoVisibility)
  181. .text("⎌")
  182. .on("click", event => {
  183. console.log("undo!");
  184. d3.text("/undo").then(redraw);
  185. });
  186. controlArea.append("div").classed("button", true)
  187. .text("⌧")
  188. .on("click", event => {
  189. console.log("reset!");
  190. d3.text("/reset").then(() => {
  191. undoVisibility = "hidden";
  192. redraw();
  193. });
  194. });
  195. return svg.node();
  196. }
  197. // height = document.documentElement.clientWidth;
  198. // width = document.documentElement.clientHeight;
  199. height = 200;
  200. width = 300;
  201. color = function() {
  202. const scale = d3.scaleOrdinal(d3.schemeCategory10);
  203. return d => scale(d.group);
  204. }
  205. drag = function (simulation) {
  206. function dragstarted(event) {
  207. if (!event.active) simulation.alphaTarget(0.3).restart();
  208. event.subject.fx = event.subject.x;
  209. event.subject.fy = event.subject.y;
  210. }
  211. function dragged(event) {
  212. event.subject.fx = event.x;
  213. event.subject.fy = event.y;
  214. }
  215. function dragended(event) {
  216. if (!event.active) simulation.alphaTarget(0);
  217. event.subject.fx = null;
  218. event.subject.fy = null;
  219. }
  220. return d3.drag()
  221. .on("start", dragstarted)
  222. .on("drag", dragged)
  223. .on("end", dragended);
  224. }
  225. drawGraph = function() {
  226. // Get the nodes and edges from the server as JSON.
  227. d3.json("/edges")
  228. .then(function(edges) {
  229. console.log("edges!");
  230. console.log(edges);
  231. d3.json("/nodes")
  232. .then(function (nodes) {
  233. console.log("nodes!");
  234. console.log(nodes);
  235. // Create the SVG.
  236. chart(edges, nodes);
  237. });
  238. });
  239. }
  240. console.log("starting!");
  241. drawGraph();