Class: RailsFlowMap::D3jsFormatter
- Inherits:
-
Object
- Object
- RailsFlowMap::D3jsFormatter
- Defined in:
- lib/rails_flow_map/formatters/d3js_formatter.rb
Overview
Generates an interactive HTML visualization using D3.js library
This formatter creates a full HTML page with embedded JavaScript that renders an interactive force-directed graph. Users can drag nodes, zoom, search, and filter by node types.
Instance Method Summary collapse
-
#format(graph = @graph) ⇒ String
Generates the HTML visualization.
-
#initialize(graph, options = {}) ⇒ D3jsFormatter
constructor
Creates a new D3.js formatter instance.
Constructor Details
#initialize(graph, options = {}) ⇒ D3jsFormatter
Creates a new D3.js formatter instance
30 31 32 33 |
# File 'lib/rails_flow_map/formatters/d3js_formatter.rb', line 30 def initialize(graph, = {}) @graph = graph = end |
Instance Method Details
#format(graph = @graph) ⇒ String
Generates the HTML visualization
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
# File 'lib/rails_flow_map/formatters/d3js_formatter.rb', line 39 def format(graph = @graph) html_content = "<!DOCTYPE html>\n<html lang=\"ja\">\n<head>\n <meta charset=\"UTF-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>Rails Flow Map - Interactive Visualization</title>\n <script src=\"https://d3js.org/d3.v7.min.js\"></script>\n <style>\n body {\n font-family: Arial, sans-serif;\n margin: 0;\n overflow: hidden;\n }\n \n #container {\n width: 100vw;\n height: 100vh;\n position: relative;\n }\n \n #controls {\n position: absolute;\n top: 10px;\n left: 10px;\n background: white;\n padding: 15px;\n border-radius: 5px;\n box-shadow: 0 2px 10px rgba(0,0,0,0.1);\n z-index: 1000;\n }\n \n #search {\n padding: 5px 10px;\n border: 1px solid #ddd;\n border-radius: 3px;\n width: 200px;\n margin-bottom: 10px;\n }\n \n .filter-group {\n margin-bottom: 10px;\n }\n \n .filter-group label {\n display: block;\n margin: 5px 0;\n cursor: pointer;\n }\n \n .node {\n cursor: pointer;\n }\n \n .node circle {\n stroke-width: 2px;\n }\n \n .node text {\n font: 12px sans-serif;\n pointer-events: none;\n }\n \n .link {\n fill: none;\n stroke: #999;\n stroke-opacity: 0.6;\n stroke-width: 1.5px;\n }\n \n .link-label {\n font: 10px sans-serif;\n fill: #666;\n }\n \n .highlighted {\n opacity: 1 !important;\n }\n \n .dimmed {\n opacity: 0.2;\n }\n \n .tooltip {\n position: absolute;\n padding: 10px;\n background: rgba(0, 0, 0, 0.8);\n color: white;\n border-radius: 5px;\n pointer-events: none;\n opacity: 0;\n transition: opacity 0.3s;\n font-size: 12px;\n }\n \n #info {\n position: absolute;\n bottom: 10px;\n right: 10px;\n background: white;\n padding: 10px;\n border-radius: 5px;\n box-shadow: 0 2px 10px rgba(0,0,0,0.1);\n font-size: 12px;\n }\n \n .legend {\n position: absolute;\n top: 10px;\n right: 10px;\n background: white;\n padding: 15px;\n border-radius: 5px;\n box-shadow: 0 2px 10px rgba(0,0,0,0.1);\n }\n \n .legend-item {\n margin: 5px 0;\n }\n \n .legend-color {\n display: inline-block;\n width: 20px;\n height: 20px;\n margin-right: 5px;\n vertical-align: middle;\n border-radius: 3px;\n }\n </style>\n</head>\n<body>\n <div id=\"container\">\n <div id=\"controls\">\n <h3>\u30D5\u30A3\u30EB\u30BF\u30FC</h3>\n <input type=\"text\" id=\"search\" placeholder=\"\u691C\u7D22...\">\n \n <div class=\"filter-group\">\n <label><input type=\"checkbox\" class=\"type-filter\" value=\"model\" checked> \u30E2\u30C7\u30EB</label>\n <label><input type=\"checkbox\" class=\"type-filter\" value=\"controller\" checked> \u30B3\u30F3\u30C8\u30ED\u30FC\u30E9\u30FC</label>\n <label><input type=\"checkbox\" class=\"type-filter\" value=\"action\" checked> \u30A2\u30AF\u30B7\u30E7\u30F3</label>\n <label><input type=\"checkbox\" class=\"type-filter\" value=\"service\" checked> \u30B5\u30FC\u30D3\u30B9</label>\n <label><input type=\"checkbox\" class=\"type-filter\" value=\"route\" checked> \u30EB\u30FC\u30C8</label>\n </div>\n \n <button id=\"reset-zoom\">\u30BA\u30FC\u30E0\u30EA\u30BB\u30C3\u30C8</button>\n <button id=\"center-graph\">\u4E2D\u592E\u306B\u914D\u7F6E</button>\n </div>\n \n <div class=\"legend\">\n <h4>\u51E1\u4F8B</h4>\n <div class=\"legend-item\">\n <span class=\"legend-color\" style=\"background: #ff9999\"></span>\u30E2\u30C7\u30EB\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-color\" style=\"background: #9999ff\"></span>\u30B3\u30F3\u30C8\u30ED\u30FC\u30E9\u30FC\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-color\" style=\"background: #99ff99\"></span>\u30A2\u30AF\u30B7\u30E7\u30F3\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-color\" style=\"background: #ffcc99\"></span>\u30B5\u30FC\u30D3\u30B9\n </div>\n <div class=\"legend-item\">\n <span class=\"legend-color\" style=\"background: #ffff99\"></span>\u30EB\u30FC\u30C8\n </div>\n </div>\n \n <div id=\"info\">\n \u30CE\u30FC\u30C9: <span id=\"node-count\">0</span> | \n \u30A8\u30C3\u30B8: <span id=\"edge-count\">0</span> |\n \u30BA\u30FC\u30E0: <span id=\"zoom-level\">100%</span>\n </div>\n \n <div class=\"tooltip\"></div>\n </div>\n\n <script>\n // \u30C7\u30FC\u30BF\u306E\u6E96\u5099\n const graphData = \#{generate_graph_data.to_json};\n \n // \u8272\u306E\u5B9A\u7FA9\n const colors = {\n model: '#ff9999',\n controller: '#9999ff',\n action: '#99ff99',\n service: '#ffcc99',\n route: '#ffff99'\n };\n \n // SVG\u306E\u8A2D\u5B9A\n const width = window.innerWidth;\n const height = window.innerHeight;\n \n const svg = d3.select(\"#container\")\n .append(\"svg\")\n .attr(\"width\", width)\n .attr(\"height\", height);\n \n const g = svg.append(\"g\");\n \n // \u30BA\u30FC\u30E0\u6A5F\u80FD\n const zoom = d3.zoom()\n .scaleExtent([0.1, 10])\n .on(\"zoom\", (event) => {\n g.attr(\"transform\", event.transform);\n d3.select(\"#zoom-level\").text(Math.round(event.transform.k * 100) + \"%\");\n });\n \n svg.call(zoom);\n \n // Force simulation\n const simulation = d3.forceSimulation(graphData.nodes)\n .force(\"link\", d3.forceLink(graphData.links).id(d => d.id).distance(100))\n .force(\"charge\", d3.forceManyBody().strength(-300))\n .force(\"center\", d3.forceCenter(width / 2, height / 2))\n .force(\"collision\", d3.forceCollide().radius(50));\n \n // \u30EA\u30F3\u30AF\u306E\u63CF\u753B\n const link = g.append(\"g\")\n .attr(\"class\", \"links\")\n .selectAll(\"line\")\n .data(graphData.links)\n .enter().append(\"line\")\n .attr(\"class\", \"link\")\n .attr(\"stroke\", d => d.type === 'belongs_to' ? '#ff6666' : '#999')\n .attr(\"stroke-dasharray\", d => d.type === 'has_action' ? '5,5' : 'none');\n \n // \u30CE\u30FC\u30C9\u30B0\u30EB\u30FC\u30D7\n const node = g.append(\"g\")\n .attr(\"class\", \"nodes\")\n .selectAll(\"g\")\n .data(graphData.nodes)\n .enter().append(\"g\")\n .attr(\"class\", \"node\")\n .call(d3.drag()\n .on(\"start\", dragstarted)\n .on(\"drag\", dragged)\n .on(\"end\", dragended));\n \n // \u30CE\u30FC\u30C9\u306E\u5186\n node.append(\"circle\")\n .attr(\"r\", d => d.type === 'model' ? 15 : 10)\n .attr(\"fill\", d => colors[d.type] || '#999')\n .attr(\"stroke\", \"#fff\");\n \n // \u30CE\u30FC\u30C9\u306E\u30E9\u30D9\u30EB\n node.append(\"text\")\n .attr(\"dy\", \".35em\")\n .attr(\"x\", 20)\n .text(d => d.name)\n .style(\"font-size\", \"12px\");\n \n // \u30C4\u30FC\u30EB\u30C1\u30C3\u30D7\n const tooltip = d3.select(\".tooltip\");\n \n node.on(\"mouseover\", function(event, d) {\n tooltip.transition()\n .duration(200)\n .style(\"opacity\", .9);\n \n let content = `<strong>${d.name}</strong><br/>\n \u30BF\u30A4\u30D7: ${d.type}<br/>`;\n \n if (d.attributes) {\n if (d.attributes.associations) {\n content += `\u95A2\u9023: ${d.attributes.associations.join(', ')}<br/>`;\n }\n if (d.attributes.path) {\n content += `\u30D1\u30B9: ${d.attributes.path}<br/>`;\n }\n if (d.attributes.verb) {\n content += `\u30E1\u30BD\u30C3\u30C9: ${d.attributes.verb}<br/>`;\n }\n }\n \n tooltip.html(content)\n .style(\"left\", (event.pageX + 10) + \"px\")\n .style(\"top\", (event.pageY - 28) + \"px\");\n })\n .on(\"mouseout\", function(d) {\n tooltip.transition()\n .duration(500)\n .style(\"opacity\", 0);\n });\n \n // \u30C0\u30D6\u30EB\u30AF\u30EA\u30C3\u30AF\u3067\u95A2\u9023\u30CE\u30FC\u30C9\u306E\u30CF\u30A4\u30E9\u30A4\u30C8\n node.on(\"dblclick\", function(event, d) {\n event.stopPropagation();\n highlightConnected(d);\n });\n \n // \u30B7\u30DF\u30E5\u30EC\u30FC\u30B7\u30E7\u30F3\u306E\u66F4\u65B0\n simulation.on(\"tick\", () => {\n link\n .attr(\"x1\", d => d.source.x)\n .attr(\"y1\", d => d.source.y)\n .attr(\"x2\", d => d.target.x)\n .attr(\"y2\", d => d.target.y);\n \n node\n .attr(\"transform\", d => `translate(${d.x},${d.y})`);\n });\n \n // \u30C9\u30E9\u30C3\u30B0\u6A5F\u80FD\n function dragstarted(event, d) {\n if (!event.active) simulation.alphaTarget(0.3).restart();\n d.fx = d.x;\n d.fy = d.y;\n }\n \n function dragged(event, d) {\n d.fx = event.x;\n d.fy = event.y;\n }\n \n function dragended(event, d) {\n if (!event.active) simulation.alphaTarget(0);\n d.fx = null;\n d.fy = null;\n }\n \n // \u691C\u7D22\u6A5F\u80FD\n d3.select(\"#search\").on(\"input\", function() {\n const searchTerm = this.value.toLowerCase();\n \n node.classed(\"dimmed\", d => {\n return searchTerm && !d.name.toLowerCase().includes(searchTerm);\n });\n \n link.classed(\"dimmed\", d => {\n return searchTerm && \n !d.source.name.toLowerCase().includes(searchTerm) && \n !d.target.name.toLowerCase().includes(searchTerm);\n });\n });\n \n // \u30D5\u30A3\u30EB\u30BF\u30FC\u6A5F\u80FD\n d3.selectAll(\".type-filter\").on(\"change\", function() {\n const visibleTypes = [];\n d3.selectAll(\".type-filter:checked\").each(function() {\n visibleTypes.push(this.value);\n });\n \n node.style(\"display\", d => visibleTypes.includes(d.type) ? null : \"none\");\n \n link.style(\"display\", d => {\n return visibleTypes.includes(d.source.type) && \n visibleTypes.includes(d.target.type) ? null : \"none\";\n });\n });\n \n // \u95A2\u9023\u30CE\u30FC\u30C9\u306E\u30CF\u30A4\u30E9\u30A4\u30C8\n function highlightConnected(selectedNode) {\n const connectedNodeIds = new Set([selectedNode.id]);\n \n graphData.links.forEach(link => {\n if (link.source.id === selectedNode.id) {\n connectedNodeIds.add(link.target.id);\n }\n if (link.target.id === selectedNode.id) {\n connectedNodeIds.add(link.source.id);\n }\n });\n \n node.classed(\"dimmed\", d => !connectedNodeIds.has(d.id));\n node.classed(\"highlighted\", d => connectedNodeIds.has(d.id));\n \n link.classed(\"dimmed\", d => {\n return !connectedNodeIds.has(d.source.id) || !connectedNodeIds.has(d.target.id);\n });\n link.classed(\"highlighted\", d => {\n return connectedNodeIds.has(d.source.id) && connectedNodeIds.has(d.target.id);\n });\n }\n \n // \u30EA\u30BB\u30C3\u30C8\u6A5F\u80FD\n svg.on(\"dblclick\", function() {\n node.classed(\"dimmed\", false);\n node.classed(\"highlighted\", false);\n link.classed(\"dimmed\", false);\n link.classed(\"highlighted\", false);\n });\n \n // \u30B3\u30F3\u30C8\u30ED\u30FC\u30EB\u30DC\u30BF\u30F3\n d3.select(\"#reset-zoom\").on(\"click\", () => {\n svg.transition()\n .duration(750)\n .call(zoom.transform, d3.zoomIdentity);\n });\n \n d3.select(\"#center-graph\").on(\"click\", () => {\n const bounds = g.node().getBBox();\n const fullWidth = width;\n const fullHeight = height;\n const widthScale = fullWidth / bounds.width;\n const heightScale = fullHeight / bounds.height;\n const scale = 0.8 * Math.min(widthScale, heightScale);\n const translate = [\n fullWidth / 2 - scale * (bounds.x + bounds.width / 2),\n fullHeight / 2 - scale * (bounds.y + bounds.height / 2)\n ];\n \n svg.transition()\n .duration(750)\n .call(zoom.transform, d3.zoomIdentity\n .translate(translate[0], translate[1])\n .scale(scale));\n });\n \n // \u60C5\u5831\u66F4\u65B0\n d3.select(\"#node-count\").text(graphData.nodes.length);\n d3.select(\"#edge-count\").text(graphData.links.length);\n </script>\n</body>\n</html>\n HTML\n \n html_content\nend\n" |