Skip to content

Commit

Permalink
fiks
Browse files Browse the repository at this point in the history
  • Loading branch information
EspenTinius committed May 27, 2026
1 parent b0c509a commit 8781ccf
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,13 @@ 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.
// No recorded history available - seed a single-point history
// anchored at the starting capital. The chart needs at least
// two points to draw a line, so it will stay empty until the
// first week is advanced, which is the correct behaviour for
// a brand-new game on week 1.
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,22 @@ protected void initLayout() {
yAxis.setTickMarkVisible(true);
yAxis.setMinorTickVisible(false);
yAxis.setAutoRanging(false);
yAxis.setLabel("Price (NOK)");
// The default rotated axis label ("Price (NOK)" sideways along
// the y-axis) is hard to read, so we drop the label and suffix
// every tick value with " NOK" instead - same currency cue, but
// readable in the same horizontal orientation as the rest of the
// app.
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,21 @@ private void renderBalanceChart() {
balanceChartPane.getChildren().add(seg);
}

// X-axis labels (week numbers).
// X-axis labels (week numbers). When the chart shows many weeks
// the labels overlap and become unreadable, so we thin them out:
// pick a step that guarantees at least ~28 px between labels and
// always render the first and last week. To avoid the last-week
// label colliding with the previous step-aligned label, we skip
// any step label that lands within one stride of the final week.
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 +662,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

0 comments on commit 8781ccf

Please sign in to comment.