Chart.swift 5.1 KB

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