HalfPieChartViewController.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // HalfPieChartViewController.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 HalfPieChartViewController: DemoBaseViewController {
  13. @IBOutlet var chartView: PieChartView!
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // Do any additional setup after loading the view.
  17. self.title = "Half Pie Chart"
  18. self.options = [.toggleValues,
  19. .toggleXValues,
  20. .togglePercent,
  21. .toggleHole,
  22. .animateX,
  23. .animateY,
  24. .animateXY,
  25. .spin,
  26. .drawCenter,
  27. .saveToGallery,
  28. .toggleData]
  29. self.setup(pieChartView: chartView)
  30. chartView.delegate = self
  31. chartView.holeColor = .white
  32. chartView.transparentCircleColor = NSUIColor.white.withAlphaComponent(0.43)
  33. chartView.holeRadiusPercent = 0.58
  34. chartView.rotationEnabled = false
  35. chartView.highlightPerTapEnabled = true
  36. chartView.maxAngle = 180 // Half chart
  37. chartView.rotationAngle = 180 // Rotate to make the half on the upper side
  38. chartView.centerTextOffset = CGPoint(x: 0, y: -20)
  39. let l = chartView.legend
  40. l.horizontalAlignment = .center
  41. l.verticalAlignment = .top
  42. l.orientation = .horizontal
  43. l.drawInside = false
  44. l.xEntrySpace = 7
  45. l.yEntrySpace = 0
  46. l.yOffset = 0
  47. // chartView.legend = l
  48. // entry label styling
  49. chartView.entryLabelColor = .white
  50. chartView.entryLabelFont = UIFont(name:"HelveticaNeue-Light", size:12)!
  51. self.updateChartData()
  52. chartView.animate(xAxisDuration: 1.4, easingOption: .easeOutBack)
  53. }
  54. override func updateChartData() {
  55. if self.shouldHideData {
  56. chartView.data = nil
  57. return
  58. }
  59. self.setDataCount(4, range: 100)
  60. }
  61. func setDataCount(_ count: Int, range: UInt32) {
  62. let entries = (0..<count).map { (i) -> PieChartDataEntry in
  63. // IMPORTANT: In a PieChart, no values (Entry) should have the same xIndex (even if from different DataSets), since no values can be drawn above each other.
  64. return PieChartDataEntry(value: Double(arc4random_uniform(range) + range / 5),
  65. label: parties[i % parties.count])
  66. }
  67. let set = PieChartDataSet(entries: entries, label: "Election Results")
  68. set.sliceSpace = 3
  69. set.selectionShift = 5
  70. set.colors = ChartColorTemplates.material()
  71. let data = PieChartData(dataSet: set)
  72. let pFormatter = NumberFormatter()
  73. pFormatter.numberStyle = .percent
  74. pFormatter.maximumFractionDigits = 1
  75. pFormatter.multiplier = 1
  76. pFormatter.percentSymbol = " %"
  77. data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
  78. data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 11)!)
  79. data.setValueTextColor(.white)
  80. chartView.data = data
  81. chartView.setNeedsDisplay()
  82. }
  83. override func optionTapped(_ option: Option) {
  84. switch option {
  85. case .toggleXValues:
  86. chartView.drawEntryLabelsEnabled = !chartView.drawEntryLabelsEnabled
  87. chartView.setNeedsDisplay()
  88. case .togglePercent:
  89. chartView.usePercentValuesEnabled = !chartView.usePercentValuesEnabled
  90. chartView.setNeedsDisplay()
  91. case .toggleHole:
  92. chartView.drawHoleEnabled = !chartView.drawHoleEnabled
  93. chartView.setNeedsDisplay()
  94. case .drawCenter:
  95. chartView.drawCenterTextEnabled = !chartView.drawCenterTextEnabled
  96. chartView.setNeedsDisplay()
  97. case .animateX:
  98. chartView.animate(xAxisDuration: 1.4)
  99. case .animateY:
  100. chartView.animate(yAxisDuration: 1.4)
  101. case .animateXY:
  102. chartView.animate(xAxisDuration: 1.4, yAxisDuration: 1.4)
  103. case .spin:
  104. chartView.spin(duration: 2,
  105. fromAngle: chartView.rotationAngle,
  106. toAngle: chartView.rotationAngle + 360,
  107. easingOption: .easeInCubic)
  108. default:
  109. handleOption(option, forChartView: chartView)
  110. }
  111. }
  112. }