ChartAxisValueDoubleLog.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // ChartAxisValueDoubleLog.swift
  3. // Naterade
  4. //
  5. // Created by Nathan Racklyeft on 2/29/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import UIKit
  9. import SwiftCharts
  10. public final class ChartAxisValueDoubleLog: ChartAxisValueDoubleScreenLoc {
  11. let unitString: String?
  12. public init(actualDouble: Double, unitString: String? = nil, formatter: NumberFormatter, labelSettings: ChartLabelSettings = ChartLabelSettings()) {
  13. let screenLocDouble: Double
  14. switch actualDouble {
  15. case let x where x < 0:
  16. screenLocDouble = -log(-x + 1)
  17. case let x where x > 0:
  18. screenLocDouble = log(x + 1)
  19. default: // 0
  20. screenLocDouble = 0
  21. }
  22. self.unitString = unitString
  23. super.init(screenLocDouble: screenLocDouble, actualDouble: actualDouble, formatter: formatter, labelSettings: labelSettings)
  24. }
  25. public init(screenLocDouble: Double, formatter: NumberFormatter, labelSettings: ChartLabelSettings = ChartLabelSettings()) {
  26. let actualDouble: Double
  27. switch screenLocDouble {
  28. case let x where x < 0:
  29. actualDouble = -pow(M_E, -x) + 1
  30. case let x where x > 0:
  31. actualDouble = pow(M_E, x) - 1
  32. default: // 0
  33. actualDouble = 0
  34. }
  35. self.unitString = nil
  36. super.init(screenLocDouble: screenLocDouble, actualDouble: actualDouble, formatter: formatter, labelSettings: labelSettings)
  37. }
  38. override public var description: String {
  39. let suffix = unitString != nil ? " \(unitString!)" : ""
  40. return super.description + suffix
  41. }
  42. }