forked from neo4j-contrib/spatial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neo4j-contrib#314 from jjaderberg/initialfixforgra…
…phviz Override dot block generation
- Loading branch information
Showing
3 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/test/java/org/neo4j/doc/tools/SpatialGraphVizHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |