-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphs.java
More file actions
72 lines (60 loc) · 2.26 KB
/
Graphs.java
File metadata and controls
72 lines (60 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.ArrayList;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Comparator;
/**
* Write a description of class Graphs here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Graphs
{
// Singleton pattern
private static Graphs instance;
// instance variables - replace the example below with your own
CovidDataLoader loader = new CovidDataLoader();
ArrayList<CovidData> records = loader.load();
// private static ControllerMain instance; // Delete soon if unnecessary
// Declare other controllers
ModelMain modelMain;
private LocalDate startDate;
private LocalDate endDate;
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private ArrayList<DataPoint> dataPoints = new ArrayList<>();
/**
* Constructor
*/
public Graphs() {}
//Returns a sorted list of all needed datapoints to populate a linechart, plotting date against deaths.
public ArrayList<DataPoint> deathTollParse() {
// Clear existing data points
dataPoints.clear();
// Retrieve start and end dates from ModelMain
modelMain = ModelMain.getInstance();
startDate = modelMain.getStartDate();
endDate = modelMain.getEndDate();
// Iterate through records and add data points within the date range
for (CovidData record : records) {
LocalDate date = LocalDate.parse(record.getDate(), FORMATTER);
if (date.isEqual(startDate) || date.isEqual(endDate) || (date.isAfter(startDate) && date.isBefore(endDate))) {
DataPoint dp = new DataPoint(date, record.getNewDeaths());
dataPoints.add(dp);
}
}
// Sort data points by date
Collections.sort(dataPoints, Comparator.comparing(DataPoint::getDate));
return dataPoints;
}
public ArrayList<DataPoint> getDataPoints() {
return dataPoints;
}
// Singleton getInstance() method
public static Graphs getInstance() {
if (instance == null) {
instance = new Graphs();
}
return instance;
}
}