SinusBarChartViewController.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // SinusBarChartViewController.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 SinusBarChartViewController: DemoBaseViewController {
  13. @IBOutlet var chartView: BarChartView!
  14. @IBOutlet var sliderX: UISlider!
  15. @IBOutlet var sliderTextX: UITextField!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. // Do any additional setup after loading the view.
  19. self.title = "Sinus Bar Chart"
  20. self.options = [.toggleValues,
  21. .toggleHighlight,
  22. .animateX,
  23. .animateY,
  24. .animateXY,
  25. .saveToGallery,
  26. .togglePinchZoom,
  27. .toggleAutoScaleMinMax,
  28. .toggleData]
  29. chartView.delegate = self
  30. chartView.chartDescription.enabled = false
  31. chartView.drawBarShadowEnabled = false
  32. chartView.drawValueAboveBarEnabled = false
  33. chartView.maxVisibleCount = 60
  34. let xAxis = chartView.xAxis
  35. xAxis.labelPosition = .bottom
  36. xAxis.enabled = false
  37. let leftAxis = chartView.leftAxis
  38. leftAxis.labelCount = 6
  39. leftAxis.axisMinimum = -2.5
  40. leftAxis.axisMaximum = 2.5
  41. leftAxis.granularityEnabled = true
  42. leftAxis.granularity = 0.1
  43. let rightAxis = chartView.rightAxis
  44. rightAxis.labelCount = 6
  45. rightAxis.axisMinimum = -2.5
  46. rightAxis.axisMaximum = 2.5
  47. rightAxis.granularity = 0.1
  48. let l = chartView.legend
  49. l.horizontalAlignment = .left
  50. l.verticalAlignment = .bottom
  51. l.orientation = .horizontal
  52. l.drawInside = false
  53. l.form = .square
  54. l.formSize = 9
  55. l.font = .systemFont(ofSize: 11)
  56. l.xEntrySpace = 4
  57. // chartView.legend = l
  58. sliderX.value = 150
  59. slidersValueChanged(nil)
  60. chartView.animate(xAxisDuration: 2, yAxisDuration: 2)
  61. }
  62. override func updateChartData() {
  63. if self.shouldHideData {
  64. chartView.data = nil
  65. return
  66. }
  67. self.setDataCount(Int(sliderX.value))
  68. }
  69. func setDataCount(_ count: Int) {
  70. let entries = (0..<count).map {
  71. BarChartDataEntry(x: Double($0), y: sin(.pi * Double($0%128) / 64))
  72. }
  73. let set = BarChartDataSet(entries: entries, label: "Sinus Function")
  74. set.setColor(UIColor(red: 240/255, green: 120/255, blue: 123/255, alpha: 1))
  75. let data = BarChartData(dataSet: set)
  76. data.setValueFont(.systemFont(ofSize: 10, weight: .light))
  77. data.setDrawValues(false)
  78. data.barWidth = 0.8
  79. chartView.data = data
  80. }
  81. override func optionTapped(_ option: Option) {
  82. super.handleOption(option, forChartView: chartView)
  83. }
  84. @IBAction func slidersValueChanged(_ sender: Any?) {
  85. sliderTextX.text = "\(Int(sliderX.value))"
  86. self.updateChartData()
  87. }
  88. }