Commit 39e92a16 authored by Rui Vieira's avatar Rui Vieira

Add marker options to ScatterPlot

parent fb296d44
......@@ -21,3 +21,7 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
*.iml
*.ipr
*.iws
\ No newline at end of file
......@@ -20,5 +20,14 @@
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package org.ruivieira.plotlib;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import static org.apache.commons.io.FileUtils.writeStringToFile;
public class Figure {
public StringBuilder script = new StringBuilder();
......@@ -12,7 +18,18 @@ public class Figure {
script.append(plot.render());
}
public void save() {
System.out.println(script.toString());
public void save(String imageName) {
try {
File tempFile = File.createTempFile("java-plotlib-", ".py");
System.out.println(tempFile.getAbsolutePath());
System.out.println(tempFile.getName());
script.append("\n").append("plt.savefig('").append(imageName).append("')");
writeStringToFile(tempFile, script.toString(), Charset.defaultCharset());
Process p = Runtime.getRuntime().exec("/usr/local/bin/python3 " + tempFile.getAbsolutePath());
System.out.println(p.getOutputStream().toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
package org.ruivieira.plotlib;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public class ScatterPlot implements Plot {
public class ScatterPlot<T, U> implements Plot {
List<String> xs = new ArrayList<>();
List<String> ys = new ArrayList<>();
private Optional<String> marker = Optional.empty();
public ScatterPlot() {
public ScatterPlot(T[] x, U[] y) {
for (T _x : x) {
xs.add(String.valueOf(_x));
}
for (U _y : y) {
ys.add(String.valueOf(_y));
}
}
public String render() {
return "plt.plot([" + String.join(",", xs) + "], [" + String.join(",", ys) + "])\n";
}
public static ScatterPlot create(int[] x, int[] y) {
List<String> xs = new ArrayList<>();
for (int _x : x) {
public ScatterPlot(Collection<T> x, Collection<U> y) {
for (T _x : x) {
xs.add(String.valueOf(_x));
}
List<String> ys = new ArrayList<>();
for (int _y : y) {
for (U _y : y) {
ys.add(String.valueOf(_y));
}
ScatterPlot plot = new ScatterPlot();
plot.xs = xs;
plot.ys = ys;
return plot;
}
public ScatterPlot setMarker(String marker) {
this.marker = Optional.of(marker);
return this;
}
public String render() {
final StringBuilder rendered = new StringBuilder();
rendered.append("plt.scatter([").append(String.join(",", xs));
rendered.append("], [");
rendered.append(String.join(",", ys));
rendered.append("]");
marker.ifPresent(s -> rendered.append(", marker='").append(s).append("'"));
rendered.append(")\n");
System.out.println(rendered.toString());
return rendered.toString();
}
}
......@@ -6,12 +6,12 @@ public class Test {
Figure figure = new Figure();
int[] x = new int[]{1, 2, 3, 4};
int[] y = new int[]{7, 9, 5, 6};
Integer[] x = new Integer[]{1, 2, 3, 4};
Integer[] y = new Integer[]{7, 9, 5, 6};
figure.add(ScatterPlot.create(x, y));
figure.add(new ScatterPlot<>(x, y));
figure.save();
// figure.save();
}
}
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