Sparkline moved to its own class
- Sparkline class creates and modifies sparkLine chart - removed sparkline implementation from RaceController - fixing RaceController code smells #story[876, 1003]
This commit is contained in:
@@ -5,8 +5,6 @@ import javafx.application.Platform;
|
|||||||
import javafx.collections.ObservableList;
|
import javafx.collections.ObservableList;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.chart.LineChart;
|
import javafx.scene.chart.LineChart;
|
||||||
import javafx.scene.chart.NumberAxis;
|
|
||||||
import javafx.scene.chart.XYChart;
|
|
||||||
import javafx.scene.control.*;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.layout.GridPane;
|
import javafx.scene.layout.GridPane;
|
||||||
import javafx.scene.layout.Pane;
|
import javafx.scene.layout.Pane;
|
||||||
@@ -17,7 +15,10 @@ import seng302.Model.*;
|
|||||||
import seng302.VisualiserInput;
|
import seng302.VisualiserInput;
|
||||||
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.ResourceBundle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by fwy13 on 15/03/2017.
|
* Created by fwy13 on 15/03/2017.
|
||||||
@@ -47,16 +48,13 @@ public class RaceController extends Controller {
|
|||||||
private static int partialBtn = 3;
|
private static int partialBtn = 3;
|
||||||
private static int importantBtn = 4;
|
private static int importantBtn = 4;
|
||||||
|
|
||||||
private ArrayList<Boat> startBoats;
|
private int legNum;
|
||||||
private Integer sparkLineNumber = 0;
|
private Sparkline sparkline;
|
||||||
|
|
||||||
private ResizableRaceCanvas raceMap;
|
private ResizableRaceCanvas raceMap;
|
||||||
private ResizableRaceMap raceBoundaries;
|
private ResizableRaceMap raceBoundaries;
|
||||||
private ToggleGroup annotationGroup;
|
private ToggleGroup annotationGroup;
|
||||||
|
|
||||||
private ArrayList<String> colours;
|
|
||||||
private Map<Integer, String> boatColours = new HashMap<>();
|
|
||||||
private int legNum;
|
|
||||||
private RaceClock raceClock;
|
private RaceClock raceClock;
|
||||||
|
|
||||||
@FXML Pane arrow;
|
@FXML Pane arrow;
|
||||||
@@ -83,8 +81,6 @@ public class RaceController extends Controller {
|
|||||||
@FXML RadioButton partialAnnoRBTN;
|
@FXML RadioButton partialAnnoRBTN;
|
||||||
@FXML RadioButton importantAnnoRBTN;
|
@FXML RadioButton importantAnnoRBTN;
|
||||||
@FXML LineChart<Number, Number> sparklineChart;
|
@FXML LineChart<Number, Number> sparklineChart;
|
||||||
@FXML NumberAxis xAxis;
|
|
||||||
@FXML NumberAxis yAxis;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the ResizableRaceCanvas (raceMap) with most recent data
|
* Updates the ResizableRaceCanvas (raceMap) with most recent data
|
||||||
@@ -120,7 +116,6 @@ public class RaceController extends Controller {
|
|||||||
@Override
|
@Override
|
||||||
public void initialize(URL location, ResourceBundle resources) {
|
public void initialize(URL location, ResourceBundle resources) {
|
||||||
//listener for fps
|
//listener for fps
|
||||||
startBoats = new ArrayList<>();
|
|
||||||
showFPS.selectedProperty().addListener((ov, old_val, new_val) -> {
|
showFPS.selectedProperty().addListener((ov, old_val, new_val) -> {
|
||||||
if (showFPS.isSelected()) {
|
if (showFPS.isSelected()) {
|
||||||
FPS.setVisible(true);
|
FPS.setVisible(true);
|
||||||
@@ -138,60 +133,18 @@ public class RaceController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and sets initial display for Sparkline for race positions.
|
* Creates and sets initial display for Sparkline for race positions.
|
||||||
* A data series for each boat in the race is added.
|
|
||||||
* Position numbers are displayed.
|
|
||||||
*
|
|
||||||
* @param boats boats to display on the sparkline
|
* @param boats boats to display on the sparkline
|
||||||
*/
|
*/
|
||||||
public void createSparkLine(ObservableList<Boat> boats){
|
public void createSparkLine(ObservableList<Boat> boats){
|
||||||
// NOTE: Y axis is in negatives to display correct positions
|
sparkline = new Sparkline(boats, legNum, sparklineChart);
|
||||||
|
}
|
||||||
|
|
||||||
makeColours();
|
/**
|
||||||
startBoats.addAll(boats);
|
* Updates the sparkline to display current boat positions.
|
||||||
mapBoatColours();
|
* @param boatsInRace used for current boat positions.
|
||||||
|
*/
|
||||||
// all boats start in 'last' place
|
public void updateSparkline(ObservableList<Boat> boatsInRace){
|
||||||
for (int i=0; i<startBoats.size(); i++){
|
sparkline.updateSparkline(boatsInRace);
|
||||||
XYChart.Series<Number, Number> series = new XYChart.Series();
|
|
||||||
series.getData().add(new XYChart.Data(0, -startBoats.size()));
|
|
||||||
series.getData().add(new XYChart.Data(0, -startBoats.size()));
|
|
||||||
sparklineChart.getData().add(series);
|
|
||||||
sparklineChart.getData().get(i).getNode().setStyle("-fx-stroke: " +
|
|
||||||
""+boatColours.get(startBoats.get(i).getSourceID())+";");
|
|
||||||
}
|
|
||||||
|
|
||||||
sparklineChart.setCreateSymbols(false);
|
|
||||||
|
|
||||||
// set x axis details
|
|
||||||
xAxis.setAutoRanging(false);
|
|
||||||
xAxis.setTickMarkVisible(false);
|
|
||||||
xAxis.setTickLabelsVisible(false);
|
|
||||||
xAxis.setMinorTickVisible(false);
|
|
||||||
xAxis.setUpperBound((startBoats.size()+1)*legNum);
|
|
||||||
xAxis.setTickUnit((startBoats.size()+1)*legNum);
|
|
||||||
|
|
||||||
|
|
||||||
// set y axis details
|
|
||||||
yAxis.setLowerBound(-(startBoats.size()+1));
|
|
||||||
yAxis.setUpperBound(0);
|
|
||||||
yAxis.setAutoRanging(false);
|
|
||||||
yAxis.setLabel("Position in Race");
|
|
||||||
yAxis.setTickUnit(1);
|
|
||||||
yAxis.setTickMarkVisible(false);
|
|
||||||
yAxis.setMinorTickVisible(false);
|
|
||||||
// hide minus number from displaying on axis
|
|
||||||
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) {
|
|
||||||
@Override
|
|
||||||
public String toString(Number value) {
|
|
||||||
if ((Double)value == 0.0
|
|
||||||
|| (Double)value < -startBoats.size()){
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return String.format("%7.0f", -value.doubleValue());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -282,78 +235,6 @@ public class RaceController extends Controller {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the sparkline to display current boat positions.
|
|
||||||
* New points are plotted to represent each boat when required.
|
|
||||||
*
|
|
||||||
* @param boatsInRace used for current boat positions.
|
|
||||||
*/
|
|
||||||
public void updateSparkline(ObservableList<Boat> boatsInRace){
|
|
||||||
int placingVal = boatsInRace.size();
|
|
||||||
sparkLineNumber++;
|
|
||||||
|
|
||||||
for (int i = boatsInRace.size() - 1; i >= 0; i--){
|
|
||||||
for (int j = startBoats.size() - 1; j >= 0; j--){
|
|
||||||
if (boatsInRace.get(i)==startBoats.get(j)){
|
|
||||||
|
|
||||||
// when a boat is on its first leg
|
|
||||||
if (boatsInRace.get(i).getCurrentLeg().getLegNumber()==0){
|
|
||||||
// adjust boats latest point on X axis
|
|
||||||
sparklineChart.getData().get(j).getData().get(1)
|
|
||||||
.setXValue(sparkLineNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
// when a boat first enters its second leg
|
|
||||||
else if (boatsInRace.get(i).getCurrentLeg().getLegNumber
|
|
||||||
()==1 && sparklineChart.getData().get(j).getData
|
|
||||||
().size()==2){
|
|
||||||
// adjust boats position from start mark
|
|
||||||
sparklineChart.getData().get(j).getData().get(1)
|
|
||||||
.setYValue(-placingVal);
|
|
||||||
sparklineChart.getData().get(j).getData().get(1)
|
|
||||||
.setXValue(sparkLineNumber);
|
|
||||||
sparklineChart.getData().get(j).getData().add(new XYChart.Data<>
|
|
||||||
(sparkLineNumber, -placingVal));
|
|
||||||
}
|
|
||||||
|
|
||||||
// plot new point for boats current position
|
|
||||||
else {
|
|
||||||
sparklineChart.getData().get(j).getData().add
|
|
||||||
(new XYChart.Data<>(sparkLineNumber, -placingVal));
|
|
||||||
}
|
|
||||||
placingVal-=1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// xAxis.setUpperBound(sparkLineNumber);
|
|
||||||
// xAxis.setTickUnit(sparkLineNumber);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void makeColours() {
|
|
||||||
colours = new ArrayList<>(Arrays.asList(
|
|
||||||
colourToHex(Color.BLUEVIOLET),
|
|
||||||
colourToHex(Color.BLACK),
|
|
||||||
colourToHex(Color.RED),
|
|
||||||
colourToHex(Color.ORANGE),
|
|
||||||
colourToHex(Color.DARKOLIVEGREEN),
|
|
||||||
colourToHex(Color.LIMEGREEN),
|
|
||||||
colourToHex(Color.PURPLE),
|
|
||||||
colourToHex(Color.DARKGRAY),
|
|
||||||
colourToHex(Color.YELLOW)
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void mapBoatColours() {
|
|
||||||
int currentColour = 0;
|
|
||||||
for (Boat boat : startBoats) {
|
|
||||||
if (!boatColours.containsKey(boat.getSourceID())) {
|
|
||||||
boatColours.put(boat.getSourceID(), colours.get(currentColour));
|
|
||||||
}
|
|
||||||
currentColour = (currentColour + 1) % colours.size();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void storeCurrentAnnotationState(String dictionaryAnnotationKey, boolean selected){
|
private void storeCurrentAnnotationState(String dictionaryAnnotationKey, boolean selected){
|
||||||
if (buttonChecked != hideBtn) {
|
if (buttonChecked != hideBtn) {
|
||||||
//if we are checking the box straight out of hide instead of using the radio buttons
|
//if we are checking the box straight out of hide instead of using the radio buttons
|
||||||
|
|||||||
@@ -0,0 +1,174 @@
|
|||||||
|
package seng302.Model;
|
||||||
|
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.fxml.FXML;
|
||||||
|
import javafx.scene.chart.LineChart;
|
||||||
|
import javafx.scene.chart.NumberAxis;
|
||||||
|
import javafx.scene.chart.XYChart;
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class to process and modify a sparkline display
|
||||||
|
*/
|
||||||
|
public class Sparkline {
|
||||||
|
private ArrayList<String> colours;
|
||||||
|
private ArrayList<Boat> startBoats = new ArrayList<>();
|
||||||
|
private Map<Integer, String> boatColours = new HashMap<>();
|
||||||
|
private Integer legNum;
|
||||||
|
private Integer sparkLineNumber = 0;
|
||||||
|
@FXML LineChart<Number, Number> sparklineChart;
|
||||||
|
@FXML NumberAxis xAxis;
|
||||||
|
@FXML NumberAxis yAxis;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor to set up initial sparkline (LineChart) object
|
||||||
|
* @param boats boats to display on the sparkline
|
||||||
|
* @param legNum total number of legs in the race
|
||||||
|
* @param sparklineChart javaFX LineChart for the sparkline
|
||||||
|
*/
|
||||||
|
public Sparkline(ObservableList<Boat> boats, Integer legNum,
|
||||||
|
LineChart<Number,Number> sparklineChart) {
|
||||||
|
this.sparklineChart = sparklineChart;
|
||||||
|
this.legNum = legNum;
|
||||||
|
this.yAxis = (NumberAxis)sparklineChart.getYAxis();
|
||||||
|
this.xAxis = (NumberAxis)sparklineChart.getXAxis();
|
||||||
|
startBoats.addAll(boats);
|
||||||
|
|
||||||
|
makeColours();
|
||||||
|
mapBoatColours();
|
||||||
|
createSparkline();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and sets initial display for Sparkline for race positions.
|
||||||
|
* A data series for each boat in the race is added.
|
||||||
|
* Position numbers are displayed.
|
||||||
|
*/
|
||||||
|
public void createSparkline(){
|
||||||
|
// NOTE: Y axis is in negatives to display correct positions
|
||||||
|
|
||||||
|
// all boats start in 'last' place
|
||||||
|
for (int i=0; i<startBoats.size(); i++){
|
||||||
|
XYChart.Series<Number, Number> series = new XYChart.Series();
|
||||||
|
series.getData().add(new XYChart.Data(0, -startBoats.size()));
|
||||||
|
series.getData().add(new XYChart.Data(0, -startBoats.size()));
|
||||||
|
sparklineChart.getData().add(series);
|
||||||
|
sparklineChart.getData().get(i).getNode().setStyle("-fx-stroke: " +
|
||||||
|
""+boatColours.get(startBoats.get(i).getSourceID())+";");
|
||||||
|
}
|
||||||
|
|
||||||
|
sparklineChart.setCreateSymbols(false);
|
||||||
|
|
||||||
|
// set x axis details
|
||||||
|
xAxis.setAutoRanging(false);
|
||||||
|
xAxis.setTickMarkVisible(false);
|
||||||
|
xAxis.setTickLabelsVisible(false);
|
||||||
|
xAxis.setMinorTickVisible(false);
|
||||||
|
xAxis.setUpperBound((startBoats.size()+1)*legNum);
|
||||||
|
xAxis.setTickUnit((startBoats.size()+1)*legNum);
|
||||||
|
|
||||||
|
// set y axis details
|
||||||
|
yAxis.setLowerBound(-(startBoats.size()+1));
|
||||||
|
yAxis.setUpperBound(0);
|
||||||
|
yAxis.setAutoRanging(false);
|
||||||
|
yAxis.setLabel("Position in Race");
|
||||||
|
yAxis.setTickUnit(1);
|
||||||
|
yAxis.setTickMarkVisible(false);
|
||||||
|
yAxis.setMinorTickVisible(false);
|
||||||
|
|
||||||
|
// hide minus number from displaying on axis
|
||||||
|
yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) {
|
||||||
|
@Override
|
||||||
|
public String toString(Number value) {
|
||||||
|
if ((Double)value == 0.0
|
||||||
|
|| (Double)value < -startBoats.size()){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return String.format("%7.0f", -value.doubleValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the sparkline to display current boat positions.
|
||||||
|
* New points are plotted to represent each boat when required.
|
||||||
|
* @param boatsInRace current position of the boats in race
|
||||||
|
*/
|
||||||
|
public void updateSparkline(ObservableList<Boat> boatsInRace){
|
||||||
|
int placingVal = boatsInRace.size();
|
||||||
|
sparkLineNumber++;
|
||||||
|
|
||||||
|
for (int i = boatsInRace.size() - 1; i >= 0; i--){
|
||||||
|
for (int j = startBoats.size() - 1; j >= 0; j--){
|
||||||
|
if (boatsInRace.get(i)==startBoats.get(j)){
|
||||||
|
|
||||||
|
// when a boat is on its first leg
|
||||||
|
if (boatsInRace.get(i).getCurrentLeg().getLegNumber()==0){
|
||||||
|
// adjust boats latest point on X axis
|
||||||
|
sparklineChart.getData().get(j).getData().get(1)
|
||||||
|
.setXValue(sparkLineNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
// when a boat first enters its second leg
|
||||||
|
else if (boatsInRace.get(i).getCurrentLeg().getLegNumber
|
||||||
|
()==1 && sparklineChart.getData().get(j).getData
|
||||||
|
().size()==2){
|
||||||
|
// adjust boats position from start mark
|
||||||
|
sparklineChart.getData().get(j).getData().get(1)
|
||||||
|
.setYValue(-placingVal);
|
||||||
|
sparklineChart.getData().get(j).getData().get(1)
|
||||||
|
.setXValue(sparkLineNumber);
|
||||||
|
sparklineChart.getData().get(j).getData().add(new XYChart.Data<>
|
||||||
|
(sparkLineNumber, -placingVal));
|
||||||
|
}
|
||||||
|
|
||||||
|
// plot new point for boats current position
|
||||||
|
else {
|
||||||
|
sparklineChart.getData().get(j).getData().add
|
||||||
|
(new XYChart.Data<>(sparkLineNumber, -placingVal));
|
||||||
|
}
|
||||||
|
placingVal-=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void makeColours() {
|
||||||
|
colours = new ArrayList<>(Arrays.asList(
|
||||||
|
colourToHex(Color.BLUEVIOLET),
|
||||||
|
colourToHex(Color.BLACK),
|
||||||
|
colourToHex(Color.RED),
|
||||||
|
colourToHex(Color.ORANGE),
|
||||||
|
colourToHex(Color.DARKOLIVEGREEN),
|
||||||
|
colourToHex(Color.LIMEGREEN),
|
||||||
|
colourToHex(Color.PURPLE),
|
||||||
|
colourToHex(Color.DARKGRAY),
|
||||||
|
colourToHex(Color.YELLOW)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String colourToHex(Color color) {
|
||||||
|
return String.format( "#%02X%02X%02X",
|
||||||
|
(int)( color.getRed() * 255 ),
|
||||||
|
(int)( color.getGreen() * 255 ),
|
||||||
|
(int)( color.getBlue() * 255 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private void mapBoatColours() {
|
||||||
|
int currentColour = 0;
|
||||||
|
for (Boat boat : startBoats) {
|
||||||
|
if (!boatColours.containsKey(boat.getSourceID())) {
|
||||||
|
boatColours.put(boat.getSourceID(), colours.get(currentColour));
|
||||||
|
}
|
||||||
|
currentColour = (currentColour + 1) % colours.size();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user