Commit 0c2e252f authored by Rui Vieira's avatar Rui Vieira

Add histogram bins.

parent 41c31e0f
......@@ -6,7 +6,7 @@
<groupId>org.ruivieira</groupId>
<artifactId>java-plotlib</artifactId>
<version>0.0.7</version>
<version>0.0.8</version>
<build>
<plugins>
......@@ -49,6 +49,11 @@
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog</artifactId>
<version>1.3.6</version>
</dependency>
</dependencies>
<profiles>
......
......@@ -7,4 +7,12 @@ public class Arguments {
builder.append(key).append("=").append("'").append(value).append("'");
return builder.toString();
}
public static String build(String key, int value) {
final StringBuilder builder = new StringBuilder();
builder.append(key).append("=").append(value);
return builder.toString();
}
}
......@@ -3,6 +3,7 @@ package org.ruivieira.plotlib;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class Converter<T> {
......@@ -15,13 +16,11 @@ public class Converter<T> {
public Converter(Collection<T> data) {
this.converted = data.stream().map(String::valueOf).collect(Collectors.toList());
this.converted = data.stream().map(Objects::toString).collect(Collectors.toList());
}
public String getConvertedList() {
final StringBuilder builder = new StringBuilder();
builder.append("[").append(String.join(",", getConverted())).append("]");
return builder.toString();
return "[" + String.join(",", getConverted()) + "]";
}
}
package org.ruivieira.plotlib;
import org.pmw.tinylog.Logger;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Optional;
import static org.apache.commons.io.FileUtils.writeStringToFile;
public class Figure {
......@@ -38,6 +41,8 @@ public class Figure {
title.ifPresent(s -> script.append("plt.title('").append(s).append("')\n"));
script.append("\n").append("plt.savefig('").append(imageName).append("', format='png', transparent=False)");
writeStringToFile(tempFile, script.toString(), Charset.defaultCharset());
Logger.debug("Saving temporary script to {}", tempFile.getAbsolutePath());
Logger.debug("Saving temporary image to {}", imageName);
Process p = runtime.exec(python + " " + tempFile.getAbsolutePath());
p.waitFor();
......@@ -53,7 +58,6 @@ public class Figure {
save(tempFile.getAbsolutePath());
File savedImage = new File(tempFile.getAbsolutePath());
FileInputStream fis = new FileInputStream(savedImage);
System.out.println(savedImage);
return ImageIO.read(fis);
}
......
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot;
import org.ruivieira.plotlib.UnaryAbstractPlot;
import java.util.Collection;
import java.util.Optional;
public class Histogram<T> extends UnaryAbstractPlot<T> implements Plot {
private Optional<Integer> bins = Optional.empty();
public Histogram(Collection<T> x) {
super(x);
}
......@@ -16,8 +20,14 @@ public class Histogram<T> extends UnaryAbstractPlot<T> implements Plot {
script.append("plt.hist(")
.append(xs.getConvertedList());
bins.ifPresent(s -> script.append(", ").append(Arguments.build("bins", s)));
script.append(")\n");
return script.toString();
}
public Histogram<T> setBins(Integer bins) {
this.bins = Optional.of(bins);
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