Skip to content
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

Minor refactoring of Formatter logic #2998

Merged
merged 4 commits into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions Source/Charts/Formatters/DefaultAxisValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter

@objc open var hasAutoDecimals: Bool = false

fileprivate var _formatter: NumberFormatter?
private var _formatter: NumberFormatter?
@objc open var formatter: NumberFormatter?
{
get { return _formatter }
Expand All @@ -32,8 +32,9 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter
_formatter = newValue
}
}

fileprivate var _decimals: Int?

// TODO: Documentation. Especially the nil case
private var _decimals: Int?
open var decimals: Int?
{
get { return _decimals }
Expand Down Expand Up @@ -90,14 +91,7 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter
open func stringForValue(_ value: Double,
axis: AxisBase?) -> String
{
if block != nil
{
return block!(value, axis)
}
else
{
return formatter?.string(from: NSNumber(floatLiteral: value)) ?? ""
}
return block?(value, axis) ??
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if let would be more readable in this case.

(formatter?.string(from: NSNumber(floatLiteral: value)) ?? "")
}

}
55 changes: 13 additions & 42 deletions Source/Charts/Formatters/DefaultFillFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ open class DefaultFillFormatter: NSObject, IFillFormatter

@objc open var block: Block?

public override init()
{

}
public override init() { }

@objc public init(block: @escaping Block)
{
Expand All @@ -45,47 +42,21 @@ open class DefaultFillFormatter: NSObject, IFillFormatter
dataSet: ILineChartDataSet,
dataProvider: LineChartDataProvider) -> CGFloat
{
if block != nil
guard block == nil else { return block!(dataSet, dataProvider) }
var fillMin = CGFloat(0.0)

if dataSet.yMax > 0.0 && dataSet.yMin < 0.0
{
return block!(dataSet, dataProvider)
fillMin = 0.0
}
else
else if let data = dataProvider.data
{
var fillMin = CGFloat(0.0)

if dataSet.yMax > 0.0 && dataSet.yMin < 0.0
{
fillMin = 0.0
}
else
{
if let data = dataProvider.data
{
var max: Double, min: Double

if data.yMax > 0.0
{
max = 0.0
}
else
{
max = dataProvider.chartYMax
}

if data.yMin < 0.0
{
min = 0.0
}
else
{
min = dataProvider.chartYMin
}

fillMin = CGFloat(dataSet.yMin >= 0.0 ? min : max)
}
}

return fillMin
let max = data.yMax > 0.0 ? 0.0 : dataProvider.chartYMax
let min = data.yMin < 0.0 ? 0.0 : dataProvider.chartYMin

fillMin = CGFloat(dataSet.yMin >= 0.0 ? min : max)
}

return fillMin
}
}
11 changes: 2 additions & 9 deletions Source/Charts/Formatters/DefaultValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ open class DefaultValueFormatter: NSObject, IValueFormatter
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String
{
if block != nil
{
return block!(value, entry, dataSetIndex, viewPortHandler)
}
else
{
return formatter?.string(from: NSNumber(floatLiteral: value)) ?? ""
}
return block?(value, entry, dataSetIndex, viewPortHandler) ??
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if let would be more readable in this case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I flip-flopped on this one a lot. I'll change it now.

(formatter?.string(from: NSNumber(floatLiteral: value)) ?? "")
}

}
7 changes: 1 addition & 6 deletions Source/Charts/Formatters/IndexAxisValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ open class IndexAxisValueFormatter: NSObject, IAxisValueFormatter
axis: AxisBase?) -> String
{
let index = Int(value.rounded())

if index < 0 || index >= _valueCount || index != Int(value)
{
return ""
}

guard values.indices.contains(index), index != Int(value) else { return "" }
return _values[index]
}
}