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 all commits
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
13 changes: 5 additions & 8 deletions Source/Charts/Formatters/DefaultAxisValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter
_formatter = newValue
}
}


// TODO: Documentation. Especially the nil case
private var _decimals: Int?
open var decimals: Int?
{
Expand Down Expand Up @@ -90,14 +91,10 @@ open class DefaultAxisValueFormatter: NSObject, IAxisValueFormatter
open func stringForValue(_ value: Double,
axis: AxisBase?) -> String
{
if block != nil
{
return block!(value, axis)
}
else
{
if let block = block {
return block(value, axis)
} else {
return 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
}
}
10 changes: 3 additions & 7 deletions Source/Charts/Formatters/DefaultValueFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,10 @@ open class DefaultValueFormatter: NSObject, IValueFormatter
dataSetIndex: Int,
viewPortHandler: ViewPortHandler?) -> String
{
if block != nil
{
return block!(value, entry, dataSetIndex, viewPortHandler)
}
else
{
if let block = block {
return block(value, entry, dataSetIndex, viewPortHandler)
} else {
return 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]
}
}