Sparklines now update when a boat passes a mark.

There are issue of initialising the sparkline before the race starts and when the race is being viewed when it starts. It works great though when you join a race halfway.
#story[952]
This commit is contained in:
Kusal Ekanayake
2017-05-19 18:08:26 +12:00
parent 422dcd4501
commit 48d58ea660
3 changed files with 32 additions and 40 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ public class App extends Application {
}
//Change the StreamReceiver in this else block to change the default data source.
else{
//sr = new StreamReceiver("localhost", 4949, "RaceStream");
// sr = new StreamReceiver("localhost", 4949, "RaceStream");
// sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream");
sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream");
}
@@ -70,7 +70,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
private Timeline timerTimeline;
private Race race;
private Stage stage;
private ArrayList<XYChart.Series<String, Double>> sparklineData = new ArrayList<>();
private static HashMap<Integer, Series<String, Double>> sparklineData = new HashMap<>();
private ArrayList<String> positions;
private int time;
@@ -193,25 +193,39 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
for (Yacht yacht: startingBoats){
XYChart.Series<String, Double> yachtData = new XYChart.Series<>();
yachtData.setName(yacht.getBoatName());
yachtData.getData().add(new XYChart.Data<>(Integer.toString(time), 1 + startingBoats.size() - Double.parseDouble(yacht.getPosition())));
sparklineData.add(yachtData);
yachtData.getData().add(new XYChart.Data<>(Integer.toString(yacht.getLegNumber()), Double.parseDouble(yacht.getPosition())));
sparklineData.put(yacht.getSourceID(),yachtData);
}
for (Series<String, Double> spark: sparklineData){
List<XYChart.Series<String, Double>> positions = new ArrayList<XYChart.Series<String, Double>>(sparklineData.values());
Collections.sort(positions, new Comparator<XYChart.Series>() {
@Override
public int compare(Series o1, Series o2) {
Integer leg1 = Integer.parseInt( ((XYChart.Data<String, Double>) o1.getData().get(o1.getData().size()-1)).getXValue());
Integer leg2 = Integer.parseInt( ((XYChart.Data<String, Double>) o2.getData().get(o2.getData().size()-1)).getXValue());
if (leg2 < leg1){
return 1;
} else {
return -1;
}
}
});
for (Series<String, Double> spark: positions){
raceSparkLine.getData().add(spark);
}
Set<Node> nodes = raceSparkLine.lookupAll(".series");
for (Node n : nodes) {
StringBuilder style = new StringBuilder();
style.append("-fx-stroke: red; -fx-background-color: red, white; ");
n.setStyle(style.toString());
}
time++;
}
public static void updateYachtPositionSparkline(Yacht yacht, Integer legNumber){
XYChart.Series<String, Double> positionData = sparklineData.get(yacht.getSourceID());
positionData.getData().add(new XYChart.Data<>(Integer.toString(legNumber), Double.parseDouble(yacht.getPosition())));
}
public void checkForPositionChange(){
// initialize position array
if (positions.size() == 0 && startingBoats.size() == 6){
if (startingBoats.size() == 6 && raceSparkLine.getData().size() != 6){
Boolean positionsAssigned = true;
for (Yacht yacht: startingBoats){
if (Objects.equals(yacht.getPosition(), "-")) {
@@ -220,28 +234,12 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
}
if (positionsAssigned){
initializeSparkline();
positions.addAll(startingBoats.stream().map(Yacht::getPosition).collect(Collectors.toList()));
}
} else {
ArrayList<String> currentPositions = startingBoats.stream().map(Yacht::getPosition).collect(Collectors.toCollection(ArrayList::new));
if (!positions.equals(currentPositions)){
updateSparkLine();
}
positions.clear();
positions.addAll(startingBoats.stream().map(Yacht::getPosition).collect(Collectors.toList()));
}
}
public void updateSparkLine(){
for (int i = 0; i < startingBoats.size();i++){
if (!Objects.equals(startingBoats.get(i).getPosition(), "-")) {
sparklineData.get(i).getData().add(new XYChart.Data<>(Integer.toString(time), 1 + startingBoats.size() - Double.parseDouble(startingBoats.get(i).getPosition())));
}
}
time++;
}
/**
* Initalises a timer which updates elements of the RaceView such as wind direction, boat
+5 -11
View File
@@ -4,6 +4,7 @@ import java.util.concurrent.ConcurrentLinkedQueue;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.paint.Color;
import seng302.controllers.RaceViewController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -20,7 +21,6 @@ public class Yacht {
private double velocity;
private int index;
private ConcurrentLinkedQueue<Number> dataQ1;
private String boatType;
private Integer sourceID;
@@ -62,7 +62,6 @@ public class Yacht {
this.shortName = shortName;
this.sourceID = id;
index = 0;
dataQ1 = new ConcurrentLinkedQueue<>();
}
@@ -74,7 +73,6 @@ public class Yacht {
this.boatName = boatName;
this.country = country;
index = 0;
dataQ1 = new ConcurrentLinkedQueue<>();
}
public String getBoatType() {
@@ -109,7 +107,11 @@ public class Yacht {
}
public void setLegNumber(Integer legNumber) {
if (legNumber != this.legNumber && colour != null && position != "-") {
RaceViewController.updateYachtPositionSparkline(this, legNumber);
}
this.legNumber = legNumber;
}
public Integer getPenaltiesAwarded() {
@@ -152,14 +154,6 @@ public class Yacht {
}
public void setPosition(String position) {
try {
if (Integer.parseInt(position) != Integer.parseInt(this.position)){
dataQ1.add(Integer.parseInt(position));
System.out.println("new position found for " + boatName + " in position " + position + " old(" + this.position + ")");
}
} catch (NumberFormatException e) {
System.out.println("No position found");
}
this.position = position;
}