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 void addPlot(StringBuilder result, CustomAggregator aggregator, LineStyle lineStyle,
Optional<String> title);
abstract String addPlot(CustomAggregator aggregator, LineStyle lineStyle, Optional<String> title);
abstract CustomAggregator createCustomAggregator(Path tmpDir, PlotSettings plotSettings, long fromEpochMilli,
long toEpochMilli);
String beforePlot(final CustomAggregator aggregator) {
return "";
}
protected String gnuplotTitle(final Optional<String> title) {
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) {
final StringBuilder plots = new StringBuilder();
final StringBuilder beforePlots = new StringBuilder();
boolean first = true;
final List<AggregateHandler> handlersInPlottingOrder = CollectionUtils.copySort(aggregateHandlers,
PLOTTING_ORDER);
@@ -113,11 +116,20 @@ public class AggregateHandlerCollection {
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;
}
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));
}
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.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import org.lucares.pdb.api.RuntimeIOException;
@@ -16,6 +17,8 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
private Long index = null;
private final String dataName = "$data" + UUID.randomUUID().toString().replace("-", "");
public BarChartAggregator(final Path tmpDir) {
super();
this.tmpDir = tmpDir;
@@ -39,11 +42,7 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
count++;
}
@Override
public AggregatedData getAggregatedData() {
try {
final File dataFile = File.createTempFile("bar", ".dat", tmpDir.toFile());
public String asCsv() {
final StringBuilder csv = new StringBuilder();
csv.append(index + 0.5);
@@ -51,9 +50,17 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
csv.append("");
csv.append(",");
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);
return result;
} catch (final IOException e) {
@@ -66,4 +73,9 @@ public class BarChartAggregator implements CustomAggregator, IndexedAggregator {
return Aggregate.BAR;
}
public String getDataName() {
return dataName;
}
}

View File

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

View File

@@ -69,12 +69,11 @@ public class CumulativeDistributionHandler extends AggregateHandler {
}
@Override
public void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle,
final Optional<String> title) {
public String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
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(), //
gnuplotTitle(title), //
gnuplotXYAxis(), //

View File

@@ -54,11 +54,10 @@ public class HistogramHandler extends AggregateHandler {
}
@Override
void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle,
final Optional<String> title) {
String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
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(), //
gnuplotTitle(title), //
gnuplotXYAxis(), //

View File

@@ -48,10 +48,9 @@ public class ParallelRequestsAggregate extends AggregateHandler {
}
@Override
public void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle,
final Optional<String> title) {
public String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
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(), //
gnuplotTitle(title), //
gnuplotXYAxis(), //

View File

@@ -44,11 +44,10 @@ public class ScatterAggregateHandler extends AggregateHandler {
}
@Override
public void addPlot(final StringBuilder result, final CustomAggregator aggregator, final LineStyle lineStyle,
final Optional<String> title) {
public String addPlot(final CustomAggregator aggregator, final LineStyle lineStyle, final Optional<String> title) {
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(), //
gnuplotTitle(title), //
GnuplotLineType.Points, //

View File

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