Skip to content

Commit 52bf598

Browse files
committed
make SingleAttributes public and improve README.md for attributes
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 7aebbb3 commit 52bf598

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

README.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,26 @@ The basic usage is as follows (assuming `import static guru.nidi.graphviz.model.
101101
```java
102102
Graph g = graph("example1").directed()
103103
.graphAttr().with(Rank.dir(LEFT_TO_RIGHT))
104+
.nodeAttr().with(Font.name("arial"))
105+
.linkAttr().with("class", "link-class")
104106
.with(
105107
node("a").with(Color.RED).link(node("b")),
106-
node("b").link(to(node("c")).with(Style.DASHED))
108+
node("b").link(
109+
to(node("c")).with(attr("weight", 5), Style.DASHED)
110+
)
107111
);
108112
Graphviz.fromGraph(g).height(100).render(Format.PNG).toFile(new File("example/ex1.png"));
109113
```
110114
[//]: # (end)
111115
<img src="https://rawgit.com/nidi3/graphviz-java/master/graphviz-java/example/ex1.png" height="100">
112116

113-
Global attributes are set using the `graphAttr`, `linkAttr` and `nodeAttr` methods.
114-
Nodes are styled using the `with` method.
115-
To style edges, use the static method `to` which returns a `Link` that also has a `with` method.
116-
The `with` method accepts predefined attributes like `Style`, `Arrow` or `Shape`
117+
- Global attributes are set using the `graphAttr`, `linkAttr` and `nodeAttr` methods.
118+
- Nodes are styled using the `with` method.
119+
- To style edges, use the static method `to` which returns a `Link` that also has a `with` method.
120+
- The `with` method accepts predefined attributes like `Style`, `Arrow` or `Shape`
117121
as well as everything defined in the [Graphviz reference](https://graphviz.gitlab.io/_pages/doc/info/attrs.html)
118-
e.g. `with("weight", 5)`
122+
e.g. `with("weight", 5)` or even arbitrary custom attributes.
123+
- Custom attribute classes can be defined by extending `SingleAttributes` or `MapAttributes`.
119124

120125
**Attention:** `Node a = node("a"); a.with(Color.RED);` Is not working as it might be expected.
121126
All "mutating" methods like `with` on nodes, links and graphs create new objects and leave the original object unchanged.
@@ -132,6 +137,11 @@ Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1
132137
[//]: # (end)
133138
<img src="https://rawgit.com/nidi3/graphviz-java/master/graphviz-java/example/ex1m.png" width="100">
134139

140+
The mutable API provides similar functions as the immutable one with slightly different syntax:
141+
- `mutGraph` instead of `graph`, `mutNode` instead of `node`
142+
- use setters: `setDirected` instead of `directed`
143+
- `add` instead of `width`
144+
135145
### Imperative
136146
There is a third possibility to use the API, based on the mutable version.
137147
Its form is closer to the way dot files are written.
@@ -380,7 +390,7 @@ Graphviz g = Graphviz.fromGraph(graph)
380390
SvgElementFinder.use(svg, finder -> {
381391
finder.findNode("unicorn").setAttribute("class", "pink");
382392
})));
383-
g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex9.png"));
393+
g.render(Format.PNG).toFile(new File("example/ex9.png"));
384394
```
385395
[//]: # (end)
386396
<img src="https://rawgit.com/nidi3/graphviz-java/master/graphviz-java/example/ex9.png" width="200">

graphviz-java/example/ex1.png

97 Bytes
Loading

graphviz-java/example/ex9.png

-102 Bytes
Loading

graphviz-java/src/main/java/guru/nidi/graphviz/attribute/SingleAttributes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.lang.reflect.Constructor;
1919
import java.lang.reflect.ParameterizedType;
2020

21-
class SingleAttributes<T, F extends For> implements Attributes<F> {
21+
public class SingleAttributes<T, F extends For> implements Attributes<F> {
2222
protected final String key;
2323
public final T value;
2424

graphviz-java/src/test/java/guru/nidi/graphviz/model/ReadmeTest.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.awt.image.BufferedImage;
2525
import java.io.*;
2626

27+
import static guru.nidi.graphviz.attribute.Attributes.attr;
2728
import static guru.nidi.graphviz.attribute.Label.Justification.LEFT;
2829
import static guru.nidi.graphviz.attribute.Rank.RankDir.LEFT_TO_RIGHT;
2930
import static guru.nidi.graphviz.attribute.Records.rec;
@@ -48,12 +49,16 @@ void ex1() throws IOException {
4849
//## basic
4950
Graph g = graph("example1").directed()
5051
.graphAttr().with(Rank.dir(LEFT_TO_RIGHT))
52+
.nodeAttr().with(Font.name("arial"))
53+
.linkAttr().with("class", "link-class")
5154
.with(
5255
node("a").with(Color.RED).link(node("b")),
53-
node("b").link(to(node("c")).with(Style.DASHED))
56+
node("b").link(
57+
to(node("c")).with(attr("weight", 5), Style.DASHED)
58+
)
5459
);
5560
Graphviz.fromGraph(g).height(100).render(Format.PNG).toFile(new File("example/ex1.png"));
56-
//## end
61+
//##
5762
}
5863

5964
@Test
@@ -62,7 +67,7 @@ void ex1m() throws IOException {
6267
MutableGraph g = mutGraph("example1").setDirected(true).add(
6368
mutNode("a").add(Color.RED).addLink(mutNode("b")));
6469
Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1m.png"));
65-
//## end
70+
//##
6671
}
6772

6873
@Test
@@ -74,7 +79,7 @@ void ex1i() throws IOException {
7479
mutNode("a").addLink(mutNode("b"));
7580
});
7681
Graphviz.fromGraph(g).width(200).render(Format.PNG).toFile(new File("example/ex1i.png"));
77-
//## end
82+
//##
7883
}
7984

8085
@Test
@@ -100,7 +105,7 @@ void ex2() throws IOException {
100105
init.link(mkString));
101106

102107
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("example/ex2.png"));
103-
//## end
108+
//##
104109
}
105110

106111
@Test
@@ -127,7 +132,7 @@ void ex3() throws IOException {
127132
node2.link(between(port("p"), node6.port(NORTH_WEST))),
128133
node4.link(between(port("p"), node7.port(SOUTH_WEST))));
129134
Graphviz.fromGraph(g).width(900).render(Format.PNG).toFile(new File("example/ex3.png"));
130-
//## end
135+
//##
131136
}
132137

133138
@Test
@@ -146,7 +151,7 @@ void ex4() throws IOException {
146151
Style.lineWidth(4).and(Style.FILLED)));
147152
Graphviz.fromGraph(g).width(700).render(Format.PNG).toFile(new File("example/ex4-2.png"));
148153
}
149-
//## end
154+
//##
150155
}
151156

152157
@Test
@@ -163,7 +168,7 @@ void ex5() throws IOException {
163168
String dot = viz.render(Format.DOT).toString();
164169
String json = viz.engine(Engine.NEATO).render(Format.JSON).toString();
165170
BufferedImage image = viz.render(Format.PNG).toImage();
166-
//## end
171+
//##
167172
end();
168173
init();
169174
}
@@ -176,7 +181,7 @@ void ex7() throws IOException {
176181
Graphviz g = Graphviz.fromGraph(graph()
177182
.with(node(Label.html("<table border='0'><tr><td><img src='graphviz.png' /></td></tr></table>"))));
178183
g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex7.png"));
179-
//## img
184+
//##
180185
}
181186

182187
@Test
@@ -185,7 +190,7 @@ void ex8() throws IOException {
185190
Graphviz g = Graphviz.fromGraph(graph()
186191
.with(node(" ").with(Size.std().margin(.8, .7), Image.of("graphviz.png"))));
187192
g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex8.png"));
188-
//## image
193+
//##
189194
}
190195

191196
@Test
@@ -199,7 +204,7 @@ void ex9() throws IOException {
199204
SvgElementFinder.use(svg, finder -> {
200205
finder.findNode("unicorn").setAttribute("class", "pink");
201206
})));
202-
g.basedir(new File("example")).render(Format.PNG).toFile(new File("example/ex9.png"));
203-
//## image
207+
g.render(Format.PNG).toFile(new File("example/ex9.png"));
208+
//##
204209
}
205210
}

0 commit comments

Comments
 (0)