RadarChartViewController.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // RadarChartViewController.swift
  3. // ChartsDemo-iOS
  4. //
  5. // Created by Jacob Christie on 2017-07-09.
  6. // Copyright © 2017 jc. All rights reserved.
  7. //
  8. #if canImport(UIKit)
  9. import UIKit
  10. #endif
  11. import Charts
  12. class RadarChartViewController: DemoBaseViewController {
  13. @IBOutlet var chartView: RadarChartView!
  14. let activities = ["Burger", "Steak", "Salad", "Pasta", "Pizza"]
  15. var originalBarBgColor: UIColor!
  16. var originalBarTintColor: UIColor!
  17. var originalBarStyle: UIBarStyle!
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Do any additional setup after loading the view.
  21. self.title = "Radar Chart"
  22. self.options = [.toggleValues,
  23. .toggleHighlight,
  24. .toggleHighlightCircle,
  25. .toggleXLabels,
  26. .toggleYLabels,
  27. .toggleRotate,
  28. .toggleFilled,
  29. .animateX,
  30. .animateY,
  31. .animateXY,
  32. .spin,
  33. .saveToGallery,
  34. .toggleData]
  35. chartView.delegate = self
  36. chartView.chartDescription.enabled = false
  37. chartView.webLineWidth = 1
  38. chartView.innerWebLineWidth = 1
  39. chartView.webColor = .lightGray
  40. chartView.innerWebColor = .lightGray
  41. chartView.webAlpha = 1
  42. let marker = RadarMarkerView.viewFromXib()!
  43. marker.chartView = chartView
  44. chartView.marker = marker
  45. let xAxis = chartView.xAxis
  46. xAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
  47. xAxis.xOffset = 0
  48. xAxis.yOffset = 0
  49. xAxis.valueFormatter = self
  50. xAxis.labelTextColor = .white
  51. let yAxis = chartView.yAxis
  52. yAxis.labelFont = .systemFont(ofSize: 9, weight: .light)
  53. yAxis.labelCount = 5
  54. yAxis.axisMinimum = 0
  55. yAxis.axisMaximum = 80
  56. yAxis.drawLabelsEnabled = false
  57. let l = chartView.legend
  58. l.horizontalAlignment = .center
  59. l.verticalAlignment = .top
  60. l.orientation = .horizontal
  61. l.drawInside = false
  62. l.font = .systemFont(ofSize: 10, weight: .light)
  63. l.xEntrySpace = 7
  64. l.yEntrySpace = 5
  65. l.textColor = .white
  66. // chartView.legend = l
  67. self.updateChartData()
  68. chartView.animate(xAxisDuration: 1.4, yAxisDuration: 1.4, easingOption: .easeOutBack)
  69. }
  70. override func viewWillAppear(_ animated: Bool) {
  71. super.viewWillAppear(animated)
  72. UIView.animate(withDuration: 0.15) {
  73. let navBar = self.navigationController!.navigationBar
  74. self.originalBarBgColor = navBar.barTintColor
  75. self.originalBarTintColor = navBar.tintColor
  76. self.originalBarStyle = navBar.barStyle
  77. navBar.barTintColor = self.view.backgroundColor
  78. navBar.tintColor = .white
  79. navBar.barStyle = .black
  80. }
  81. }
  82. override func viewWillDisappear(_ animated: Bool) {
  83. super.viewWillDisappear(animated)
  84. UIView.animate(withDuration: 0.15) {
  85. let navBar = self.navigationController!.navigationBar
  86. navBar.barTintColor = self.originalBarBgColor
  87. navBar.tintColor = self.originalBarTintColor
  88. navBar.barStyle = self.originalBarStyle
  89. }
  90. }
  91. override func updateChartData() {
  92. if self.shouldHideData {
  93. chartView.data = nil
  94. return
  95. }
  96. self.setChartData()
  97. }
  98. func setChartData() {
  99. let mult: UInt32 = 80
  100. let min: UInt32 = 20
  101. let cnt = 5
  102. let block: (Int) -> RadarChartDataEntry = { _ in return RadarChartDataEntry(value: Double(arc4random_uniform(mult) + min))}
  103. let entries1 = (0..<cnt).map(block)
  104. let entries2 = (0..<cnt).map(block)
  105. let set1 = RadarChartDataSet(entries: entries1, label: "Last Week")
  106. set1.setColor(UIColor(red: 103/255, green: 110/255, blue: 129/255, alpha: 1))
  107. set1.fillColor = UIColor(red: 103/255, green: 110/255, blue: 129/255, alpha: 1)
  108. set1.drawFilledEnabled = true
  109. set1.fillAlpha = 0.7
  110. set1.lineWidth = 2
  111. set1.drawHighlightCircleEnabled = true
  112. set1.setDrawHighlightIndicators(false)
  113. let set2 = RadarChartDataSet(entries: entries2, label: "This Week")
  114. set2.setColor(UIColor(red: 121/255, green: 162/255, blue: 175/255, alpha: 1))
  115. set2.fillColor = UIColor(red: 121/255, green: 162/255, blue: 175/255, alpha: 1)
  116. set2.drawFilledEnabled = true
  117. set2.fillAlpha = 0.7
  118. set2.lineWidth = 2
  119. set2.drawHighlightCircleEnabled = true
  120. set2.setDrawHighlightIndicators(false)
  121. let data: RadarChartData = [set1, set2]
  122. data.setValueFont(.systemFont(ofSize: 8, weight: .light))
  123. data.setDrawValues(false)
  124. data.setValueTextColor(.white)
  125. chartView.data = data
  126. }
  127. override func optionTapped(_ option: Option) {
  128. guard let data = chartView.data else { return }
  129. switch option {
  130. case .toggleXLabels:
  131. chartView.xAxis.drawLabelsEnabled = !chartView.xAxis.drawLabelsEnabled
  132. chartView.data?.notifyDataChanged()
  133. chartView.notifyDataSetChanged()
  134. chartView.setNeedsDisplay()
  135. case .toggleYLabels:
  136. chartView.yAxis.drawLabelsEnabled = !chartView.yAxis.drawLabelsEnabled
  137. chartView.setNeedsDisplay()
  138. case .toggleRotate:
  139. chartView.rotationEnabled = !chartView.rotationEnabled
  140. case .toggleFilled:
  141. for case let set as RadarChartDataSet in data {
  142. set.drawFilledEnabled = !set.drawFilledEnabled
  143. }
  144. chartView.setNeedsDisplay()
  145. case .toggleHighlightCircle:
  146. for case let set as RadarChartDataSet in data {
  147. set.drawHighlightCircleEnabled = !set.drawHighlightCircleEnabled
  148. }
  149. chartView.setNeedsDisplay()
  150. case .animateX:
  151. chartView.animate(xAxisDuration: 1.4)
  152. case .animateY:
  153. chartView.animate(yAxisDuration: 1.4)
  154. case .animateXY:
  155. chartView.animate(xAxisDuration: 1.4, yAxisDuration: 1.4)
  156. case .spin:
  157. chartView.spin(duration: 2, fromAngle: chartView.rotationAngle, toAngle: chartView.rotationAngle + 360, easingOption: .easeInCubic)
  158. default:
  159. super.handleOption(option, forChartView: chartView)
  160. }
  161. }
  162. }
  163. extension RadarChartViewController: AxisValueFormatter {
  164. func stringForValue(_ value: Double, axis: AxisBase?) -> String {
  165. return activities[Int(value) % activities.count]
  166. }
  167. }