ColoredLineChartViewController.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // ColoredLineChartViewController.swift
  3. // ChartsDemo-iOS
  4. //
  5. // Created by Jacob Christie on 2017-07-04.
  6. // Copyright © 2017 jc. All rights reserved.
  7. //
  8. import UIKit
  9. import Charts
  10. class ColoredLineChartViewController: DemoBaseViewController {
  11. @IBOutlet var chartViews: [LineChartView]!
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. self.title = "Colored Line Chart"
  15. let colors = [UIColor(red: 137/255, green: 230/255, blue: 81/255, alpha: 1),
  16. UIColor(red: 240/255, green: 240/255, blue: 30/255, alpha: 1),
  17. UIColor(red: 89/255, green: 199/255, blue: 250/255, alpha: 1),
  18. UIColor(red: 250/255, green: 104/255, blue: 104/255, alpha: 1)]
  19. for (i, chartView) in chartViews.enumerated() {
  20. let data = dataWithCount(36, range: 100)
  21. data.setValueFont(UIFont(name: "HelveticaNeue", size: 7)!)
  22. setupChart(chartView, data: data, color: colors[i % colors.count])
  23. }
  24. }
  25. func setupChart(_ chart: LineChartView, data: LineChartData, color: UIColor) {
  26. (data.getDataSetByIndex(0) as! LineChartDataSet).circleHoleColor = color
  27. chart.delegate = self
  28. chart.backgroundColor = color
  29. chart.chartDescription?.enabled = false
  30. chart.dragEnabled = true
  31. chart.setScaleEnabled(true)
  32. chart.pinchZoomEnabled = false
  33. chart.setViewPortOffsets(left: 10, top: 0, right: 10, bottom: 0)
  34. chart.legend.enabled = false
  35. chart.leftAxis.enabled = false
  36. chart.leftAxis.spaceTop = 0.4
  37. chart.leftAxis.spaceBottom = 0.4
  38. chart.rightAxis.enabled = false
  39. chart.xAxis.enabled = false
  40. chart.data = data
  41. chart.animate(xAxisDuration: 2.5)
  42. }
  43. func dataWithCount(_ count: Int, range: UInt32) -> LineChartData {
  44. let yVals = (0..<count).map { i -> ChartDataEntry in
  45. let val = Double(arc4random_uniform(range)) + 3
  46. return ChartDataEntry(x: Double(i), y: val)
  47. }
  48. let set1 = LineChartDataSet(values: yVals, label: "DataSet 1")
  49. set1.lineWidth = 1.75
  50. set1.circleRadius = 5.0
  51. set1.circleHoleRadius = 2.5
  52. set1.setColor(.white)
  53. set1.setCircleColor(.white)
  54. set1.highlightColor = .white
  55. set1.drawValuesEnabled = false
  56. return LineChartData(dataSet: set1)
  57. }
  58. }