Skip to content

Commit a480405

Browse files
Merge pull request #178 from refactorfirst/#164-use-Vizdom
#164 Use Vizdom instead of D3 for DOT graph rendering
2 parents eb2cf74 + 9f39bcb commit a480405

3 files changed

Lines changed: 29 additions & 30 deletions

File tree

refactor-first-maven-plugin/src/main/java/org/hjug/mavenreport/RefactorFirstMavenReport.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ private void printHead(Sink mainSink) {
101101
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/sigma.js/2.4.0/sigma.min.js");
102102
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/graphology/0.25.4/graphology.umd.min.js");
103103

104-
// may only need graphlib-dot
105-
renderJsDeclaration(mainSink, "https://cdnjs.cloudflare.com/ajax/libs/graphlib/2.1.8/graphlib.min.js");
106-
renderJsDeclaration(mainSink, "https://cdn.jsdelivr.net/npm/graphlib-dot@0.6.4/dist/graphlib-dot.min.js");
107104
renderJsDeclaration(mainSink, "https://cdn.jsdelivr.net/npm/3d-force-graph");
105+
renderModuleDeclaration(mainSink, "https://cdn.jsdelivr.net/npm/@vizdom/vizdom-ts-web@0.1.19/vizdom_ts.min.js");
108106

109107
mainSink.head_();
110108
}
@@ -120,6 +118,14 @@ private void renderJsDeclaration(Sink mainSink, String scriptUrl) {
120118
mainSink.unknown("script", new Object[] {HtmlMarkup.TAG_TYPE_END}, null);
121119
}
122120

121+
private void renderModuleDeclaration(Sink mainSink, String scriptUrl) {
122+
SinkEventAttributeSet githubButtonJS = new SinkEventAttributeSet();
123+
githubButtonJS.addAttribute(SinkEventAttributes.TYPE, "module");
124+
githubButtonJS.addAttribute(SinkEventAttributes.SRC, scriptUrl);
125+
mainSink.unknown("script", new Object[] {HtmlMarkup.TAG_TYPE_START}, githubButtonJS);
126+
mainSink.unknown("script", new Object[] {HtmlMarkup.TAG_TYPE_END}, null);
127+
}
128+
123129
private void renderStyle(Sink mainSink) {
124130
SinkEventAttributeSet githubButtonJS = new SinkEventAttributeSet();
125131
githubButtonJS.addAttribute(SinkEventAttributes.SRC, HtmlReport.POPUP_STYLE);

report/src/main/java/org/hjug/refactorfirst/report/HtmlReport.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@Slf4j
1515
public class HtmlReport extends SimpleHtmlReport {
1616

17-
int d3Threshold = 700;
17+
int dotGraphThreshold = 4000;
1818

1919
// use Files.readString(Path.of(file))
2020
// Created by generative AI and modified slightly
@@ -279,10 +279,9 @@ public class HtmlReport extends SimpleHtmlReport {
279279
+ " </script>";
280280

281281
// Created by generative AI and modified
282-
public static final String POPUP_STYLE = "<style>\n" + " /* Popup container */\n"
282+
public static final String POPUP_STYLE = "<style>\n"
283283
+ " main {\n" + " max-width: 100vw;/*3840px;*/\n"
284284
+ " width: 100vw; /* or 100vw for viewport width */\n"
285-
+ " /*background-color: lightblue; *//* just for visibility */\n"
286285
+ " padding: 0px 0px; /* 0px top & bottom, 40px left & right */\n"
287286
+ " }\n"
288287
+ "\n"
@@ -301,6 +300,7 @@ public class HtmlReport extends SimpleHtmlReport {
301300
+ " width: 100%;\n"
302301
+ " height: 100%;\n"
303302
+ " }"
303+
+ " /* Popup container */\n"
304304
+ " .popup {\n"
305305
+ " position: fixed;\n"
306306
+ " display: none;\n"
@@ -388,24 +388,14 @@ public String printHead() {
388388
"<script async defer src=\"https://buttons.github.io/buttons.js\"></script>\n"
389389
// google chart import
390390
+ "<script type=\"text/javascript\" src=\"https://www.gstatic.com/charts/loader.js\"></script>\n"
391-
// d3 dot graph imports
392-
// + "<script src=\"https://d3js.org/d3.v5.min.js\"></script>\n"
393-
// + "<script
394-
// src=\"https://cdnjs.cloudflare.com/ajax/libs/d3-graphviz/3.0.5/d3-graphviz.min.js\"></script>\n"
395-
// + "<script
396-
// src=\"https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js\"></script>\n"
397-
398-
// + "<script
399-
// src=\"https://cdn.jsdelivr.net/npm/@hpcc-js/wasm-graphviz@1.7.0/dist/index.min.js\"></script>\n"
391+
// graphing imports - sigma, graphology, vizdom
400392
+ "<script src=\"https://cdn.jsdelivr.net/npm/svg-pan-zoom@3.6.1/dist/svg-pan-zoom.min.js\"></script>"
401-
402-
// sigma graph imports - sigma, graphology, graphlib, and graphlib-dot
403393
+ "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/sigma.js/2.4.0/sigma.min.js\"></script>\n"
404394
+ "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/graphology/0.25.4/graphology.umd.min.js\"></script>\n"
405-
// may only need graphlib-dot
406-
+ "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/graphlib/2.1.8/graphlib.min.js\"></script>\n"
407395
+ "<script src=\"https://cdn.jsdelivr.net/npm/graphlib-dot@0.6.4/dist/graphlib-dot.min.js\"></script>\n"
408396
+ "<script src=\"https://cdn.jsdelivr.net/npm/3d-force-graph\"></script>\n"
397+
+ "<script type=\"module\" src=\"https://cdn.jsdelivr.net/npm/@vizdom/vizdom-ts-web@0.1.19/vizdom_ts.min.js\"></script>\n"
398+
// Make the output look decent. Don't use in RefactorFirstMavenReport.
409399
+ "<link rel=\"stylesheet\" href=\"https://unpkg.com/mvp.css\">\n";
410400
}
411401

@@ -489,7 +479,7 @@ public String renderClassGraphVisuals() {
489479
int relationshipCount = classGraph.edgeSet().size();
490480
stringBuilder.append("<div align=\"center\">Number of classes: " + classCount + " Number of relationships: "
491481
+ relationshipCount + "<br></div>");
492-
if (classCount + relationshipCount < d3Threshold) {
482+
if (classCount + relationshipCount < dotGraphThreshold) {
493483
stringBuilder.append(generateDotImage(classGraphName));
494484
} else {
495485
// revisit and add DOT SVG popup button
@@ -519,15 +509,18 @@ private StringBuilder generateGraphButtons(String graphName, String dot) {
519509
private static String generateDotImage(String graphName) {
520510
// revisit and add D3 popup button as well
521511
return "<div id=\"" + graphName
522-
+ "\" style=\"width: 95%; margin: auto; border: thin solid black\"></div>\n"
512+
+ "\" style=\"width: 95%; height: 70vh; margin: auto; border: thin solid black\"></div>\n"
523513
+ "<script type=\"module\">\n"
524-
+ "import { Graphviz } from \"https://cdn.jsdelivr.net/npm/@hpcc-js/wasm/dist/index.js\";\n"
525-
+ " if (Graphviz) {\n"
526-
+ " const graphviz = await Graphviz.load();\n"
527-
+ " let svg = graphviz.layout("
528-
+ graphName + "_dot, \"svg\", \"dot\");\n"
529-
+ " // Set desired width and height\n"
530-
+ "\n"
514+
+ "import init, { DotParser } from \"https://esm.run/@vizdom/vizdom-ts-web\";\n"
515+
+ " if(DotParser) {\n"
516+
+ " // Wait for the WASM binary to be compiled and the 'wasm' object to be populated\n"
517+
+ " await init();\n"
518+
+ " // Create a new Dot Parser\n"
519+
+ " const parser = new DotParser();\n"
520+
+ " const dotGraph = parser.parse("
521+
+ graphName + "_dot);\n" + " const directedGraph = dotGraph.to_directed();\n"
522+
+ " const positioned = directedGraph.layout();\n"
523+
+ " let svg = positioned.to_svg().to_string();\n"
531524
+ " // Modify the SVG string to include width and height attributes\n"
532525
+ " svg = svg.replace('<svg ', `<svg class=\"fullscreen-svg\"`);\n"
533526
+ "\n"
@@ -637,7 +630,7 @@ public String renderCycleVisuals(RankedCycle cycle) {
637630
StringBuilder stringBuilder = new StringBuilder();
638631
stringBuilder.append(generateGraphButtons(cycleName, dot));
639632

640-
if (cycle.getCycleNodes().size() + cycle.getEdgeSet().size() < d3Threshold) {
633+
if (cycle.getCycleNodes().size() + cycle.getEdgeSet().size() < dotGraphThreshold) {
641634
stringBuilder.append(generateDotImage(cycleName));
642635
} else {
643636
// revisit and add DOT SVG popup button

report/src/main/java/org/hjug/refactorfirst/report/SimpleHtmlReport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ public String renderDisharmonyInfo(
744744
// simplify the method signature to just the name and type
745745
sig = getSimpleMethodSignature(sig);
746746
}
747-
sb.append(drawTableCell(sig != null ? sig : ""));
747+
sb.append(drawTableCell(sig != null ? sig.replace("<", "&lt;").replace(">", "&gt;") : ""));
748748
}
749749
sb.append(drawTableCell(rd.getPriority().toString()));
750750
if (showDetails) {

0 commit comments

Comments
 (0)