Chart.swift 5.9 KB

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