Permalink
Browse files

Merge pull request #655 from nawroth/cypherfixes

Better looking graphs
  • Loading branch information...
systay committed Jun 29, 2012
2 parents 19d21a4 + ac517a6 commit b632e23ed32cefff4d12fa98aae5d4b9d801f230
@@ -123,9 +123,6 @@ include::calculating-the-clustering-coefficient-of-a-friend-network.txt[]
This section is showing how to create some of the http://en.wikipedia.org/wiki/Gallery_of_named_graphs[named pretty graphs on Wikipedia].
-The graph visualizations look best using a Force Directed Layout, while the current layout below is different. For better results,
-use the live demos in http://docs.neo4j.org/chunked/{neo4j-version}/cypher-cookbook-pretty-graphs.html[the online version of this page].
-
:leveloffset: 2
include::star-graph.txt[]
@@ -166,12 +166,17 @@ abstract class ArticleTest extends Assertions with DocumentationHelper {
private def includeGraphviz(startText: String, dir: File):String = {
val graphVizLine = "include::" + graphvizFileName + "[]"
- val txt = startText.replaceAllLiterally("###graph-image###", graphVizLine)
- if (txt != startText) {
- dumpGraphViz(dir)
+ val regex = "###graph-image(.*?)###".r
+ regex.findFirstMatchIn(startText) match {
+ case None => startText
+ case Some(options) =>
+ val optionString = options.group(1)
+ val txt = startText.replaceAllLiterally("###graph-image" + optionString + "###", graphVizLine)
+ if (txt != startText) {
+ dumpGraphViz(dir, optionString.trim)
+ }
+ txt
}
-
- txt
}
private def includeQueries(query: String, dir: File) = {
@@ -56,15 +56,15 @@ trait DocumentationHelper {
val graphvizFileName = "cypher-" + this.getClass.getSimpleName.replaceAll("Test", "").toLowerCase + "-graph.txt"
- def dumpGraphViz(dir: File) {
+ def dumpGraphViz(dir: File, graphVizOptions:String) {
val graphViz = new PrintWriter(new File(dir, graphvizFileName), "UTF-8")
- val foo = emitGraphviz(graphvizFileName)
+ val foo = emitGraphviz(graphvizFileName, graphVizOptions)
graphViz.write(foo)
graphViz.flush()
graphViz.close()
}
- private def emitGraphviz(fileName:String): String = {
+ private def emitGraphviz(fileName:String, graphVizOptions:String): String = {
val out = new ByteArrayOutputStream()
val writer = new GraphvizWriter(getGraphvizStyle)
@@ -73,12 +73,12 @@ trait DocumentationHelper {
return """
_Graph_
-["dot", """" + fileName + """.svg", "neoviz"]
+["dot", "%s.svg", "neoviz", "%s"]
----
%s
----
-""".format(out)
+""".format(fileName, graphVizOptions, out)
}
protected def getGraphvizStyle: GraphStyle = AsciiDocStyle.withAutomaticRelationshipTypeColors()
@@ -94,7 +94,7 @@ abstract class DocumentingTestBase extends Assertions with DocumentationHelper {
val (dir: File, writer: PrintWriter) = createWriter(title, section)
dumpToFile(writer, title, query, returns, text, result)
- dumpGraphViz(dir)
+ dumpGraphViz(dir, graphvizOptions)
}
var db: GraphDatabaseService = null
@@ -105,7 +105,8 @@ abstract class DocumentingTestBase extends Assertions with DocumentationHelper {
var relIndex: Index[Relationship] = null
val properties: Map[String, Map[String, Any]] = Map()
var generateConsole: Boolean = true
- var generateInitialGraphForConsole: Boolean = true;
+ var generateInitialGraphForConsole: Boolean = true
+ val graphvizOptions: String = ""
def section: String
@@ -21,12 +21,16 @@ package org.neo4j.cypher.docgen.cookbook
import org.neo4j.cypher.docgen.ArticleTest
import org.neo4j.cypher.{ExecutionResult, StatisticsChecker}
+import org.neo4j.visualization.graphviz.GraphStyle
+import org.neo4j.visualization.graphviz.AsciiDocSimpleStyle
class LinkedListTest extends ArticleTest with StatisticsChecker {
val graphDescription = List("ROOT LINK A", "A LINK B", "B LINK C", "C LINK ROOT")
val section = "cookbook"
val title = "Linked List"
+ override protected def getGraphvizStyle: GraphStyle =
+ AsciiDocSimpleStyle.withAutomaticRelationshipTypeColors()
override def assert(name: String, result: ExecutionResult) {
name match {
@@ -62,7 +66,7 @@ to it self.
Something like this:
-###graph-image###
+###graph-image graph [rankdir=RL]###
To initialize an empty linked list, we simply create an empty node, and make it link to itself.
@@ -75,11 +79,12 @@ Adding values is done by finding the relationship where the new value should be
a new node, and two relationships to it.
###no-results assertion=add
-START root=node(%ROOT%)
+START root=node:node_auto_index(name = "ROOT")
MATCH root-[:LINK*0..]->before,// before could be same as root
after-[:LINK*0..]->root, // after could be same as root
before-[old:LINK]->after
-WHERE before.value? < 25 // This is the value. It would normally be supplied through a parameter
+WHERE before.value? < 25 // This is the value, which would normally
+ // be supplied through a parameter.
AND 25 < after.value?
CREATE before-[:LINK]->({value:25})-[:LINK]->after
DELETE old###
@@ -88,7 +93,7 @@ Deleting a value, conversely, is done by finding the node with the value, and th
from it, and replacing with a new value.
###no-results assertion=delete
-START root=node(%ROOT%)
+START root=node:node_auto_index(name = "ROOT")
MATCH root-[:LINK*0..]->before,
before-[delBefore:LINK]->del-[delAfter:LINK]->after,
after-[:LINK*0..]->root
@@ -42,10 +42,10 @@ class MultirelationalSocialNetworkTest extends DocumentingTestBase {
"Maria LIKES cars")
def section = "cookbook"
+ override val graphvizOptions = "graph [layout=circo]"
- override protected def getGraphvizStyle: GraphStyle = {
+ override protected def getGraphvizStyle: GraphStyle =
AsciiDocSimpleStyle.withAutomaticRelationshipTypeColors()
- }
@Test def followBack() {
testQuery(
@@ -27,6 +27,7 @@ class PrettyGraphsCompleteGraphTest extends DocumentingTestBase {
def graphDescription = List()
def section = "cookbook"
generateInitialGraphForConsole = false
+ override val graphvizOptions = "graph [layout=circo]"
@Test def completeGraph() {
@@ -27,6 +27,7 @@ class PrettyGraphsFriendshipGraphTest extends DocumentingTestBase {
def graphDescription = List()
def section = "cookbook"
generateInitialGraphForConsole = false
+ override val graphvizOptions = "graph [layout=neato]"
@Test def completeGraph() {
@@ -27,7 +27,7 @@ class PrettyGraphsStarTest extends DocumentingTestBase {
def graphDescription = List()
def section = "cookbook"
generateInitialGraphForConsole = false
-
+ override val graphvizOptions = "graph [layout=neato]"
@Test def completeGraph() {
testQuery(
@@ -27,6 +27,7 @@ class PrettyGraphsWheelTest extends DocumentingTestBase {
def graphDescription = List()
def section = "cookbook"
generateInitialGraphForConsole = false
+ override val graphvizOptions = "graph [layout=neato]"
@Test def completeGraph() {
testQuery(
@@ -49,27 +49,63 @@ public static String createGraphViz( String title,
AsciiDocSimpleStyle.withAutomaticRelationshipTypeColors() );
}
+ public static String createGraphViz( String title,
+ GraphDatabaseService graph, String identifier,
+ String graphvizOptions )
+ {
+ return createGraphViz( title, graph, identifier,
+ AsciiDocSimpleStyle.withAutomaticRelationshipTypeColors(),
+ graphvizOptions );
+ }
+
public static String createGraphVizWithNodeId( String title,
GraphDatabaseService graph, String identifier )
{
return createGraphViz( title, graph, identifier,
AsciiDocStyle.withAutomaticRelationshipTypeColors() );
}
+ public static String createGraphVizWithNodeId( String title,
+ GraphDatabaseService graph, String identifier,
+ String graphvizOptions )
+ {
+ return createGraphViz( title, graph, identifier,
+ AsciiDocStyle.withAutomaticRelationshipTypeColors(),
+ graphvizOptions );
+ }
+
public static String createGraphVizDeletingReferenceNode( String title,
GraphDatabaseService graph, String identifier )
+ {
+ return createGraphVizDeletingReferenceNode( title, graph, identifier,
+ "" );
+ }
+
+ public static String createGraphVizDeletingReferenceNode( String title,
+ GraphDatabaseService graph, String identifier,
+ String graphvizOptions )
{
removeReferenceNode( graph );
return createGraphViz( title, graph, identifier,
- AsciiDocSimpleStyle.withAutomaticRelationshipTypeColors() );
+ AsciiDocSimpleStyle.withAutomaticRelationshipTypeColors(),
+ graphvizOptions );
}
public static String createGraphVizWithNodeIdDeletingReferenceNode(
String title, GraphDatabaseService graph, String identifier )
+ {
+ return createGraphVizWithNodeIdDeletingReferenceNode( title, graph,
+ identifier, "" );
+ }
+
+ public static String createGraphVizWithNodeIdDeletingReferenceNode(
+ String title, GraphDatabaseService graph, String identifier,
+ String graphvizOptions )
{
removeReferenceNode( graph );
return createGraphViz( title, graph, identifier,
- AsciiDocStyle.withAutomaticRelationshipTypeColors() );
+ AsciiDocStyle.withAutomaticRelationshipTypeColors(),
+ graphvizOptions );
}
/**
@@ -80,6 +116,13 @@ public static String createGraphVizWithNodeIdDeletingReferenceNode(
*/
public static String createGraphViz( String title,
GraphDatabaseService graph, String identifier, GraphStyle graphStyle )
+ {
+ return createGraphViz( title, graph, identifier, graphStyle, "" );
+ }
+
+ public static String createGraphViz( String title,
+ GraphDatabaseService graph, String identifier,
+ GraphStyle graphStyle, String graphvizOptions )
{
GraphvizWriter writer = new GraphvizWriter( graphStyle );
OutputStream out = new ByteArrayOutputStream();
@@ -96,10 +139,8 @@ public static String createGraphViz( String title,
return "." + title + "\n[\"dot\", \""
+ ( safeTitle + "-" + identifier ).replace( " ", "-" )
- + ".svg\", \"neoviz\"]\n" +
- "----\n" +
- out.toString() +
- "----\n";
+ + ".svg\", \"neoviz\", \"" + graphvizOptions + "\"]\n"
+ + "----\n" + out.toString() + "----\n";
}
private static void removeReferenceNode( GraphDatabaseService graph )

0 comments on commit b632e23

Please sign in to comment.