BarChartData.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // BarChartData.swift
  3. // Charts
  4. //
  5. // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
  6. // A port of MPAndroidChart for iOS
  7. // Licensed under Apache License 2.0
  8. //
  9. // https://github.com/danielgindi/Charts
  10. //
  11. import Foundation
  12. import CoreGraphics
  13. open class BarChartData: BarLineScatterCandleBubbleChartData
  14. {
  15. public required init()
  16. {
  17. super.init()
  18. }
  19. public override init(dataSets: [ChartDataSetProtocol])
  20. {
  21. super.init(dataSets: dataSets)
  22. }
  23. public required init(arrayLiteral elements: ChartDataSetProtocol...)
  24. {
  25. super.init(dataSets: elements)
  26. }
  27. /// The width of the bars on the x-axis, in values (not pixels)
  28. ///
  29. /// **default**: 0.85
  30. @objc open var barWidth = Double(0.85)
  31. /// Groups all BarDataSet objects this data object holds together by modifying the x-value of their entries.
  32. /// Previously set x-values of entries will be overwritten. Leaves space between bars and groups as specified by the parameters.
  33. /// Do not forget to call notifyDataSetChanged() on your BarChart object after calling this method.
  34. ///
  35. /// - Parameters:
  36. /// - fromX: the starting point on the x-axis where the grouping should begin
  37. /// - groupSpace: The space between groups of bars in values (not pixels) e.g. 0.8f for bar width 1f
  38. /// - barSpace: The space between individual bars in values (not pixels) e.g. 0.1f for bar width 1f
  39. @objc open func groupBars(fromX: Double, groupSpace: Double, barSpace: Double)
  40. {
  41. guard !isEmpty else {
  42. print("BarData needs to hold at least 2 BarDataSets to allow grouping.", terminator: "\n")
  43. return
  44. }
  45. let max = maxEntryCountSet
  46. let maxEntryCount = max?.entryCount ?? 0
  47. let groupSpaceWidthHalf = groupSpace / 2.0
  48. let barSpaceHalf = barSpace / 2.0
  49. let barWidthHalf = self.barWidth / 2.0
  50. var fromX = fromX
  51. let interval = groupWidth(groupSpace: groupSpace, barSpace: barSpace)
  52. for i in 0..<maxEntryCount
  53. {
  54. let start = fromX
  55. fromX += groupSpaceWidthHalf
  56. (_dataSets as! [BarChartDataSetProtocol]).forEach { set in
  57. fromX += barSpaceHalf
  58. fromX += barWidthHalf
  59. if i < set.entryCount
  60. {
  61. if let entry = set.entryForIndex(i)
  62. {
  63. entry.x = fromX
  64. }
  65. }
  66. fromX += barWidthHalf
  67. fromX += barSpaceHalf
  68. }
  69. fromX += groupSpaceWidthHalf
  70. let end = fromX
  71. let innerInterval = end - start
  72. let diff = interval - innerInterval
  73. // correct rounding errors
  74. if diff > 0 || diff < 0
  75. {
  76. fromX += diff
  77. }
  78. }
  79. notifyDataChanged()
  80. }
  81. /// In case of grouped bars, this method returns the space an individual group of bar needs on the x-axis.
  82. ///
  83. /// - Parameters:
  84. /// - groupSpace:
  85. /// - barSpace:
  86. @objc open func groupWidth(groupSpace: Double, barSpace: Double) -> Double
  87. {
  88. return Double(count) * (self.barWidth + barSpace) + groupSpace
  89. }
  90. }