use inline data for bar charts

Bar charts only need very little data. There is no need to create
a file for two numbers.
This commit is contained in:
2020-01-19 13:15:44 +01:00
parent 99e57cda24
commit 459c659f85
10 changed files with 66 additions and 31 deletions

View File

@@ -52,12 +52,15 @@ public abstract class AggregateHandler implements Appender {
abstract AxisSettings createYAxisSettings(GnuplotSettings settings, Collection<DataSeries> dataSeries); abstract AxisSettings createYAxisSettings(GnuplotSettings settings, Collection<DataSeries> dataSeries);
abstract void addPlot(StringBuilder result, CustomAggregator aggregator, LineStyle lineStyle, abstract String addPlot(CustomAggregator aggregator, LineStyle lineStyle, Optional<String> title);
Optional<String> title);
abstract CustomAggregator createCustomAggregator(Path tmpDir, PlotSettings plotSettings, long fromEpochMilli, abstract CustomAggregator createCustomAggregator(Path tmpDir, PlotSettings plotSettings, long fromEpochMilli,
long toEpochMilli); long toEpochMilli);
String beforePlot(final CustomAggregator aggregator) {
return "";
}
protected String gnuplotTitle(final Optional<String> title) { protected String gnuplotTitle(final Optional<String> title) {
return title.isPresent() ? "title '" + title.get() + "'" : "notitle"; return title.isPresent() ? "title '" + title.get() + "'" : "notitle";

View File

@@ -95,6 +95,9 @@ public class AggregateHandlerCollection {
public void addPlots(final StringBuilder result, final Collection<DataSeries> dataSeries) { public void addPlots(final StringBuilder result, final Collection<DataSeries> dataSeries) {
final StringBuilder plots = new StringBuilder();
final StringBuilder beforePlots = new StringBuilder();
boolean first = true; boolean first = true;
final List<AggregateHandler> handlersInPlottingOrder = CollectionUtils.copySort(aggregateHandlers, final List<AggregateHandler> handlersInPlottingOrder = CollectionUtils.copySort(aggregateHandlers,
PLOTTING_ORDER); PLOTTING_ORDER);
@@ -113,11 +116,20 @@ public class AggregateHandlerCollection {
index++; index++;
} }
handler.addPlot(result, aggregator, dataSerie.getStyle(), title); final String beforePlot = handler.beforePlot(aggregator);
beforePlots.append(beforePlot);
final String plot = handler.addPlot(aggregator, dataSerie.getStyle(), title);
plots.append(plot);
} }
} }
first = false; first = false;
} }
result.append(beforePlots);
result.append("\n");
result.append("plot ");
result.append(plots);
} }
} }

View File

@@ -15,4 +15,8 @@ public interface Appender {
builder.append(String.format(Locale.US, format, args)); builder.append(String.format(Locale.US, format, args));
} }
default String formatln(final String format, final Object... args) {
return String.format(Locale.US, format + "\n", args);
}
} }

View File

@@ -5,6 +5,7 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.UUID;
import org.lucares.pdb.api.RuntimeIOException; import org.lucares.pdb.api.RuntimeIOException;
@@ -16,6 +17,8 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
private Long index = null; private Long index = null;
private final String dataName = "$data" + UUID.randomUUID().toString().replace("-", "");
public BarChartAggregator(final Path tmpDir) { public BarChartAggregator(final Path tmpDir) {
super(); super();
this.tmpDir = tmpDir; this.tmpDir = tmpDir;
@@ -39,11 +42,7 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
count++; count++;
} }
@Override public String asCsv() {
public AggregatedData getAggregatedData() {
try {
final File dataFile = File.createTempFile("bar", ".dat", tmpDir.toFile());
final StringBuilder csv = new StringBuilder(); final StringBuilder csv = new StringBuilder();
csv.append(index + 0.5); csv.append(index + 0.5);
@@ -51,9 +50,17 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
csv.append(""); csv.append("");
csv.append(","); csv.append(",");
csv.append(count); csv.append(count);
csv.append("\n"); return csv.toString();
}
Files.writeString(dataFile.toPath(), csv.toString(), StandardCharsets.UTF_8); @Override
public AggregatedData getAggregatedData() {
try {
final File dataFile = File.createTempFile("bar", ".dat", tmpDir.toFile());
final String csv = asCsv();
Files.writeString(dataFile.toPath(), csv, StandardCharsets.UTF_8);
final AggregatedData result = new AggregatedData("label", dataFile); final AggregatedData result = new AggregatedData("label", dataFile);
return result; return result;
} catch (final IOException e) { } catch (final IOException e) {
@@ -66,4 +73,9 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
return Aggregate.BAR; return Aggregate.BAR;
} }
public String getDataName() {
return dataName;
}
} }

View File

@@ -68,19 +68,29 @@ public class BarChartHandler extends AggregateHandler {
} }
@Override @Override
void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle, String beforePlot(final CustomAggregator aggregator) {
final Optional<String> title) { final StringBuilder result = new StringBuilder();
final AggregatedData aggregatedData = aggregator.getAggregatedData(); final BarChartAggregator barAggregator = (BarChartAggregator) aggregator;
appendfln(result, "%s <<EOD", barAggregator.getDataName());
appendln(result, barAggregator.asCsv());
appendln(result, "EOD");
return result.toString();
}
@Override
String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
final BarChartAggregator barAggregator = (BarChartAggregator) aggregator;
/* /*
* appendfln(result, * appendfln(result,
* "'%s' using 1:3:xtic(2) notitle with %s axes %s fs solid %s, \\", // * "'%s' using 1:3:xtic(2) notitle with %s axes %s fs solid %s, \\", //
* aggregatedData.getDataFile(), // GnuplotLineType.Bar, // gnuplotXYAxis(), // * aggregatedData.getDataFile(), // GnuplotLineType.Bar, // gnuplotXYAxis(), //
* lineStyle// ); * lineStyle// );
*/ */
appendfln(result, "'%s' using 1:3:xtic(2) %s with %s axes %s fs solid %s, \\", // return formatln("'%s' using 1:3:xtic(2) %s with %s axes %s fs solid %s, \\", //
aggregatedData.getDataFile(), // barAggregator.getDataName(), //
gnuplotTitle(title), // gnuplotTitle(title), //
GnuplotLineType.Bar, // GnuplotLineType.Bar, //
gnuplotXYAxis(), // gnuplotXYAxis(), //

View File

@@ -69,12 +69,11 @@ public class CumulativeDistributionHandler extends AggregateHandler {
} }
@Override @Override
public void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle, public String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
final Optional<String> title) {
final AggregatedData aggregatedData = aggregator.getAggregatedData(); final AggregatedData aggregatedData = aggregator.getAggregatedData();
appendfln(result, "'%s' using 1:2 %s with lines axes %s lw 2 %s, \\", // return formatln("'%s' using 1:2 %s with lines axes %s lw 2 %s, \\", //
aggregatedData.getDataFile().getAbsolutePath(), // aggregatedData.getDataFile().getAbsolutePath(), //
gnuplotTitle(title), // gnuplotTitle(title), //
gnuplotXYAxis(), // gnuplotXYAxis(), //

View File

@@ -54,11 +54,10 @@ public class HistogramHandler extends AggregateHandler {
} }
@Override @Override
void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle, String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
final Optional<String> title) {
final AggregatedData aggregatedData = aggregator.getAggregatedData(); final AggregatedData aggregatedData = aggregator.getAggregatedData();
appendfln(result, "'%s' using 1:2 %s with boxes axes %s lw 1 %s, \\", // return formatln("'%s' using 1:2 %s with boxes axes %s lw 1 %s, \\", //
aggregatedData.getDataFile().getAbsolutePath(), // aggregatedData.getDataFile().getAbsolutePath(), //
gnuplotTitle(title), // gnuplotTitle(title), //
gnuplotXYAxis(), // gnuplotXYAxis(), //

View File

@@ -48,10 +48,9 @@ public class ParallelRequestsAggregate extends AggregateHandler {
} }
@Override @Override
public void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle, public String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
final Optional<String> title) {
final AggregatedData aggregatedData = aggregator.getAggregatedData(); final AggregatedData aggregatedData = aggregator.getAggregatedData();
appendfln(result, "'%s' using 1:2 %s with filledcurve axes %s lw 1 %s, \\", // return formatln("'%s' using 1:2 %s with filledcurve axes %s lw 1 %s, \\", //
aggregatedData.getDataFile().getAbsolutePath(), // aggregatedData.getDataFile().getAbsolutePath(), //
gnuplotTitle(title), // gnuplotTitle(title), //
gnuplotXYAxis(), // gnuplotXYAxis(), //

View File

@@ -44,11 +44,10 @@ public class ScatterAggregateHandler extends AggregateHandler {
} }
@Override @Override
public void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle, public String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
final Optional<String> title) {
final AggregatedData aggregatedData = aggregator.getAggregatedData(); final AggregatedData aggregatedData = aggregator.getAggregatedData();
appendfln(result, "'%s' using 1:2 %s with %s axes %s %s, \\", // return formatln("'%s' using 1:2 %s with %s axes %s %s, \\", //
aggregatedData.getDataFile(), // aggregatedData.getDataFile(), //
gnuplotTitle(title), // gnuplotTitle(title), //
GnuplotLineType.Points, // GnuplotLineType.Points, //

View File

@@ -67,8 +67,6 @@ public class GnuplotFileGenerator implements Appender {
// appendfln(result, "set xrange [-1:1]"); // appendfln(result, "set xrange [-1:1]");
appendfln(result, "set boxwidth 0.5"); appendfln(result, "set boxwidth 0.5");
appendf(result, "plot ");
settings.getAggregates().addPlots(result, dataSeries); settings.getAggregates().addPlots(result, dataSeries);
// Add a plot outside of the visible range. Without this gnuplot would not // Add a plot outside of the visible range. Without this gnuplot would not