Skip to content

Commit

Permalink
Merge pull request neo4j-contrib#314 from jjaderberg/initialfixforgra…
Browse files Browse the repository at this point in the history
…phviz

Override dot block generation
  • Loading branch information
craigtaverner authored Jul 18, 2017
2 parents 15817d9 + 3fef3f8 commit 68afa47
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 1 deletion.
101 changes: 101 additions & 0 deletions src/test/java/org/neo4j/doc/tools/GraphVizConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.doc.tools;

public class GraphVizConfig {

String nodeFontSize = "10";
String edgeFontSize = nodeFontSize;
String nodeFontColor = "#1c2021"; // darker grey
String edgeFontColor = nodeFontColor;
String edgeColor = "#2e3436"; // dark grey
String boxColor = edgeColor;
String edgeHighlight = "#a40000"; // dark red
String nodeFillColor = "#ffffff";
String nodeHighlight = "#fcee7d"; // lighter yellow
String nodeHighlight2 = "#fcc574"; // lighter orange
String nodeShape = "box";

// Commandline args in the shell script, in order
String fontPath;
// We are not calling the dot command from here at the moment, so we don't need the filename
// String targetImage;
String colorSet;
String graphAttrs;

String graphSettings = "graph [size=\"7.0,9.0\" fontpath=\"" + fontPath + "\"]";
String nodeStyle = "filled,rounded";
String nodeSep = "0.4";

String textNode = "shape=plaintext,style=diagonals,height=0.2,margin=0.0,0.0";

String arrowHead = "vee";
String arrowSize = "0.75";

String inData;

String graphFont = "FreeSans";
String nodeFont = graphFont;
String edgeFont = graphFont;

public GraphVizConfig(String inData, String fontPath, String colorSet, String graphAttrs) {
this.fontPath = fontPath;
this.colorSet = colorSet;
this.graphAttrs = graphAttrs;

handleColorSet(colorSet);

this.inData = handleInData(inData);
}

public String get() {
String prepend = String.format("digraph g{ %s ", graphSettings) +
String.format("node [shape=\"%s\" penwidth=1.5 fillcolor=\"%s\" color=\"%s\" ", nodeShape, nodeFillColor, boxColor) +
String.format("fontcolor=\"%s\" style=\"%s\" fontsize=%s fontname=\"%s\"] ", nodeFontColor, nodeStyle, nodeFontSize, nodeFont) +
String.format("edge [color=\"%s\" penwidth=2 arrowhead=\"%s\" arrowtail=\"%s\" ", boxColor, arrowHead, arrowHead) +
String.format("arrowSize=%s fontcolor=\"%s\" fontsize=%s fontname=\"%s\"] ", arrowSize, edgeFontColor, edgeFontSize, edgeFont) +
String.format("nodesep=%s fontname=\"%s\"", nodeSep, graphFont) +
graphAttrs;

return prepend + this.inData + "}";
}

private final void handleColorSet(String colorSet) {
if (colorSet.equals("meta")) {
this.nodeFillColor = "#fadcad";
this.nodeHighlight = "#a8e270";
this.nodeHighlight2 = "#95bbe3";
} else if (colorSet.equals("neoviz")) {
this.nodeShape = "Mrecord";
this.nodeFontSize = "8";
this.edgeFontSize = this.nodeFontSize;
}
}

private final String handleInData(String in) {
return in.replace("NODEHIGHLIGHT", nodeHighlight)
.replace("NODE2HIGHLIGHT", nodeHighlight2)
.replace("EDGEHIGHLIGHT", edgeHighlight)
.replace("BOXCOLOR", boxColor)
.replace("TEXTNODE", textNode);
}


}
2 changes: 1 addition & 1 deletion src/test/java/org/neo4j/doc/tools/RESTDocsGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ protected void document( final DocumentationData data )
{
fw.append( org.neo4j.doc.tools.AsciiDocGenerator.dumpToSeparateFile( dir,
name + ".graph",
AsciidocHelper.createGraphVizWithNodeId( "Final Graph",
SpatialGraphVizHelper.createGraphVizWithNodeId( "Final Graph",
graph, title ) ) );
line(fw, "" );
}
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/org/neo4j/doc/tools/SpatialGraphVizHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.doc.tools;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.visualization.graphviz.AsciiDocStyle;
import org.neo4j.visualization.graphviz.GraphStyle;
import org.neo4j.visualization.graphviz.GraphvizWriter;
import org.neo4j.walk.Walker;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class SpatialGraphVizHelper extends org.neo4j.visualization.asciidoc.AsciidocHelper {

private static final String ILLEGAL_STRINGS = "[:\\(\\)\t;&/\\\\]";

public static String createGraphVizWithNodeId(
String title, GraphDatabaseService graph, String identifier
) {
return createGraphViz(
title, graph, identifier, AsciiDocStyle.withAutomaticRelationshipTypeColors(), ""
);
}

public static String createGraphViz(String title, GraphDatabaseService graph, String identifier, GraphStyle graphStyle, String graphvizOptions) {
try (Transaction tx = graph.beginTx()) {
GraphvizWriter writer = new GraphvizWriter( graphStyle );
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
writer.emit( out, Walker.fullGraph( graph ) );
} catch (IOException e) {
e.printStackTrace();
}

String safeTitle = title.replaceAll( ILLEGAL_STRINGS, "" );

tx.success();

String fontsDir = "target/tools/bin/fonts";
String colorSet = "neoviz";
String graphAttrs = "";

try {
String result = "." + title + "\n[graphviz, "
+ (safeTitle + "-" + identifier).replace( " ", "-" )
+ ", svg]\n"
+ "----\n" +
new GraphVizConfig(
out.toString( StandardCharsets.UTF_8.name()),
fontsDir,
colorSet, graphAttrs
).get() + "\n" +
"----\n";
System.out.println(result);
return result;
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}
}

0 comments on commit 68afa47

Please sign in to comment.