-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChartFormatters.swift
72 lines (64 loc) · 2.05 KB
/
ChartFormatters.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
//
// ChartFormatters.swift
// Cardiologic
//
// Created by Ben Zimring on 7/26/18.
// Copyright © 2018 pulseApp. All rights reserved.
//
import Foundation
import Charts
/**
VariabilityValueFormatter
- Parameter value: input double to format
- Returns: string of input stripped of decimals, appends "ms"
*/
class VariabilityValueFormatter : NSObject, IValueFormatter {
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
return "\(Int(value))"
}
}
/**
DigitValueFormatter
- Parameter value: input double to format
- Returns: formatted string stripped of decimals, appends K if in 1000s
*/
class DigitValueFormatter : NSObject, IValueFormatter {
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
let n = Int(value)
return n >= 1000 ? "\(n/1000)K" : "\(n)"
}
}
/**
DayValueFormatter
- Parameter value: number of days from now to get string
- Returns: 2char prefix string of day of the week
*/
class DayValueFormatter : NSObject, IAxisValueFormatter {
func stringForValue(_ value: Double, axis _: AxisBase?) -> String {
let calendar = Calendar.current
let day = calendar.date(byAdding: .day, value: Int(value), to: Date())
let formatter = DateFormatter()
formatter.dateFormat = "EEEE"
let str = formatter.string(from: day!)
return String(str.prefix(2))
}
}
/**
WorkoutValueFormatter
- Parameter value: time (minutes) of workout
- Returns: string formatted for minutes/hours
*/
class WorkoutValueFormatter : NSObject, IValueFormatter {
func stringForValue(_ value: Double,
entry: ChartDataEntry,
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String {
return "\(Int(value))"
}
}