Skip to content

Commit

Permalink
Merge pull request #2998 from jjatie/minor-formatter-logic
Browse files Browse the repository at this point in the history
Minor changes to Formatter logic
  • Loading branch information
jjatie committed Dec 24, 2017
2 parents 5639818 + 3ab6706 commit 8b18e76
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 63 deletions.
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]
}
}

0 comments on commit 8b18e76

Please sign in to comment.