CubicLineChartViewController.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // CubicLineChartViewController.swift
  3. // ChartsDemo-iOS
  4. //
  5. // Created by Jacob Christie on 2017-07-09.
  6. // Copyright © 2017 jc. All rights reserved.
  7. //
  8. import UIKit
  9. import Charts
  10. private class CubicLineSampleFillFormatter: IFillFormatter {
  11. func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
  12. return -10
  13. }
  14. }
  15. class CubicLineChartViewController: DemoBaseViewController {
  16. @IBOutlet var chartView: LineChartView!
  17. @IBOutlet var sliderX: UISlider!
  18. @IBOutlet var sliderY: UISlider!
  19. @IBOutlet var sliderTextX: UITextField!
  20. @IBOutlet var sliderTextY: UITextField!
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. // Do any additional setup after loading the view.
  24. self.title = "Cubic Line Chart"
  25. self.options = [.toggleValues,
  26. .toggleFilled,
  27. .toggleCircles,
  28. .toggleCubic,
  29. .toggleHorizontalCubic,
  30. .toggleStepped,
  31. .toggleHighlight,
  32. .animateX,
  33. .animateY,
  34. .animateXY,
  35. .saveToGallery,
  36. .togglePinchZoom,
  37. .toggleAutoScaleMinMax,
  38. .toggleData]
  39. chartView.delegate = self
  40. chartView.setViewPortOffsets(left: 0, top: 20, right: 0, bottom: 0)
  41. chartView.backgroundColor = UIColor(red: 104/255, green: 241/255, blue: 175/255, alpha: 1)
  42. chartView.dragEnabled = true
  43. chartView.setScaleEnabled(true)
  44. chartView.pinchZoomEnabled = false
  45. chartView.maxHighlightDistance = 300
  46. chartView.xAxis.enabled = false
  47. let yAxis = chartView.leftAxis
  48. yAxis.labelFont = UIFont(name: "HelveticaNeue-Light", size:12)!
  49. yAxis.setLabelCount(6, force: false)
  50. yAxis.labelTextColor = .white
  51. yAxis.labelPosition = .insideChart
  52. yAxis.axisLineColor = .white
  53. chartView.rightAxis.enabled = false
  54. chartView.legend.enabled = false
  55. sliderX.value = 45
  56. sliderY.value = 100
  57. self.slidersValueChanged(nil)
  58. chartView.animate(xAxisDuration: 2, yAxisDuration: 2)
  59. }
  60. override func updateChartData() {
  61. if self.shouldHideData {
  62. chartView.data = nil
  63. return
  64. }
  65. self.setDataCount(Int(sliderX.value + 1), range: UInt32(sliderY.value))
  66. }
  67. func setDataCount(_ count: Int, range: UInt32) {
  68. let yVals1 = (0..<count).map { (i) -> ChartDataEntry in
  69. let mult = range + 1
  70. let val = Double(arc4random_uniform(mult) + 20)
  71. return ChartDataEntry(x: Double(i), y: val)
  72. }
  73. let set1 = LineChartDataSet(values: yVals1, label: "DataSet 1")
  74. set1.mode = .cubicBezier
  75. set1.drawCirclesEnabled = false
  76. set1.lineWidth = 1.8
  77. set1.circleRadius = 4
  78. set1.setCircleColor(.white)
  79. set1.highlightColor = UIColor(red: 244/255, green: 117/255, blue: 117/255, alpha: 1)
  80. set1.fillColor = .white
  81. set1.fillAlpha = 1
  82. set1.drawHorizontalHighlightIndicatorEnabled = false
  83. set1.fillFormatter = CubicLineSampleFillFormatter()
  84. let data = LineChartData(dataSet: set1)
  85. data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 9)!)
  86. data.setDrawValues(false)
  87. chartView.data = data
  88. }
  89. override func optionTapped(_ option: Option) {
  90. switch option {
  91. case .toggleFilled:
  92. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  93. set.drawFilledEnabled = !set.drawFilledEnabled
  94. }
  95. chartView.setNeedsDisplay()
  96. case .toggleCircles:
  97. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  98. set.drawCirclesEnabled = !set.drawCirclesEnabled
  99. }
  100. chartView.setNeedsDisplay()
  101. case .toggleCubic:
  102. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  103. set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
  104. }
  105. chartView.setNeedsDisplay()
  106. case .toggleStepped:
  107. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  108. set.mode = (set.mode == .stepped) ? .linear : .stepped
  109. }
  110. chartView.setNeedsDisplay()
  111. case .toggleHorizontalCubic:
  112. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  113. set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
  114. }
  115. chartView.setNeedsDisplay()
  116. default:
  117. super.handleOption(option, forChartView: chartView)
  118. }
  119. }
  120. // MARK: - Actions
  121. @IBAction func slidersValueChanged(_ sender: Any?) {
  122. sliderTextX.text = "\(Int(sliderX.value))"
  123. sliderTextY.text = "\(Int(sliderY.value))"
  124. self.updateChartData()
  125. }
  126. }