prevent labels from overlapping

This commit is contained in:
2020-09-20 09:14:04 +02:00
parent 70430eb1fd
commit 69f12aba98
2 changed files with 23 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import org.lucares.recommind.logs.GnuplotAxis; import org.lucares.recommind.logs.GnuplotAxis;
import org.lucares.recommind.logs.GnuplotSettings;
public class BarChartAggregatorForIntervals implements CustomAggregator, IndexedAggregator, BarChart { public class BarChartAggregatorForIntervals implements CustomAggregator, IndexedAggregator, BarChart {
@@ -24,8 +25,11 @@ public class BarChartAggregatorForIntervals implements CustomAggregator, Indexed
private int count; private int count;
public BarChartAggregatorForIntervals(final Interval interval) { private final PlotSettings settings;
this.interval = interval;
public BarChartAggregatorForIntervals(final PlotSettings settings) {
this.settings = settings;
this.interval = settings.getInterval().get();
buckets = interval.getBuckets(); buckets = interval.getBuckets();
} }
@@ -72,20 +76,35 @@ public class BarChartAggregatorForIntervals implements CustomAggregator, Indexed
final StringBuilder csv = new StringBuilder(); final StringBuilder csv = new StringBuilder();
final boolean isMiddleSeries = getIndex() == numberOfDataSeries / 2; final boolean isMiddleSeries = getIndex() == numberOfDataSeries / 2;
int i = 0;
int offset = 0; int offset = 0;
for (final String bucketId : bucketIds()) { for (final String bucketId : bucketIds()) {
final long count = buckets.get(bucketId).get(); final long count = buckets.get(bucketId).get();
csv.append(String.format(Locale.US, "%f", offset + getIndex() * SPACE_BETWEEN_BARS + 0.5)); csv.append(String.format(Locale.US, "%f", offset + getIndex() * SPACE_BETWEEN_BARS + 0.5));
csv.append(","); csv.append(",");
csv.append(renderLabels && isMiddleSeries ? bucketId : ""); csv.append(renderLabels && isMiddleSeries && showLabel(i, buckets.size()) ? bucketId : "");
csv.append(","); csv.append(",");
csv.append(count); csv.append(count);
csv.append("\n"); csv.append("\n");
offset += numberOfDataSeries; offset += numberOfDataSeries;
i++;
} }
return csv.toString(); return csv.toString();
} }
private boolean showLabel(final int index, final int numberOfBuckets) {
final int width = settings.getWidth();
final int widthInPx = width - GnuplotSettings.GNUPLOT_LEFT_RIGHT_MARGIN;
final long maxLabels = Math.max(1, widthInPx / (GnuplotSettings.TICKS_FONT_SIZE * 8));
if (maxLabels >= numberOfBuckets) {
return true;
} else {
return index % (int) Math.ceil(numberOfBuckets / maxLabels) == 0;
}
}
private SortedSet<String> bucketIds() { private SortedSet<String> bucketIds() {
return new TreeSet<>(buckets.keySet()); return new TreeSet<>(buckets.keySet());

View File

@@ -82,7 +82,7 @@ public class BarChartHandler extends AggregateHandler {
CustomAggregator createCustomAggregator(final Path tmpDir, final PlotSettings plotSettings, CustomAggregator createCustomAggregator(final Path tmpDir, final PlotSettings plotSettings,
final long fromEpochMilli, final long toEpochMilli) { final long fromEpochMilli, final long toEpochMilli) {
if (plotSettings.getInterval().isPresent()) { if (plotSettings.getInterval().isPresent()) {
return new BarChartAggregatorForIntervals(plotSettings.getInterval().get()); return new BarChartAggregatorForIntervals(plotSettings);
} else { } else {
return new BarChartAggregator(); return new BarChartAggregator();
} }