LineChart1ViewController.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // LineChart1ViewController.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 LineChart1ViewController: DemoBaseViewController {
  13. @IBOutlet var chartView: LineChartView!
  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 = "Line Chart 1"
  22. self.options = [.toggleValues,
  23. .toggleFilled,
  24. .toggleCircles,
  25. .toggleCubic,
  26. .toggleHorizontalCubic,
  27. .toggleIcons,
  28. .toggleStepped,
  29. .toggleHighlight,
  30. .animateX,
  31. .animateY,
  32. .animateXY,
  33. .saveToGallery,
  34. .togglePinchZoom,
  35. .toggleAutoScaleMinMax,
  36. .toggleData]
  37. chartView.delegate = self
  38. chartView.chartDescription?.enabled = false
  39. chartView.dragEnabled = true
  40. chartView.setScaleEnabled(true)
  41. chartView.pinchZoomEnabled = true
  42. // x-axis limit line
  43. let llXAxis = ChartLimitLine(limit: 10, label: "Index 10")
  44. llXAxis.lineWidth = 4
  45. llXAxis.lineDashLengths = [10, 10, 0]
  46. llXAxis.labelPosition = .bottomRight
  47. llXAxis.valueFont = .systemFont(ofSize: 10)
  48. chartView.xAxis.gridLineDashLengths = [10, 10]
  49. chartView.xAxis.gridLineDashPhase = 0
  50. let ll1 = ChartLimitLine(limit: 150, label: "Upper Limit")
  51. ll1.lineWidth = 4
  52. ll1.lineDashLengths = [5, 5]
  53. ll1.labelPosition = .topRight
  54. ll1.valueFont = .systemFont(ofSize: 10)
  55. let ll2 = ChartLimitLine(limit: -30, label: "Lower Limit")
  56. ll2.lineWidth = 4
  57. ll2.lineDashLengths = [5,5]
  58. ll2.labelPosition = .bottomRight
  59. ll2.valueFont = .systemFont(ofSize: 10)
  60. let leftAxis = chartView.leftAxis
  61. leftAxis.removeAllLimitLines()
  62. leftAxis.addLimitLine(ll1)
  63. leftAxis.addLimitLine(ll2)
  64. leftAxis.axisMaximum = 200
  65. leftAxis.axisMinimum = -50
  66. leftAxis.gridLineDashLengths = [5, 5]
  67. leftAxis.drawLimitLinesBehindDataEnabled = true
  68. chartView.rightAxis.enabled = false
  69. //[_chartView.viewPortHandler setMaximumScaleY: 2.f];
  70. //[_chartView.viewPortHandler setMaximumScaleX: 2.f];
  71. let marker = BalloonMarker(color: UIColor(white: 180/255, alpha: 1),
  72. font: .systemFont(ofSize: 12),
  73. textColor: .white,
  74. insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8))
  75. marker.chartView = chartView
  76. marker.minimumSize = CGSize(width: 80, height: 40)
  77. chartView.marker = marker
  78. chartView.legend.form = .line
  79. sliderX.value = 45
  80. sliderY.value = 100
  81. slidersValueChanged(nil)
  82. chartView.animate(xAxisDuration: 2.5)
  83. }
  84. override func updateChartData() {
  85. if self.shouldHideData {
  86. chartView.data = nil
  87. return
  88. }
  89. self.setDataCount(Int(sliderX.value), range: UInt32(sliderY.value))
  90. }
  91. func setDataCount(_ count: Int, range: UInt32) {
  92. let values = (0..<count).map { (i) -> ChartDataEntry in
  93. let val = Double(arc4random_uniform(range) + 3)
  94. return ChartDataEntry(x: Double(i), y: val, icon: #imageLiteral(resourceName: "icon"))
  95. }
  96. let set1 = LineChartDataSet(entries: values, label: "DataSet 1")
  97. set1.drawIconsEnabled = false
  98. set1.lineDashLengths = [5, 2.5]
  99. set1.highlightLineDashLengths = [5, 2.5]
  100. set1.setColor(.black)
  101. set1.setCircleColor(.black)
  102. set1.lineWidth = 1
  103. set1.circleRadius = 3
  104. set1.drawCircleHoleEnabled = false
  105. set1.valueFont = .systemFont(ofSize: 9)
  106. set1.formLineDashLengths = [5, 2.5]
  107. set1.formLineWidth = 1
  108. set1.formSize = 15
  109. let gradientColors = [ChartColorTemplates.colorFromString("#00ff0000").cgColor,
  110. ChartColorTemplates.colorFromString("#ffff0000").cgColor]
  111. let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!
  112. set1.fillAlpha = 1
  113. set1.fill = Fill(linearGradient: gradient, angle: 90) //.linearGradient(gradient, angle: 90)
  114. set1.drawFilledEnabled = true
  115. let data = LineChartData(dataSet: set1)
  116. chartView.data = data
  117. }
  118. override func optionTapped(_ option: Option) {
  119. switch option {
  120. case .toggleFilled:
  121. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  122. set.drawFilledEnabled = !set.drawFilledEnabled
  123. }
  124. chartView.setNeedsDisplay()
  125. case .toggleCircles:
  126. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  127. set.drawCirclesEnabled = !set.drawCirclesEnabled
  128. }
  129. chartView.setNeedsDisplay()
  130. case .toggleCubic:
  131. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  132. set.mode = (set.mode == .cubicBezier) ? .linear : .cubicBezier
  133. }
  134. chartView.setNeedsDisplay()
  135. case .toggleStepped:
  136. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  137. set.mode = (set.mode == .stepped) ? .linear : .stepped
  138. }
  139. chartView.setNeedsDisplay()
  140. case .toggleHorizontalCubic:
  141. for set in chartView.data!.dataSets as! [LineChartDataSet] {
  142. set.mode = (set.mode == .cubicBezier) ? .horizontalBezier : .cubicBezier
  143. }
  144. chartView.setNeedsDisplay()
  145. default:
  146. super.handleOption(option, forChartView: chartView)
  147. }
  148. }
  149. @IBAction func slidersValueChanged(_ sender: Any?) {
  150. sliderTextX.text = "\(Int(sliderX.value))"
  151. sliderTextY.text = "\(Int(sliderY.value))"
  152. self.updateChartData()
  153. }
  154. }