Skip to content

Commit 80725e1

Browse files
committed
Add horizontal and vertical lines.
1 parent b4e3525 commit 80725e1

File tree

5 files changed

+105
-13
lines changed

5 files changed

+105
-13
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.ruivieira</groupId>
88
<artifactId>java-plotlib</artifactId>
9-
<version>0.0.8</version>
9+
<version>0.0.9</version>
1010

1111
<build>
1212
<plugins>
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.ruivieira.plotlib;
22

3-
import org.ruivieira.plotlib.plots.ScatterPlot;
3+
import org.pmw.tinylog.Configurator;
4+
import org.pmw.tinylog.Level;
5+
import org.pmw.tinylog.writers.ConsoleWriter;
6+
import org.ruivieira.plotlib.plots.Histogram;
47

5-
import javax.imageio.ImageIO;
68
import java.io.IOException;
79
import java.util.Arrays;
810

@@ -11,18 +13,17 @@ public class Test {
1113

1214
public static void main(String[] args) throws IOException {
1315

16+
Configurator.defaultConfig()
17+
.writer(new ConsoleWriter())
18+
.level(Level.DEBUG)
19+
.activate();
20+
1421
Figure figure = new Figure();
1522

16-
Integer[] x = new Integer[]{1, 2, 3, 4};
17-
Integer[] y = new Integer[]{7, 9, 5, 6};
23+
Double[] x = new Double[]{1.0, 2.0, 3.0, 4.0};
1824

19-
figure.add(new ScatterPlot<>(Arrays.asList(x), Arrays.asList(y)));
25+
figure.add(new Histogram<>(Arrays.asList(x)).setBins(200));
2026

2127
System.out.println(figure.getBufferedImage());
22-
23-
String[] readers = ImageIO.getReaderFormatNames();
24-
for (String reader : readers)
25-
System.out.println("reader: " + reader);
26-
}
27-
28+
}
2829
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.ruivieira.plotlib.plots;
2+
3+
import org.ruivieira.plotlib.Arguments;
4+
import org.ruivieira.plotlib.Plot;
5+
import org.ruivieira.plotlib.interfaces.Colour;
6+
import org.ruivieira.plotlib.interfaces.LineStyle;
7+
8+
import java.util.Optional;
9+
10+
public class HorizontalLine<T> implements Plot,
11+
LineStyle<HorizontalLine>,
12+
Colour<HorizontalLine> {
13+
14+
private final T y;
15+
private Optional<String> linestyle = Optional.empty();
16+
private Optional<String> colour = Optional.empty();
17+
18+
public HorizontalLine(T y) {
19+
this.y = y;
20+
}
21+
22+
@Override
23+
public String render() {
24+
StringBuilder script = new StringBuilder();
25+
script.append("plt.axhline(y=")
26+
.append(y);
27+
linestyle.ifPresent(s -> script.append(", ").append(Arguments.build("linestyle", s)));
28+
colour.ifPresent(s -> script.append(", color='").append(s).append("'"));
29+
script.append(")\n");
30+
return script.toString();
31+
}
32+
33+
@Override
34+
public HorizontalLine setLineStyle(String style) {
35+
this.linestyle = Optional.of(style);
36+
return this;
37+
38+
}
39+
40+
@Override
41+
public HorizontalLine setColour(String colour) {
42+
this.colour = Optional.of(colour);
43+
return this;
44+
}
45+
}

src/main/java/org/ruivieira/plotlib/plots/LinePlot.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,5 @@ public String render() {
5454
public LinePlot<T, U> setColour(String colour) {
5555
this.colour = Optional.of(colour);
5656
return this;
57-
5857
}
5958
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.ruivieira.plotlib.plots;
2+
3+
import org.ruivieira.plotlib.Arguments;
4+
import org.ruivieira.plotlib.Plot;
5+
import org.ruivieira.plotlib.interfaces.Colour;
6+
import org.ruivieira.plotlib.interfaces.LineStyle;
7+
8+
import java.util.Optional;
9+
10+
public class VerticalLine<T> implements Plot,
11+
LineStyle<VerticalLine>,
12+
Colour<VerticalLine> {
13+
14+
private final T x;
15+
private Optional<String> linestyle = Optional.empty();
16+
private Optional<String> colour = Optional.empty();
17+
18+
19+
public VerticalLine(T x) {
20+
this.x = x;
21+
}
22+
23+
@Override
24+
public String render() {
25+
StringBuilder script = new StringBuilder();
26+
script.append("plt.axvline(x=")
27+
.append(x);
28+
linestyle.ifPresent(s -> script.append(", ").append(Arguments.build("linestyle", s)));
29+
colour.ifPresent(s -> script.append(", color='").append(s).append("'"));
30+
script.append(")\n");
31+
return script.toString();
32+
}
33+
34+
@Override
35+
public VerticalLine setLineStyle(String style) {
36+
this.linestyle = Optional.of(style);
37+
return this;
38+
39+
}
40+
41+
@Override
42+
public VerticalLine setColour(String colour) {
43+
this.colour = Optional.of(colour);
44+
return this;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)