Chart.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // Chart.swift
  3. // LoopFollow
  4. //
  5. // Created by Jon Fawcett on 6/3/20.
  6. // Copyright © 2020 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import Charts
  10. final class OverrideFillFormatter: IFillFormatter {
  11. func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
  12. return CGFloat(dataSet.entryForIndex(0)!.y)
  13. //return 375
  14. }
  15. }
  16. final class CarbFillFormatter: IFillFormatter {
  17. func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
  18. return 315
  19. }
  20. }
  21. final class BolusFillFormatter: IFillFormatter {
  22. func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
  23. return 345
  24. }
  25. }
  26. final class basalFillFormatter: IFillFormatter {
  27. func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
  28. return 0
  29. }
  30. }
  31. final class ChartXValueFormatter: IAxisValueFormatter {
  32. func stringForValue(_ value: Double, axis: AxisBase?) -> String {
  33. let dateFormatter = DateFormatter()
  34. //let timezoneOffset = TimeZone.current.secondsFromGMT()
  35. //let epochTimezoneOffset = value + Double(timezoneOffset)
  36. if dateTimeUtils.is24Hour() {
  37. dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
  38. } else {
  39. dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
  40. }
  41. //let date = Date(timeIntervalSince1970: epochTimezoneOffset)
  42. let date = Date(timeIntervalSince1970: value)
  43. let formattedDate = dateFormatter.string(from: date)
  44. return formattedDate
  45. }
  46. }
  47. final class ChartYDataValueFormatter: IValueFormatter {
  48. func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
  49. if entry.data != nil {
  50. return entry.data as? String ?? ""
  51. } else {
  52. return ""
  53. }
  54. }
  55. }
  56. final class ChartYOverrideValueFormatter: IValueFormatter {
  57. func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
  58. if entry.data != nil {
  59. return entry.data as? String ?? ""
  60. } else {
  61. return ""
  62. }
  63. }
  64. }
  65. final class ChartYMMOLValueFormatter: IAxisValueFormatter {
  66. func stringForValue(_ value: Double, axis: AxisBase?) -> String {
  67. return bgUnits.toDisplayUnits(String(value))
  68. }
  69. }
  70. class ChartMarker: MarkerView {
  71. private var text = String()
  72. private let drawAttributes: [NSAttributedString.Key: Any] = [
  73. .font: UIFont.systemFont(ofSize: 15),
  74. //.foregroundColor: UIColor.white,
  75. //.backgroundColor: UIColor.darkGray
  76. .foregroundColor: UIColor.label,
  77. .backgroundColor: UIColor.secondarySystemBackground
  78. ]
  79. override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
  80. if entry.data != nil {
  81. text = entry.data as? String ?? ""
  82. } else {
  83. text = String(entry.y)
  84. }
  85. }
  86. override func draw(context: CGContext, point: CGPoint) {
  87. super.draw(context: context, point: point)
  88. let sizeForDrawing = text.size(withAttributes: drawAttributes)
  89. bounds.size = sizeForDrawing
  90. offset = CGPoint(x: -sizeForDrawing.width / 2, y: -sizeForDrawing.height - 4)
  91. let offset = offsetForDrawing(atPoint: point)
  92. let originPoint = CGPoint(x: point.x + offset.x, y: point.y + offset.y)
  93. let rectForText = CGRect(origin: originPoint, size: sizeForDrawing)
  94. drawText(text: text, rect: rectForText, withAttributes: drawAttributes)
  95. }
  96. private func drawText(text: String, rect: CGRect, withAttributes attributes: [NSAttributedString.Key: Any]? = nil) {
  97. let size = bounds.size
  98. let centeredRect = CGRect(
  99. x: rect.origin.x + (rect.size.width - size.width) / 2,
  100. y: rect.origin.y + (rect.size.height - size.height) / 2,
  101. width: size.width,
  102. height: size.height
  103. )
  104. text.draw(in: centeredRect, withAttributes: attributes)
  105. }
  106. }
  107. class PillMarker: MarkerImage {
  108. private (set) var color: UIColor
  109. private (set) var font: UIFont
  110. private (set) var textColor: UIColor
  111. private var labelText: String = ""
  112. private var attrs: [NSAttributedString.Key: AnyObject]!
  113. static let formatter: DateComponentsFormatter = {
  114. let f = DateComponentsFormatter()
  115. f.allowedUnits = [.minute, .second]
  116. f.unitsStyle = .short
  117. return f
  118. }()
  119. init(color: UIColor, font: UIFont, textColor: UIColor) {
  120. self.color = color
  121. self.font = font
  122. self.textColor = textColor
  123. let paragraphStyle = NSMutableParagraphStyle()
  124. paragraphStyle.alignment = .center
  125. attrs = [.font: font, .paragraphStyle: paragraphStyle, .foregroundColor: textColor, .baselineOffset: NSNumber(value: -4)]
  126. super.init()
  127. }
  128. override func draw(context: CGContext, point: CGPoint) {
  129. // custom padding around text
  130. let labelWidth = labelText.size(withAttributes: attrs).width + 10
  131. // if you modify labelHeigh you will have to tweak baselineOffset in attrs
  132. let labelHeight = labelText.size(withAttributes: attrs).height + 4
  133. // place pill above the marker, centered along x
  134. var rectangle = CGRect(x: point.x, y: point.y, width: labelWidth, height: labelHeight)
  135. rectangle.origin.x -= rectangle.width / 2.0
  136. var spacing: CGFloat = 20
  137. if point.y < 300 { spacing = -40 }
  138. rectangle.origin.y -= rectangle.height + spacing
  139. // rounded rect
  140. let clipPath = UIBezierPath(roundedRect: rectangle, cornerRadius: 6.0).cgPath
  141. context.addPath(clipPath)
  142. context.setFillColor(UIColor.secondarySystemBackground.cgColor)
  143. context.setStrokeColor(UIColor.label.cgColor)
  144. context.closePath()
  145. context.drawPath(using: .fillStroke)
  146. // add the text
  147. labelText.draw(with: rectangle, options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
  148. }
  149. override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
  150. if entry.data != nil {
  151. //var multiplier = entry.data as! Double * 100.0
  152. //labelText = String(format: "%.0f%%", multiplier)
  153. labelText = entry.data as? String ?? ""
  154. } else {
  155. labelText = String(entry.y)
  156. }
  157. }
  158. private func customString(_ value: Double) -> String {
  159. let formattedString = PillMarker.formatter.string(from: TimeInterval(value))!
  160. // using this to convert the left axis values formatting, ie 2 min
  161. return "\(formattedString)"
  162. }
  163. }