IndexAxisValueFormatter.swift 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // IndexAxisValueFormatter.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. /// This formatter is used for passing an array of x-axis labels, on whole x steps.
  13. @objc(ChartIndexAxisValueFormatter)
  14. open class IndexAxisValueFormatter: NSObject, AxisValueFormatter
  15. {
  16. @objc public var values: [String] = [String]()
  17. public override init()
  18. {
  19. super.init()
  20. }
  21. @objc public init(values: [String])
  22. {
  23. super.init()
  24. self.values = values
  25. }
  26. @objc public static func with(values: [String]) -> IndexAxisValueFormatter?
  27. {
  28. return IndexAxisValueFormatter(values: values)
  29. }
  30. open func stringForValue(_ value: Double,
  31. axis: AxisBase?) -> String
  32. {
  33. let index = Int(value.rounded())
  34. guard values.indices.contains(index), index == Int(value) else { return "" }
  35. return values[index]
  36. }
  37. }