Chart.swift 6.2 KB

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