From 0483859c404fdabdc3cc1b4e09bf34e237352eca Mon Sep 17 00:00:00 2001 From: fjc40 Date: Tue, 15 Aug 2017 20:40:29 +1200 Subject: [PATCH] Fixed sparkline y axis labelling. Reverted it to how it previously worked (negative data values instead of negative scale). issue #32 --- .../main/java/visualiser/model/Sparkline.java | 48 +++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/racevisionGame/src/main/java/visualiser/model/Sparkline.java b/racevisionGame/src/main/java/visualiser/model/Sparkline.java index 3e49c5a3..3f7e07b8 100644 --- a/racevisionGame/src/main/java/visualiser/model/Sparkline.java +++ b/racevisionGame/src/main/java/visualiser/model/Sparkline.java @@ -71,7 +71,7 @@ public class Sparkline { * @param race The race to listen to. * @param sparklineChart JavaFX LineChart for the sparkline. */ - public Sparkline(VisualiserRaceState race, LineChart sparklineChart) { + public Sparkline(VisualiserRaceState race, LineChart sparklineChart) { this.race = race; this.boats = new SortedList<>(race.getBoats()); this.legs = race.getLegs(); @@ -115,7 +115,7 @@ public class Sparkline { } //Update height of y axis. - yAxis.setLowerBound(boats.size()); + setYAxisLowerBound(); }); }); @@ -143,17 +143,47 @@ public class Sparkline { xAxis.setUpperBound(race.getLegCount()); xAxis.setTickUnit(1); + + //The y-axis uses negative values, with the minus sign hidden (e.g., boat in 1st has position -1, which becomes 1, boat in 6th has position -6, which becomes 6). + //This is necessary to actually get the y-axis labelled correctly. Negative tick count doesn't work. //Set y axis details - yAxis.setLowerBound(boats.size()); - yAxis.setUpperBound(1); yAxis.setAutoRanging(false); + + yAxis.setTickUnit(1); + yAxis.setMinorTickCount(0); + + yAxis.setUpperBound(0); + setYAxisLowerBound(); + yAxis.setLabel("Position in Race"); - yAxis.setTickUnit(-1);//Negative tick reverses the y axis. yAxis.setTickMarkVisible(true); yAxis.setTickLabelsVisible(true); - yAxis.setTickMarkVisible(true); - yAxis.setMinorTickVisible(true); + yAxis.setMinorTickVisible(false); + + + + //Hide minus number from displaying on axis. + yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { + @Override + public String toString(Number value) { + if ((value.intValue() == 0) || (value.intValue() < -boats.size())) { + return ""; + + } else { + return String.format("%d", -value.intValue()); + } + } + }); + + } + + + /** + * Sets the lower bound of the y-axis. + */ + private void setYAxisLowerBound() { + yAxis.setLowerBound( -(boats.size() + 1)); } @@ -179,7 +209,7 @@ public class Sparkline { //All boats start in "last" place. - series.getData().add(new XYChart.Data<>(0, boats.size())); + series.getData().add(new XYChart.Data<>(0, -(boats.size()))); //Listen for changes in the boat's leg - we only update the graph when it changes leg. boat.legProperty().addListener( @@ -188,7 +218,7 @@ public class Sparkline { //Get the data to plot. List boatOrder = race.getLegCompletionOrder().get(oldValue); //Find boat position in list. - int boatPosition = boatOrder.indexOf(boat) + 1; + int boatPosition = -(boatOrder.indexOf(boat) + 1); //Get leg number. int legNumber = oldValue.getLegNumber() + 1;