NegativeStackedBarChartViewController.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // NegativeStackedBarChartViewController.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 NegativeStackedBarChartViewController: DemoBaseViewController {
  13. @IBOutlet var chartView: HorizontalBarChartView!
  14. lazy var customFormatter: NumberFormatter = {
  15. let formatter = NumberFormatter()
  16. formatter.negativePrefix = ""
  17. formatter.positiveSuffix = "m"
  18. formatter.negativeSuffix = "m"
  19. formatter.minimumSignificantDigits = 1
  20. formatter.minimumFractionDigits = 1
  21. return formatter
  22. }()
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. // Do any additional setup after loading the view.
  26. self.title = "Stacked Bar Chart Negative"
  27. self.options = [.toggleValues,
  28. .toggleIcons,
  29. .toggleHighlight,
  30. .animateX,
  31. .animateY,
  32. .animateXY,
  33. .saveToGallery,
  34. .togglePinchZoom,
  35. .toggleAutoScaleMinMax,
  36. .toggleData,
  37. .toggleBarBorders]
  38. chartView.delegate = self
  39. chartView.chartDescription.enabled = false
  40. chartView.drawBarShadowEnabled = false
  41. chartView.drawValueAboveBarEnabled = true
  42. chartView.leftAxis.enabled = false
  43. let rightAxis = chartView.rightAxis
  44. rightAxis.axisMaximum = 25
  45. rightAxis.axisMinimum = -25
  46. rightAxis.drawZeroLineEnabled = true
  47. rightAxis.labelCount = 7
  48. rightAxis.valueFormatter = DefaultAxisValueFormatter(formatter: customFormatter)
  49. rightAxis.labelFont = .systemFont(ofSize: 9)
  50. let xAxis = chartView.xAxis
  51. xAxis.labelPosition = .bothSided
  52. xAxis.drawAxisLineEnabled = false
  53. xAxis.axisMinimum = 0
  54. xAxis.axisMaximum = 110
  55. xAxis.centerAxisLabelsEnabled = true
  56. xAxis.labelCount = 12
  57. xAxis.granularity = 10
  58. xAxis.valueFormatter = self
  59. xAxis.labelFont = .systemFont(ofSize: 9)
  60. let l = chartView.legend
  61. l.horizontalAlignment = .right
  62. l.verticalAlignment = .bottom
  63. l.orientation = .horizontal
  64. l.formSize = 8
  65. l.formToTextSpace = 8
  66. l.xEntrySpace = 6
  67. // chartView.legend = l
  68. self.updateChartData()
  69. }
  70. override func updateChartData() {
  71. if self.shouldHideData {
  72. chartView.data = nil
  73. return
  74. }
  75. self.setChartData()
  76. }
  77. func setChartData() {
  78. let yVals = [BarChartDataEntry(x: 5, yValues: [-10, 10]),
  79. BarChartDataEntry(x: 15, yValues: [-12, 13]),
  80. BarChartDataEntry(x: 25, yValues: [-15, 15]),
  81. BarChartDataEntry(x: 35, yValues: [-17, 17]),
  82. BarChartDataEntry(x: 45, yValues: [-19, 120]),
  83. BarChartDataEntry(x: 55, yValues: [-19, 19]),
  84. BarChartDataEntry(x: 65, yValues: [-16, 16]),
  85. BarChartDataEntry(x: 75, yValues: [-13, 14]),
  86. BarChartDataEntry(x: 85, yValues: [-10, 11]),
  87. BarChartDataEntry(x: 95, yValues: [-5, 6]),
  88. BarChartDataEntry(x: 105, yValues: [-1, 2])
  89. ]
  90. let set = BarChartDataSet(entries: yVals, label: "Age Distribution")
  91. set.drawIconsEnabled = false
  92. set.valueFormatter = DefaultValueFormatter(formatter: customFormatter)
  93. set.valueFont = .systemFont(ofSize: 7)
  94. set.axisDependency = .right
  95. set.colors = [UIColor(red: 67/255, green: 67/255, blue: 72/255, alpha: 1),
  96. UIColor(red: 124/255, green: 181/255, blue: 236/255, alpha: 1)
  97. ]
  98. set.stackLabels = ["Men", "Women"]
  99. let data = BarChartData(dataSet: set)
  100. data.barWidth = 8.5
  101. chartView.data = data
  102. chartView.setNeedsDisplay()
  103. }
  104. override func optionTapped(_ option: Option) {
  105. super.handleOption(option, forChartView: chartView)
  106. }
  107. }
  108. extension NegativeStackedBarChartViewController: AxisValueFormatter {
  109. func stringForValue(_ value: Double, axis: AxisBase?) -> String {
  110. return String(format: "%03.0f-%03.0f", value, value + 10)
  111. }
  112. }