Skip to content

170 three bugs one issue #171

Merged
merged 3 commits into from
May 27, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,8 @@ private void applySave(final SaveGame save) {
if (save.getNetWorthHistory() != null && !save.getNetWorthHistory().isEmpty()) {
player.setNetWorthHistory(save.getNetWorthHistory());
} else {
// No recorded history available - seed a minimal two-point
// history so the chart still has something to render.
List<BigDecimal> seed = new ArrayList<>();
seed.add(BigDecimal.valueOf(save.getStartingCapital()));
seed.add(player.getNetWorth());
player.setNetWorthHistory(seed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,17 @@ protected void initLayout() {
yAxis.setTickMarkVisible(true);
yAxis.setMinorTickVisible(false);
yAxis.setAutoRanging(false);
yAxis.setLabel("Price (NOK)");
yAxis.setTickLabelFormatter(new javafx.util.StringConverter<Number>() {
@Override
public String toString(final Number value) {
return Math.round(value.floatValue() * 100f) / 100f + " NOK";
}

@Override
public Number fromString(final String string) {
return 0;
}
});

chart = new LineChart<>(xAxis, yAxis);
chart.setCreateSymbols(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public <T> void handleEvent(final EventData<T> data) {
balanceHistory.addAll(player.getNetWorthHistory());
} else {
balanceHistory.add(player.getStartingMoney());
balanceHistory.add(player.getNetWorth());
}
pushSnapshot();
}
Expand Down Expand Up @@ -277,7 +276,6 @@ public void handleContextUpdate(final Exchange updatedExchange, final Player upd
this.balanceHistory.addAll(this.player.getNetWorthHistory());
} else {
this.balanceHistory.add(this.player.getStartingMoney());
this.balanceHistory.add(this.player.getNetWorth());
}
pushSnapshot();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,15 @@ private void renderBalanceChart() {
balanceChartPane.getChildren().add(seg);
}

// X-axis labels (week numbers).
int xLabelStep = computeXLabelStep(n, stepX);
int lastIndex = n - 1;
for (int i = 0; i < n; i++) {
boolean isEdge = (i == 0 || i == lastIndex);
boolean isStepAligned = (i % xLabelStep == 0)
&& (lastIndex - i >= xLabelStep || i == 0);
if (!isEdge && !isStepAligned) {
continue;
}
double x = padL + i * stepX;
Text xLabel = new Text(0, h - 6, String.valueOf(i + 1));
xLabel.getStyleClass().add("stats-chart-axis");
Expand Down Expand Up @@ -649,6 +656,35 @@ private static String stripTrailingZeros(final BigDecimal v) {
return v.stripTrailingZeros().toPlainString();
}

/**
* Picks a stride that lets us draw every {@code step}-th week label
* along the x-axis without overlap.
*
* <p>Targets at least ~28 px of horizontal space between adjacent
* rendered labels. The step is also rounded to a "nice" value
* (1, 2, 5, 10, 20, 50, ...) so the labels read as a clean
* progression rather than landing on awkward weeks.</p>
*
* @param n the total number of data points on the chart.
* @param stepX the horizontal spacing between consecutive points.
* @return the index stride to use when rendering x-axis labels.
* */
private static int computeXLabelStep(final int n, final double stepX) {
final double minLabelSpacing = 28.0;
if (stepX >= minLabelSpacing || n <= 1) {
return 1;
}
int rawStep = (int) Math.ceil(minLabelSpacing / stepX);
// Snap to a nice 1-2-5 progression so labels land on tidy weeks.
int[] niceSteps = {1, 2, 5, 10, 20, 50, 100};
for (int s : niceSteps) {
if (s >= rawStep) {
return s;
}
}
return rawStep;
}

/** Internal data carrier for a single pie slice. */
private static final class Segment {
final String label;
Expand Down