Created
June 25, 2021 19:33
-
-
Save AppCodeZip/e50ef487393c1353e1f36f0f3e4b8140 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ChartsDemo-OSX | |
// | |
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda | |
// Copyright © 2017 thierry Hentic. | |
// A port of MPAndroidChart for iOS | |
// Licensed under Apache License 2.0 | |
// | |
// https://github.com/danielgindi/ios-charts | |
// https://en.wikipedia.org/wiki/Metric_prefix | |
import Foundation | |
import Charts | |
open class LargeValueFormatter: NSObject, IValueFormatter, IAxisValueFormatter | |
{ | |
fileprivate static let MAX_LENGTH = 5 | |
/// Suffix to be appended after the values. | |
/// | |
/// **default**: suffix: ["", "k", "m", "b", "t"] | |
@objc open var suffix = ["", "k", "L", "Cr", "Ar"] | |
/// An appendix text to be added at the end of the formatted value. | |
@objc open var appendix: String? | |
public override init() | |
{ | |
} | |
@objc public init(appendix: String?) | |
{ | |
self.appendix = appendix | |
} | |
fileprivate func format(value: Double) -> String | |
{ | |
var sig = value | |
// var length = 0 | |
var length = 3 | |
// let maxLength = suffix.count - 1 | |
if (sig >= 10000000) { | |
sig = (sig / 10000000) //Cr | |
length -= 0 | |
} else if (sig >= 100000) { | |
sig = (sig / 100000) // ' Lac'; | |
length -= 1 | |
} | |
else if(sig >= 1000){ | |
sig = (sig/1000) //' K' | |
length -= 2 | |
} | |
else{ | |
length -= 3 // "" | |
} | |
//old condition | |
// while sig >= 1000.0 && length < maxLength | |
// { | |
// sig /= 1000.0 | |
// length += 1 | |
// } | |
var r = String(format: "%2.f", sig ) + suffix[length] | |
if appendix != nil | |
{ | |
r += appendix! | |
} | |
return r | |
} | |
open func stringForValue( | |
_ value: Double, axis: AxisBase?) -> String | |
{ | |
return format(value: value) | |
} | |
open func stringForValue( | |
_ value: Double, | |
entry: ChartDataEntry, | |
dataSetIndex: Int, | |
viewPortHandler: ViewPortHandler?) -> String | |
{ | |
return format(value: value) | |
} | |
} | |
//*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment