-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChartsViewController.swift
307 lines (260 loc) · 10.7 KB
/
ChartsViewController.swift
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// ChartViewController.swift
// My Diabetic Manager
//
// Created by Vincenzo De Rosa on 01/02/17.
// Copyright © 2017 Vincenzo De Rosa. All rights reserved.
//
import UIKit
import Charts
var disclaimerHasBeenDisplayed = false
struct Giorno{
var nGiorno : Int = 0
var values : [Double] = []
}
struct Settimana{
var nSettimana : Int = 0
var giorni = [Giorno](repeating: Giorno(), count: 7)
}
struct Mese{
var nMese : Int = 0
var settimane = [Settimana](repeating: Settimana(), count: 5)
}
struct Anno{
var nAnno : Int = 0
var mesi = [Mese](repeating: Mese(), count: 12)
}
func inizialize_year()->Anno{
let data = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: data)
let year:Int = components.year!
var ANNO = Anno()
ANNO.nAnno = year
for i in 0...11{
ANNO.mesi[i].nMese = i + 1
for j in 0...4{
ANNO.mesi[i].settimane[j].nSettimana = j+1
for k in 0...6{
ANNO.mesi[i].settimane[j].giorni[k].nGiorno = k+1
ANNO.mesi[i].settimane[j].giorni[k].values = []
}
}
}
return ANNO
}
class ChartViewController: UIViewController, UITextFieldDelegate{
@IBOutlet weak var Salva: UIButton!
@IBOutlet weak var valore: UITextField!
@IBOutlet weak var todayLabel: UILabel!
@IBOutlet weak var textLabel: UILabel!
@IBOutlet weak var viewForChart: LineChartView!
@IBOutlet weak var segmentControl: UISegmentedControl!
weak var axisFormatDelegate: IAxisValueFormatter?
var support_day: [String] = []
var support_week: [String] = []
var support_month: [String] = []
var period: [String] = []
var current_year: Anno = Anno()
@IBAction func addValue(_ sender: UIButton) {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
let timeString = formatter.string(from: NSDate() as Date)
let data = returnDate()
let mese_c = getMonth(data)!
let sett_c = getWeekOfMonth(data)!
let giorno_c = getDayOfWeek(data)!
let letters = NSCharacterSet.letters
let range = valore.text!.rangeOfCharacter(from: letters)
if (valore.text != "" && range == nil){
if (Double(valore.text!))! >= 0.00{
current_year.mesi[mese_c-1].settimane[sett_c-1].giorni[giorno_c-1].values.append(Double(valore.text!)!)
support_day.append(timeString)
period = support_day
let values = current_year.mesi[mese_c-1].settimane[sett_c-1].giorni[giorno_c-1].values
setChart(dataEntryX: period, dataEntryY: values)
UserDefaults.standard.set(String(average(nums: values)), forKey: "average")
UserDefaults.standard.synchronize()
textLabel.text = String(average(nums: values))
}
}
else{
let alertController = UIAlertController(title: "Attention!", message: "Insert a valid value!", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Got it!", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
@IBAction func indexChanged(_ sender: UISegmentedControl) {
let data = returnDate()
let mese_c = getMonth(data)!
let sett_c = getWeekOfMonth(data)!
let giorno_c = getDayOfWeek(data)!
switch segmentControl.selectedSegmentIndex
{
case 0:
let values = current_year.mesi[mese_c-1].settimane[sett_c-1].giorni[giorno_c-1].values
if !period.isEmpty && values.isEmpty{
setChart(dataEntryX: [""], dataEntryY: [0.00])
textLabel.text = String(average(nums: []))
}
else{
period = support_day
setChart(dataEntryX: period, dataEntryY: values)
textLabel.text = String(average(nums: values))
}
case 1:
//init week
var values: [Double] = []
let week = current_year.mesi[mese_c-1].settimane[sett_c-1]
for day in week.giorni{
values.append(average(nums: day.values))
}
support_week = ["Lun", "Mar", "Mer", "Gio", "Ven", "Sab", "Dom"]
period = support_week
setChart(dataEntryX: period, dataEntryY: values)
textLabel.text = String(average(nums: values))
case 2:
var values: [Double] = []
var values_week: [Double] = []
var values_month: [Double] = []
for month in current_year.mesi{
for week in month.settimane{
for day in week.giorni{
values.append(average(nums: day.values))
}
values_week.append(average(nums: values))
values.removeAll()
}
values_month.append(average(nums: values_week))
values_week.removeAll()
}
support_month = ["Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic"]
period = support_month
setChart(dataEntryX: period, dataEntryY: values_month)
textLabel.text = String(average(nums: values_month))
default:
break
}
}
func average(nums: [Double]) -> Double {
if nums.isEmpty{return 0.00}
else{
var total = 0.0
for vote in nums{
total += Double(vote)
}
let votesTotal = Double(nums.count)
let average = total/votesTotal
return average
}
}
func returnDate()->(String){
let date = Date()
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day], from: date)
let year:Int = components.year!
let month:Int = components.month!
let day:Int = components.day!
return "\(year)-\(month)-\(day)"
}
func getDayOfWeek(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekDay = myCalendar.component(.weekday, from: todayDate)
return weekDay - 1
}
func getMonth(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let month = myCalendar.component(.month, from: todayDate)
return month
}
func getWeekOfMonth(_ today:String) -> Int? {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
guard let todayDate = formatter.date(from: today) else { return nil }
let myCalendar = Calendar(identifier: .gregorian)
let weekMonth = myCalendar.component(.weekOfMonth, from: todayDate)
return weekMonth
}
override func viewDidLoad() {
super.viewDidLoad()
current_year = inizialize_year()
axisFormatDelegate = self
valore.delegate = self
todayLabel.text = returnDate()
//cerchio bottone
Salva.layer.borderWidth = 0.5
Salva.layer.cornerRadius = 7
Salva.layer.borderColor = UIColor.lightGray.cgColor
textLabel.layer.borderWidth = 0.5
textLabel.layer.cornerRadius = 7
textLabel.layer.borderColor = UIColor.lightGray.cgColor
textLabel.layer.backgroundColor = hexStringToUIColor(hex: "EBEBF1").cgColor
// Do any additional setup after loading the view.
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setChart(dataEntryX forX:[String],dataEntryY forY: [Double]) {
viewForChart.noDataText = "You need to provide data for the chart."
var dataEntries:[ChartDataEntry] = []
for i in 0..<forX.count{
let dataEntry = ChartDataEntry(x: Double(i), y: Double(forY[i]) , data: forX as AnyObject?)
dataEntries.append(dataEntry)
}
let chartDataSet = LineChartDataSet(values: dataEntries, label: "Glycemic Value")
let chartData = LineChartData(dataSet: chartDataSet)
viewForChart.data = chartData
let xAxisValue = viewForChart.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
viewForChart.rightAxis.drawLabelsEnabled = false
xAxisValue.labelPosition = .bottom
viewForChart.pinchZoomEnabled = false
}
override func viewDidAppear(_ animated: Bool) {
if disclaimerHasBeenDisplayed == false {
disclaimerHasBeenDisplayed = true
let alertController = UIAlertController(title: "Welcome!", message: "Here You can check and annotate your glycemic values!", preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "Let's start!", style: UIAlertActionStyle.default,handler: nil))
self.present(alertController, animated: true, completion: nil)
}
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
}
extension ChartViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return period[Int(value)]
}
}
// converte codice colori
func hexStringToUIColor (hex:String) -> UIColor {
var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
if (cString.hasPrefix("#")) {
cString.remove(at: cString.startIndex)
}
if ((cString.characters.count) != 6) {
return UIColor.gray
}
var rgbValue:UInt32 = 0
Scanner(string: cString).scanHexInt32(&rgbValue)
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}