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

Removed optionality from valueFormatter where appropriate #3106

Merged
merged 7 commits into from
Jan 23, 2018
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
12 changes: 6 additions & 6 deletions Source/Charts/Charts/ChartViewBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
// MARK: - Properties

/// The default IValueFormatter that has been determined by the chart considering the provided minimum and maximum values.
internal var _defaultValueFormatter: ValueFormatter? = DefaultValueFormatter(decimals: 0)
internal lazy var defaultValueFormatter: ValueFormatter = DefaultValueFormatter(decimals: 0)

/// The data for the chart
@objc open var data: ChartData?
{
Expand All @@ -56,9 +56,9 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate

for set in data.dataSets
{
if set.needsFormatter || set.valueFormatter === _defaultValueFormatter
if set.valueFormatter === defaultValueFormatter
{
set.valueFormatter = _defaultValueFormatter
set.valueFormatter = defaultValueFormatter
}
}

Expand Down Expand Up @@ -251,12 +251,12 @@ open class ChartViewBase: NSUIView, ChartDataProvider, AnimatorDelegate
}


if _defaultValueFormatter is DefaultValueFormatter
if defaultValueFormatter is DefaultValueFormatter
{
// setup the formatter with a new number of digits
let digits = reference.decimalPlaces

(_defaultValueFormatter as? DefaultValueFormatter)?.decimals
(defaultValueFormatter as? DefaultValueFormatter)?.decimals
= digits
}
}
Expand Down
9 changes: 4 additions & 5 deletions Source/Charts/Components/AxisBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class AxisBase: ComponentBase
}

/// Custom formatter that is used instead of the auto-formatter if set
private var _axisValueFormatter: AxisValueFormatter?
private lazy var _axisValueFormatter: AxisValueFormatter = DefaultAxisValueFormatter(decimals: decimals)

@objc open var labelFont = NSUIFont.systemFont(ofSize: 10.0)
@objc open var labelTextColor = NSUIColor.black
Expand Down Expand Up @@ -161,10 +161,9 @@ open class AxisBase: ComponentBase
{
get
{
if _axisValueFormatter == nil ||
(_axisValueFormatter is DefaultAxisValueFormatter &&
(_axisValueFormatter as! DefaultAxisValueFormatter).hasAutoDecimals &&
(_axisValueFormatter as! DefaultAxisValueFormatter).decimals != decimals)
if _axisValueFormatter is DefaultAxisValueFormatter,
(_axisValueFormatter as! DefaultAxisValueFormatter).hasAutoDecimals,
(_axisValueFormatter as! DefaultAxisValueFormatter).decimals != decimals
{
_axisValueFormatter = DefaultAxisValueFormatter(decimals: decimals)
}
Expand Down
31 changes: 3 additions & 28 deletions Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,35 +268,10 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol

/// - returns: `true` if value highlighting is enabled for this dataset
open var isHighlightEnabled: Bool { return highlightEnabled }

/// Custom formatter that is used instead of the auto-formatter if set
internal var _valueFormatter: ValueFormatter?


/// Custom formatter that is used instead of the auto-formatter if set
open var valueFormatter: ValueFormatter?
{
get
{
if needsFormatter
{
return ChartUtils.defaultValueFormatter()
}

return _valueFormatter
}
set
{
if newValue == nil { return }

_valueFormatter = newValue
}
}

open var needsFormatter: Bool
{
return _valueFormatter == nil
}

open lazy var valueFormatter: ValueFormatter = ChartUtils.defaultValueFormatter()

/// Sets/get a single color for value text.
/// Setting the color clears the colors array and adds a single color.
/// Getting will return the first color in the array.
Expand Down
5 changes: 1 addition & 4 deletions Source/Charts/Data/Interfaces/ChartDataSetProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,7 @@ public protocol ChartDataSetProtocol
var isHighlightEnabled: Bool { get }

/// Custom formatter that is used instead of the auto-formatter if set
var valueFormatter: ValueFormatter? { get set }

/// - returns: `true` if the valueFormatter object of this DataSet is null.
var needsFormatter: Bool { get }
var valueFormatter: ValueFormatter { get set }

/// Sets/get a single color for value text.
/// Setting the color clears the colors array and adds a single color.
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer

let buffer = _buffers[dataSetIndex]

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

Expand Down
6 changes: 2 additions & 4 deletions Source/Charts/Renderers/BubbleChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,9 @@ open class BubbleChartRenderer: BarLineScatterCandleBubbleRenderer
{
let dataSet = dataSets[i]

guard
shouldDrawValues(forDataSet: dataSet),
let formatter = dataSet.valueFormatter
else { continue }
guard shouldDrawValues(forDataSet: dataSet) else { continue }

let formatter = dataSet.valueFormatter
let alpha = phaseX == 1 ? phaseY : phaseX

_xBounds.set(chart: dataProvider, dataSet: dataSet, animator: animator)
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer

let valueFont = dataSet.valueFont

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
let valueToPixelMatrix = trans.valueToPixelMatrix
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ open class HorizontalBarChartRenderer: BarChartRenderer
let valueFont = dataSet.valueFont
let yOffset = -valueFont.lineHeight / 2.0

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)

Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ open class LineChartRenderer: LineRadarRenderer

let valueFont = dataSet.valueFont

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
let valueToPixelMatrix = trans.valueToPixelMatrix
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/PieChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ open class PieChartRenderer: NSObject, DataRenderer
let entryLabelFont = dataSet.entryLabelFont
let lineHeight = valueFont.lineHeight

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

for j in 0 ..< dataSet.entryCount
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ open class RadarChartRenderer: LineRadarRenderer

let valueFont = dataSet.valueFont

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

if dataSet.isDrawValuesEnabled
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Charts/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer

let valueFont = dataSet.valueFont

guard let formatter = dataSet.valueFormatter else { continue }
let formatter = dataSet.valueFormatter

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
let valueToPixelMatrix = trans.valueToPixelMatrix
Expand Down