-
-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Moved the default value formatter #3088
Changes from all commits
0b64eda
9cf48c1
45207ae
60b5b60
e1628de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
import Foundation | ||
|
||
/// The default value formatter used for all chart components that needs a default | ||
@objc(ChartDefaultValueFormatter) | ||
open class DefaultValueFormatter: NSObject, ValueFormatter | ||
{ | ||
|
@@ -22,69 +23,65 @@ open class DefaultValueFormatter: NSObject, ValueFormatter | |
|
||
@objc open var block: Block? | ||
|
||
@objc open var hasAutoDecimals: Bool = false | ||
@objc open var hasAutoDecimals: Bool | ||
|
||
private var _formatter: NumberFormatter? | ||
@objc open var formatter: NumberFormatter? | ||
{ | ||
get { return _formatter } | ||
set | ||
{ | ||
@objc open var formatter: NumberFormatter? { | ||
willSet { | ||
hasAutoDecimals = false | ||
_formatter = newValue | ||
} | ||
} | ||
|
||
private var _decimals: Int? | ||
open var decimals: Int? | ||
{ | ||
get { return _decimals } | ||
set | ||
{ | ||
_decimals = newValue | ||
|
||
if let digits = newValue | ||
open var decimals: Int? { | ||
didSet { | ||
if let digits = decimals | ||
{ | ||
self.formatter?.minimumFractionDigits = digits | ||
self.formatter?.maximumFractionDigits = digits | ||
self.formatter?.usesGroupingSeparator = true | ||
formatter?.minimumFractionDigits = digits | ||
formatter?.maximumFractionDigits = digits | ||
formatter?.usesGroupingSeparator = true | ||
} | ||
} | ||
} | ||
|
||
public override init() | ||
{ | ||
super.init() | ||
|
||
self.formatter = NumberFormatter() | ||
formatter = NumberFormatter() | ||
formatter?.usesGroupingSeparator = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why set usesGroupingSeparator and decimals here while the old code does not? did I miss anything There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the empty initializer provide the same formatter that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry. I missed the removal |
||
decimals = 1 | ||
hasAutoDecimals = true | ||
|
||
super.init() | ||
} | ||
|
||
@objc public init(formatter: NumberFormatter) | ||
{ | ||
super.init() | ||
|
||
self.formatter = formatter | ||
hasAutoDecimals = false | ||
|
||
super.init() | ||
} | ||
|
||
@objc public init(decimals: Int) | ||
{ | ||
super.init() | ||
|
||
self.formatter = NumberFormatter() | ||
self.formatter?.usesGroupingSeparator = true | ||
formatter = NumberFormatter() | ||
formatter?.usesGroupingSeparator = true | ||
self.decimals = decimals | ||
hasAutoDecimals = true | ||
|
||
super.init() | ||
} | ||
|
||
@objc public init(block: @escaping Block) | ||
{ | ||
super.init() | ||
|
||
self.block = block | ||
hasAutoDecimals = false | ||
|
||
super.init() | ||
} | ||
|
||
@objc public static func with(block: @escaping Block) -> DefaultValueFormatter? | ||
|
||
/// This function is deprecated - Use `init(block:)` instead. | ||
// DEC 11, 2017 | ||
@available(*, deprecated: 3.0, message: "Use `init(block:)` instead.") | ||
@objc public static func with(block: @escaping Block) -> DefaultValueFormatter | ||
{ | ||
return DefaultValueFormatter(block: block) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,7 +76,7 @@ extension Double | |
self != 0.0 | ||
else { return 0 } | ||
|
||
let i = self.roundedToNextSignificant() | ||
let i = roundedToNextSignificant() | ||
|
||
guard | ||
!i.isInfinite, | ||
|
@@ -97,23 +97,6 @@ extension CGPoint | |
} | ||
} | ||
|
||
open class ChartUtils | ||
{ | ||
private static var _defaultValueFormatter: ValueFormatter = generateDefaultValueFormatter() | ||
|
||
private class func generateDefaultValueFormatter() -> ValueFormatter | ||
{ | ||
let formatter = DefaultValueFormatter(decimals: 1) | ||
return formatter | ||
} | ||
|
||
/// - returns: The default value formatter used for all chart components that needs a default | ||
open class func defaultValueFormatter() -> ValueFormatter | ||
{ | ||
return _defaultValueFormatter | ||
} | ||
} | ||
|
||
extension CGContext { | ||
|
||
open func drawImage(_ image: NSUIImage, atCenter center: CGPoint, size: CGSize) | ||
|
@@ -226,7 +209,7 @@ extension CGContext { | |
NSUIGraphicsPopContext() | ||
} | ||
|
||
internal func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, knownTextSize: CGSize, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, knownTextSize: CGSize, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about we make these two There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First, it's always been internal. Second, we expose a way to extend the draw methods by wrapping them in another open method. I think it's safe to keep it internal. If we get requests to expose it, maybe we can change our mind then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, I don't think it makes sense to have any of these be open, because they would have to subclass |
||
{ | ||
var rect = CGRect(origin: .zero, size: knownTextSize) | ||
|
||
|
@@ -274,7 +257,7 @@ extension CGContext { | |
NSUIGraphicsPopContext() | ||
} | ||
|
||
internal func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, angleRadians: CGFloat, attributes: [NSAttributedStringKey : Any]?) | ||
{ | ||
let rect = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil) | ||
drawMultilineText(text, at: point, constrainedTo: size, anchor: anchor, knownTextSize: rect.size, angleRadians: angleRadians, attributes: attributes) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove default value here but initialize later in init() many times?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it has different values based on which initializer. This way it's not assigned twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright, makes sense then.