RadarChartViewController.swift 5.8 KB

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