ColoredLineChartViewController.swift 2.5 KB

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