reduce mempool size x axis labelling

This commit is contained in:
Craig Raw 2020-11-23 12:23:45 +02:00
parent d8b7938a63
commit f305e0bdc2
2 changed files with 37 additions and 19 deletions

View file

@ -26,6 +26,7 @@ import java.util.stream.Collectors;
public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> { public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> {
private static final DateFormat dateFormatter = new SimpleDateFormat("HH:mm"); private static final DateFormat dateFormatter = new SimpleDateFormat("HH:mm");
public static final int MAX_PERIOD_HOURS = 2;
private Tooltip tooltip; private Tooltip tooltip;
@ -54,23 +55,22 @@ public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> {
} }
Map<Date, Set<MempoolRateSize>> periodRateSizes = getPeriodRateSizes(mempoolRateSizes); Map<Date, Set<MempoolRateSize>> periodRateSizes = getPeriodRateSizes(mempoolRateSizes);
List<String> categories = getCategories(periodRateSizes); Map<Date, String> categories = getCategories(periodRateSizes);
CategoryAxis categoryAxis = (CategoryAxis)getXAxis(); CategoryAxis categoryAxis = (CategoryAxis)getXAxis();
if(categoryAxis.getCategories() == null) { categoryAxis.setTickMarkVisible(false);
categoryAxis.setCategories(FXCollections.observableArrayList(categories)); categoryAxis.setTickLabelGap(10);
} else { categoryAxis.setAutoRanging(false);
categoryAxis.getCategories().retainAll(categories); categoryAxis.setCategories(FXCollections.observableArrayList(categories.values()));
categories.removeAll(categoryAxis.getCategories()); categoryAxis.invalidateRange(new ArrayList<>(categories.values()));
categoryAxis.getCategories().addAll(categories);
}
categoryAxis.setGapStartAndEnd(false); categoryAxis.setGapStartAndEnd(false);
categoryAxis.setTickLabelRotation(0); categoryAxis.setTickLabelRotation(0);
categoryAxis.setOnMouseMoved(mouseEvent -> { categoryAxis.setOnMouseMoved(mouseEvent -> {
String category = categoryAxis.getValueForDisplay(mouseEvent.getX()); String category = categoryAxis.getValueForDisplay(mouseEvent.getX());
if(category != null) { if(category != null) {
tooltip.setGraphic(new ChartTooltip(category, getData())); Optional<String> time = categories.entrySet().stream().filter(entry -> entry.getValue().equals(category)).map(entry -> dateFormatter.format(entry.getKey())).findFirst();
time.ifPresent(s -> tooltip.setGraphic(new ChartTooltip(category, s, getData())));
} }
}); });
@ -103,7 +103,7 @@ public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> {
} }
} }
series.getData().add(new XYChart.Data<>(dateFormatter.format(date), totalVSize)); series.getData().add(new XYChart.Data<>(categories.get(date), totalVSize));
seriesTotalVSize += totalVSize; seriesTotalVSize += totalVSize;
} }
@ -114,8 +114,9 @@ public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> {
previousFeeRate = feeRate; previousFeeRate = feeRate;
} }
if(categories.iterator().hasNext()) { if(categories.keySet().iterator().hasNext()) {
tooltip.setGraphic(new ChartTooltip(categories.iterator().next(), getData())); String time = categories.values().iterator().next();
tooltip.setGraphic(new ChartTooltip(time, time, getData()));
numberAxis.setTickLabelsVisible(true); numberAxis.setTickLabelsVisible(true);
numberAxis.setOpacity(1); numberAxis.setOpacity(1);
} else { } else {
@ -129,7 +130,7 @@ public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> {
return mempoolRateSizes; return mempoolRateSizes;
} }
LocalDateTime period = LocalDateTime.now().minusHours(6); LocalDateTime period = LocalDateTime.now().minusHours(MAX_PERIOD_HOURS);
return mempoolRateSizes.entrySet().stream().filter(entry -> { return mempoolRateSizes.entrySet().stream().filter(entry -> {
LocalDateTime dateTime = entry.getKey().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); LocalDateTime dateTime = entry.getKey().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
return dateTime.isAfter(period); return dateTime.isAfter(period);
@ -138,18 +139,27 @@ public class MempoolSizeFeeRatesChart extends StackedAreaChart<String, Number> {
TreeMap::new)); TreeMap::new));
} }
private List<String> getCategories(Map<Date, Set<MempoolRateSize>> mempoolHistogram) { private Map<Date, String> getCategories(Map<Date, Set<MempoolRateSize>> mempoolHistogram) {
List<String> categories = new ArrayList<>(); Map<Date, String> categories = new LinkedHashMap<>();
for(Date date : mempoolHistogram.keySet()) {
categories.add(dateFormatter.format(date)); String invisible = "" + (char)29;
for(Iterator<Date> iter = mempoolHistogram.keySet().iterator(); iter.hasNext(); ) {
Date date = iter.next();
String label = dateFormatter.format(date);
if(!categories.isEmpty() && iter.hasNext()) {
label = invisible;
invisible += (char)29;
}
categories.put(date, label);
} }
return categories; return categories;
} }
private static class ChartTooltip extends VBox { private static class ChartTooltip extends VBox {
public ChartTooltip(String category, List<Series<String, Number>> seriesList) { public ChartTooltip(String category, String time, List<Series<String, Number>> seriesList) {
Label title = new Label("At " + category); Label title = new Label("At " + time);
HBox titleBox = new HBox(title); HBox titleBox = new HBox(title);
title.getStyleClass().add("tooltip-title"); title.getStyleClass().add("tooltip-title");
getChildren().add(titleBox); getChildren().add(titleBox);

View file

@ -40,4 +40,12 @@ public class MempoolRateSize implements Comparable<MempoolRateSize> {
public int compareTo(MempoolRateSize other) { public int compareTo(MempoolRateSize other) {
return Long.compare(fee, other.fee); return Long.compare(fee, other.fee);
} }
@Override
public String toString() {
return "MempoolRateSize{" +
"fee=" + fee +
", vSize=" + vSize +
'}';
}
} }