Commit 80725e11 authored by Rui Vieira's avatar Rui Vieira

Add horizontal and vertical lines.

parent b4e35250
......@@ -6,7 +6,7 @@
<groupId>org.ruivieira</groupId>
<artifactId>java-plotlib</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
<build>
<plugins>
......
package org.ruivieira.plotlib;
import org.ruivieira.plotlib.plots.ScatterPlot;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Level;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.ruivieira.plotlib.plots.Histogram;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Arrays;
......@@ -11,18 +13,17 @@ public class Test {
public static void main(String[] args) throws IOException {
Configurator.defaultConfig()
.writer(new ConsoleWriter())
.level(Level.DEBUG)
.activate();
Figure figure = new Figure();
Integer[] x = new Integer[]{1, 2, 3, 4};
Integer[] y = new Integer[]{7, 9, 5, 6};
Double[] x = new Double[]{1.0, 2.0, 3.0, 4.0};
figure.add(new ScatterPlot<>(Arrays.asList(x), Arrays.asList(y)));
figure.add(new Histogram<>(Arrays.asList(x)).setBins(200));
System.out.println(figure.getBufferedImage());
String[] readers = ImageIO.getReaderFormatNames();
for (String reader : readers)
System.out.println("reader: " + reader);
}
}
}
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot;
import org.ruivieira.plotlib.interfaces.Colour;
import org.ruivieira.plotlib.interfaces.LineStyle;
import java.util.Optional;
public class HorizontalLine<T> implements Plot,
LineStyle<HorizontalLine>,
Colour<HorizontalLine> {
private final T y;
private Optional<String> linestyle = Optional.empty();
private Optional<String> colour = Optional.empty();
public HorizontalLine(T y) {
this.y = y;
}
@Override
public String render() {
StringBuilder script = new StringBuilder();
script.append("plt.axhline(y=")
.append(y);
linestyle.ifPresent(s -> script.append(", ").append(Arguments.build("linestyle", s)));
colour.ifPresent(s -> script.append(", color='").append(s).append("'"));
script.append(")\n");
return script.toString();
}
@Override
public HorizontalLine setLineStyle(String style) {
this.linestyle = Optional.of(style);
return this;
}
@Override
public HorizontalLine setColour(String colour) {
this.colour = Optional.of(colour);
return this;
}
}
......@@ -54,6 +54,5 @@ public class LinePlot<T, U> extends BinaryAbstractPlot<T, U> implements Plot, Ma
public LinePlot<T, U> setColour(String colour) {
this.colour = Optional.of(colour);
return this;
}
}
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot;
import org.ruivieira.plotlib.interfaces.Colour;
import org.ruivieira.plotlib.interfaces.LineStyle;
import java.util.Optional;
public class VerticalLine<T> implements Plot,
LineStyle<VerticalLine>,
Colour<VerticalLine> {
private final T x;
private Optional<String> linestyle = Optional.empty();
private Optional<String> colour = Optional.empty();
public VerticalLine(T x) {
this.x = x;
}
@Override
public String render() {
StringBuilder script = new StringBuilder();
script.append("plt.axvline(x=")
.append(x);
linestyle.ifPresent(s -> script.append(", ").append(Arguments.build("linestyle", s)));
colour.ifPresent(s -> script.append(", color='").append(s).append("'"));
script.append(")\n");
return script.toString();
}
@Override
public VerticalLine setLineStyle(String style) {
this.linestyle = Optional.of(style);
return this;
}
@Override
public VerticalLine setColour(String colour) {
this.colour = Optional.of(colour);
return this;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment