Skip to content

Commit

Permalink
Reassess convenience initializers
Browse files Browse the repository at this point in the history
The only data required to initialize an entry is an x and y value (or y in the case of Pie and Radar). All other data can easily be updated by initializing and assigning properties on the entry. Therefor, those initializers should be the ones marked as convenience.
Made initializer declarations consistent.
  • Loading branch information
jjatie committed Feb 24, 2019
1 parent cf36285 commit d0ef92f
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 110 deletions.
47 changes: 23 additions & 24 deletions Source/Charts/Data/Implementations/Standard/BarChartDataEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ open class BarChartDataEntry: ChartDataEntry
}

/// Constructor for normal bars (not stacked).
public override init(x: Double, y: Double, data: AnyObject?)
public convenience init(x: Double, y: Double, data: AnyObject?)
{
super.init(x: x, y: y, data: data)
self.init(x: x, y: y)
self.data = data
}

/// Constructor for normal bars (not stacked).
public override init(x: Double, y: Double, icon: NSUIImage?)
public convenience init(x: Double, y: Double, icon: NSUIImage?)
{
super.init(x: x, y: y, icon: icon)
self.init(x: x, y: y)
self.icon = icon
}

/// Constructor for normal bars (not stacked).
public override init(x: Double, y: Double, icon: NSUIImage?, data: AnyObject?)
public convenience init(x: Double, y: Double, icon: NSUIImage?, data: AnyObject?)
{
super.init(x: x, y: y, icon: icon, data: data)
self.init(x: x, y: y)
self.icon = icon
self.data = data
}

/// Constructor for stacked bar entries.
Expand All @@ -62,32 +66,27 @@ open class BarChartDataEntry: ChartDataEntry
calcPosNegSum()
calcRanges()
}

/// Constructor for stacked bar entries. One data object for whole stack
@objc public init(x: Double, yValues: [Double], data: AnyObject?)
@objc public convenience init(x: Double, yValues: [Double], icon: NSUIImage?)
{
super.init(x: x, y: BarChartDataEntry.calcSum(values: yValues), data: data)
self._yVals = yValues
calcPosNegSum()
calcRanges()
self.init(x: x, yValues: yValues)
self.icon = icon
}

/// Constructor for stacked bar entries. One data object for whole stack
@objc public init(x: Double, yValues: [Double], icon: NSUIImage?, data: AnyObject?)
@objc public convenience init(x: Double, yValues: [Double], data: AnyObject?)
{
super.init(x: x, y: BarChartDataEntry.calcSum(values: yValues), icon: icon, data: data)
self._yVals = yValues
calcPosNegSum()
calcRanges()
self.init(x: x, yValues: yValues)
self.data = data
}

/// Constructor for stacked bar entries. One data object for whole stack
@objc public init(x: Double, yValues: [Double], icon: NSUIImage?)
@objc public convenience init(x: Double, yValues: [Double], icon: NSUIImage?, data: AnyObject?)
{
super.init(x: x, y: BarChartDataEntry.calcSum(values: yValues), icon: icon)
self._yVals = yValues
calcPosNegSum()
calcRanges()
self.init(x: x, yValues: yValues)
self.icon = icon
self.data = data
}

@objc open func sumBelow(stackIndex :Int) -> Double
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,21 @@ open class BubbleChartDataEntry: ChartDataEntry
/// - y: The value on the y-axis.
/// - size: The size of the bubble.
/// - data: Spot for additional data this Entry represents.
@objc public init(x: Double, y: Double, size: CGFloat, data: AnyObject?)
@objc public convenience init(x: Double, y: Double, size: CGFloat, data: AnyObject?)
{
super.init(x: x, y: y, data: data)

self.size = size
self.init(x: x, y: y, size: size)
self.data = data
}

/// - Parameters:
/// - x: The index on the x-axis.
/// - y: The value on the y-axis.
/// - size: The size of the bubble.
/// - icon: icon image
@objc public init(x: Double, y: Double, size: CGFloat, icon: NSUIImage?)
@objc public convenience init(x: Double, y: Double, size: CGFloat, icon: NSUIImage?)
{
super.init(x: x, y: y, icon: icon)

self.size = size
self.init(x: x, y: y, size: size)
self.icon = icon
}

/// - Parameters:
Expand All @@ -63,11 +61,11 @@ open class BubbleChartDataEntry: ChartDataEntry
/// - size: The size of the bubble.
/// - icon: icon image
/// - data: Spot for additional data this Entry represents.
@objc public init(x: Double, y: Double, size: CGFloat, icon: NSUIImage?, data: AnyObject?)
@objc public convenience init(x: Double, y: Double, size: CGFloat, icon: NSUIImage?, data: AnyObject?)
{
super.init(x: x, y: y, icon: icon, data: data)

self.size = size
self.init(x: x, y: y, size: size)
self.icon = icon
self.data = data
}

// MARK: NSCopying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,24 @@ open class CandleChartDataEntry: ChartDataEntry
self.open = open
self.close = close
}
@objc public init(x: Double, shadowH: Double, shadowL: Double, open: Double, close: Double, data: AnyObject?)

@objc public convenience init(x: Double, shadowH: Double, shadowL: Double, open: Double, close: Double, icon: NSUIImage?)
{
super.init(x: x, y: (shadowH + shadowL) / 2.0, data: data)

self.high = shadowH
self.low = shadowL
self.open = open
self.close = close
self.init(x: x, shadowH: shadowH, shadowL: shadowL, open: open, close: close)
self.icon = icon
}
@objc public init(x: Double, shadowH: Double, shadowL: Double, open: Double, close: Double, icon: NSUIImage?)

@objc public convenience init(x: Double, shadowH: Double, shadowL: Double, open: Double, close: Double, data: AnyObject?)
{
super.init(x: x, y: (shadowH + shadowL) / 2.0, icon: icon)

self.high = shadowH
self.low = shadowL
self.open = open
self.close = close
self.init(x: x, shadowH: shadowH, shadowL: shadowL, open: open, close: close)
self.data = data
}
@objc public init(x: Double, shadowH: Double, shadowL: Double, open: Double, close: Double, icon: NSUIImage?, data: AnyObject?)

@objc public convenience init(x: Double, shadowH: Double, shadowL: Double, open: Double, close: Double, icon: NSUIImage?, data: AnyObject?)
{
super.init(x: x, y: (shadowH + shadowL) / 2.0, icon: icon, data: data)

self.high = shadowH
self.low = shadowL
self.open = open
self.close = close
self.init(x: x, shadowH: shadowH, shadowL: shadowL, open: open, close: close)
self.icon = icon
self.data = data
}

/// The overall range (difference) between shadow-high and shadow-low.
Expand Down
25 changes: 10 additions & 15 deletions Source/Charts/Data/Implementations/Standard/ChartDataEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
open class ChartDataEntry: ChartDataEntryBase, NSCopying
{
/// the x value
@objc open var x = Double(0.0)
@objc open var x = 0.0

public required init()
{
Expand All @@ -29,7 +29,6 @@ open class ChartDataEntry: ChartDataEntryBase, NSCopying
@objc public init(x: Double, y: Double)
{
super.init(y: y)

self.x = x
}

Expand All @@ -40,12 +39,9 @@ open class ChartDataEntry: ChartDataEntryBase, NSCopying
/// - y: the y value (the actual value of the entry)
/// - data: Space for additional data this Entry represents.

@objc public init(x: Double, y: Double, data: AnyObject?)
@objc public convenience init(x: Double, y: Double, data: AnyObject?)
{
super.init(y: y)

self.x = x

self.init(x: x, y: y)
self.data = data
}

Expand All @@ -56,11 +52,10 @@ open class ChartDataEntry: ChartDataEntryBase, NSCopying
/// - y: the y value (the actual value of the entry)
/// - icon: icon image

@objc public init(x: Double, y: Double, icon: NSUIImage?)
@objc public convenience init(x: Double, y: Double, icon: NSUIImage?)
{
super.init(y: y, icon: icon)

self.x = x
self.init(x: x, y: y)
self.icon = icon
}

/// An Entry represents one single entry in the chart.
Expand All @@ -71,11 +66,11 @@ open class ChartDataEntry: ChartDataEntryBase, NSCopying
/// - icon: icon image
/// - data: Space for additional data this Entry represents.

@objc public init(x: Double, y: Double, icon: NSUIImage?, data: AnyObject?)
@objc public convenience init(x: Double, y: Double, icon: NSUIImage?, data: AnyObject?)
{
super.init(y: y, icon: icon, data: data)

self.x = x
self.init(x: x, y: y)
self.icon = icon
self.data = data
}

// MARK: NSObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
open class ChartDataEntryBase: NSObject
{
/// the y value
@objc open var y = Double(0.0)
@objc open var y = 0.0

/// optional spot for additional data this Entry represents
@objc open var data: AnyObject?
Expand Down Expand Up @@ -42,23 +42,21 @@ open class ChartDataEntryBase: NSObject
/// - y: the y value (the actual value of the entry)
/// - data: Space for additional data this Entry represents.

@objc public init(y: Double, data: AnyObject?)
@objc public convenience init(y: Double, data: AnyObject?)
{
super.init()
self.init(y: y)

self.y = y
self.data = data
}

/// - Parameters:
/// - y: the y value (the actual value of the entry)
/// - icon: icon image

@objc public init(y: Double, icon: NSUIImage?)
@objc public convenience init(y: Double, icon: NSUIImage?)
{
super.init()

self.y = y
self.init(y: y)

self.icon = icon
}

Expand All @@ -67,11 +65,10 @@ open class ChartDataEntryBase: NSObject
/// - icon: icon image
/// - data: Space for additional data this Entry represents.

@objc public init(y: Double, icon: NSUIImage?, data: AnyObject?)
@objc public convenience init(y: Double, icon: NSUIImage?, data: AnyObject?)
{
super.init()

self.y = y
self.init(y: y)

self.icon = icon
self.data = data
}
Expand Down
41 changes: 25 additions & 16 deletions Source/Charts/Data/Implementations/Standard/PieChartDataEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,21 @@ open class PieChartDataEntry: ChartDataEntry
super.init()
}


/// - Parameters:
/// - value: The value on the y-axis
@objc public init(value: Double)
{
super.init(x: .nan, y: value)
}

/// - Parameters:
/// - value: The value on the y-axis
/// - label: The label for the x-axis
@objc public convenience init(value: Double, label: String?)
{
self.init(value: value, label: label, icon: nil, data: nil)
self.init(value: value)
self.label = label
}

/// - Parameters:
Expand All @@ -42,42 +51,40 @@ open class PieChartDataEntry: ChartDataEntry
/// - icon: icon image
@objc public convenience init(value: Double, label: String?, icon: NSUIImage?)
{
self.init(value: value, label: label, icon: icon, data: nil)
self.init(value: value)
self.label = label
self.icon = icon
}

/// - Parameters:
/// - value: The value on the y-axis
/// - label: The label for the x-axis
/// - icon: icon image
/// - data: Spot for additional data this Entry represents
@objc public init(value: Double, label: String?, icon: NSUIImage?, data: AnyObject?)
@objc public convenience init(value: Double, label: String?, icon: NSUIImage?, data: AnyObject?)
{
super.init(x: 0.0, y: value, icon: icon, data: data)

self.init(value: value)
self.label = label
self.icon = icon
self.data = data
}

/// - Parameters:
/// - value: The value on the y-axis
@objc public convenience init(value: Double)
{
self.init(value: value, label: nil, icon: nil, data: nil)
}


/// - Parameters:
/// - value: The value on the y-axis
/// - data: Spot for additional data this Entry represents
@objc public convenience init(value: Double, data: AnyObject?)
{
self.init(value: value, label: nil, icon: nil, data: data)
self.init(value: value)
self.data = data
}

/// - Parameters:
/// - value: The value on the y-axis
/// - icon: icon image
@objc public convenience init(value: Double, icon: NSUIImage?)
{
self.init(value: value, label: nil, icon: icon, data: nil)
self.init(value: value)
self.icon = icon
}

/// - Parameters:
Expand All @@ -86,7 +93,9 @@ open class PieChartDataEntry: ChartDataEntry
/// - data: Spot for additional data this Entry represents
@objc public convenience init(value: Double, icon: NSUIImage?, data: AnyObject?)
{
self.init(value: value, label: nil, icon: icon, data: data)
self.init(value: value)
self.icon = icon
self.data = data
}

// MARK: Data property accessors
Expand Down
Loading

0 comments on commit d0ef92f

Please sign in to comment.