Chart.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // LoopFollow
  2. // Chart.swift
  3. import Charts
  4. import Foundation
  5. final class OverrideFillFormatter: FillFormatter {
  6. func getFillLinePosition(dataSet: Charts.LineChartDataSetProtocol, dataProvider _: Charts.LineChartDataProvider) -> CGFloat {
  7. return CGFloat(dataSet.entryForIndex(0)!.y)
  8. // return 375
  9. }
  10. }
  11. final class basalFillFormatter: FillFormatter {
  12. func getFillLinePosition(dataSet _: Charts.LineChartDataSetProtocol, dataProvider _: Charts.LineChartDataProvider) -> CGFloat {
  13. return 0
  14. }
  15. }
  16. final class ChartXValueFormatter: AxisValueFormatter {
  17. func stringForValue(_ value: Double, axis _: AxisBase?) -> String {
  18. let dateFormatter = DateFormatter()
  19. // let timezoneOffset = TimeZone.current.secondsFromGMT()
  20. // let epochTimezoneOffset = value + Double(timezoneOffset)
  21. if dateTimeUtils.is24Hour() {
  22. dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
  23. } else {
  24. dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
  25. }
  26. // let date = Date(timeIntervalSince1970: epochTimezoneOffset)
  27. let date = Date(timeIntervalSince1970: value)
  28. let formattedDate = dateFormatter.string(from: date)
  29. return formattedDate
  30. }
  31. }
  32. final class ChartYDataValueFormatter: ValueFormatter {
  33. func stringForValue(_: Double, entry: ChartDataEntry, dataSetIndex _: Int, viewPortHandler _: ViewPortHandler?) -> String {
  34. if entry.data != nil {
  35. return entry.data as? String ?? ""
  36. } else {
  37. return ""
  38. }
  39. }
  40. }
  41. final class ChartYOverrideValueFormatter: ValueFormatter {
  42. func stringForValue(_: Double, entry: ChartDataEntry, dataSetIndex _: Int, viewPortHandler _: ViewPortHandler?) -> String {
  43. if entry.data != nil {
  44. return entry.data as? String ?? ""
  45. } else {
  46. return ""
  47. }
  48. }
  49. }
  50. final class ChartYMMOLValueFormatter: AxisValueFormatter {
  51. func stringForValue(_ value: Double, axis _: AxisBase?) -> String {
  52. return Localizer.toDisplayUnits(String(value))
  53. }
  54. }
  55. class PillMarker: MarkerImage {
  56. private(set) var color: UIColor
  57. private(set) var font: UIFont
  58. private(set) var textColor: UIColor
  59. private var labelText: String = ""
  60. private var attrs: [NSAttributedString.Key: AnyObject]!
  61. static let formatter: DateComponentsFormatter = {
  62. let f = DateComponentsFormatter()
  63. f.allowedUnits = [.minute, .second]
  64. f.unitsStyle = .short
  65. return f
  66. }()
  67. init(color: UIColor, font: UIFont, textColor: UIColor) {
  68. self.color = color
  69. self.font = font
  70. self.textColor = textColor
  71. let paragraphStyle = NSMutableParagraphStyle()
  72. paragraphStyle.alignment = .center
  73. attrs = [.font: font, .paragraphStyle: paragraphStyle, .foregroundColor: textColor, .baselineOffset: NSNumber(value: -4)]
  74. super.init()
  75. }
  76. override func draw(context: CGContext, point: CGPoint) {
  77. // custom padding around text
  78. let labelWidth = labelText.size(withAttributes: attrs).width + 10
  79. // if you modify labelHeigh you will have to tweak baselineOffset in attrs
  80. let labelHeight = labelText.size(withAttributes: attrs).height + 4
  81. // place pill above the marker, centered along x
  82. var rectangle = CGRect(x: point.x, y: point.y, width: labelWidth, height: labelHeight)
  83. rectangle.origin.x -= rectangle.width / 2.0
  84. var spacing: CGFloat = 20
  85. if point.y < 300 { spacing = -40 }
  86. rectangle.origin.y -= rectangle.height + spacing
  87. // rounded rect
  88. let clipPath = UIBezierPath(roundedRect: rectangle, cornerRadius: 6.0).cgPath
  89. context.addPath(clipPath)
  90. context.setFillColor(UIColor.secondarySystemBackground.cgColor)
  91. context.setStrokeColor(UIColor.label.cgColor)
  92. context.closePath()
  93. context.drawPath(using: .fillStroke)
  94. // add the text
  95. labelText.draw(with: rectangle, options: .usesLineFragmentOrigin, attributes: attrs, context: nil)
  96. }
  97. override func refreshContent(entry: ChartDataEntry, highlight _: Highlight) {
  98. if entry.data != nil {
  99. // var multiplier = entry.data as! Double * 100.0
  100. // labelText = String(format: "%.0f%%", multiplier)
  101. labelText = entry.data as? String ?? ""
  102. } else {
  103. labelText = String(entry.y)
  104. }
  105. }
  106. private func customString(_ value: Double) -> String {
  107. let formattedString = PillMarker.formatter.string(from: TimeInterval(value))!
  108. // using this to convert the left axis values formatting, ie 2 min
  109. return "\(formattedString)"
  110. }
  111. }