PieChartViewController.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // PieChartViewController.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 PieChartViewController: DemoBaseViewController {
  13. @IBOutlet var chartView: PieChartView!
  14. @IBOutlet var sliderX: UISlider!
  15. @IBOutlet var sliderY: UISlider!
  16. @IBOutlet var sliderTextX: UITextField!
  17. @IBOutlet var sliderTextY: UITextField!
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Do any additional setup after loading the view.
  21. self.title = "Pie Chart"
  22. self.options = [.toggleValues,
  23. .toggleXValues,
  24. .togglePercent,
  25. .toggleHole,
  26. .toggleIcons,
  27. .animateX,
  28. .animateY,
  29. .animateXY,
  30. .spin,
  31. .drawCenter,
  32. .saveToGallery,
  33. .toggleData]
  34. self.setup(pieChartView: chartView)
  35. chartView.delegate = self
  36. let l = chartView.legend
  37. l.horizontalAlignment = .right
  38. l.verticalAlignment = .top
  39. l.orientation = .vertical
  40. l.xEntrySpace = 7
  41. l.yEntrySpace = 0
  42. l.yOffset = 0
  43. // chartView.legend = l
  44. // entry label styling
  45. chartView.entryLabelColor = .white
  46. chartView.entryLabelFont = .systemFont(ofSize: 12, weight: .light)
  47. sliderX.value = 4
  48. sliderY.value = 100
  49. self.slidersValueChanged(nil)
  50. chartView.animate(xAxisDuration: 1.4, easingOption: .easeOutBack)
  51. }
  52. override func updateChartData() {
  53. if self.shouldHideData {
  54. chartView.data = nil
  55. return
  56. }
  57. self.setDataCount(Int(sliderX.value), range: UInt32(sliderY.value))
  58. }
  59. func setDataCount(_ count: Int, range: UInt32) {
  60. let entries = (0..<count).map { (i) -> PieChartDataEntry in
  61. // 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.
  62. return PieChartDataEntry(value: Double(arc4random_uniform(range) + range / 5),
  63. label: parties[i % parties.count],
  64. icon: #imageLiteral(resourceName: "icon"))
  65. }
  66. let set = PieChartDataSet(entries: entries, label: "Election Results")
  67. set.drawIconsEnabled = false
  68. set.sliceSpace = 2
  69. set.colors = ChartColorTemplates.vordiplom()
  70. + ChartColorTemplates.joyful()
  71. + ChartColorTemplates.colorful()
  72. + ChartColorTemplates.liberty()
  73. + ChartColorTemplates.pastel()
  74. + [UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)]
  75. let data = PieChartData(dataSet: set)
  76. let pFormatter = NumberFormatter()
  77. pFormatter.numberStyle = .percent
  78. pFormatter.maximumFractionDigits = 1
  79. pFormatter.multiplier = 1
  80. pFormatter.percentSymbol = " %"
  81. data.setValueFormatter(DefaultValueFormatter(formatter: pFormatter))
  82. data.setValueFont(.systemFont(ofSize: 11, weight: .light))
  83. data.setValueTextColor(.white)
  84. chartView.data = data
  85. chartView.highlightValues(nil)
  86. }
  87. override func optionTapped(_ option: Option) {
  88. switch option {
  89. case .toggleXValues:
  90. chartView.drawEntryLabelsEnabled = !chartView.drawEntryLabelsEnabled
  91. chartView.setNeedsDisplay()
  92. case .togglePercent:
  93. chartView.usePercentValuesEnabled = !chartView.usePercentValuesEnabled
  94. chartView.setNeedsDisplay()
  95. case .toggleHole:
  96. chartView.drawHoleEnabled = !chartView.drawHoleEnabled
  97. chartView.setNeedsDisplay()
  98. case .drawCenter:
  99. chartView.drawCenterTextEnabled = !chartView.drawCenterTextEnabled
  100. chartView.setNeedsDisplay()
  101. case .animateX:
  102. chartView.animate(xAxisDuration: 1.4)
  103. case .animateY:
  104. chartView.animate(yAxisDuration: 1.4)
  105. case .animateXY:
  106. chartView.animate(xAxisDuration: 1.4, yAxisDuration: 1.4)
  107. case .spin:
  108. chartView.spin(duration: 2,
  109. fromAngle: chartView.rotationAngle,
  110. toAngle: chartView.rotationAngle + 360,
  111. easingOption: .easeInCubic)
  112. default:
  113. handleOption(option, forChartView: chartView)
  114. }
  115. }
  116. // MARK: - Actions
  117. @IBAction func slidersValueChanged(_ sender: Any?) {
  118. sliderTextX.text = "\(Int(sliderX.value))"
  119. sliderTextY.text = "\(Int(sliderY.value))"
  120. self.updateChartData()
  121. }
  122. }