Skip to content

Commit

Permalink
Reassess convenience initializers (#3862)
Browse files Browse the repository at this point in the history
* Reassess convenience initializers

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.

* Modernize BarChartDataEntry internal calculations

* Style updated for PR
  • Loading branch information
jjatie authored and liuxuan30 committed Apr 12, 2019
1 parent b155c2f commit 2896791
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 142 deletions.
91 changes: 35 additions & 56 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 Expand Up @@ -123,30 +122,16 @@ open class BarChartDataEntry: ChartDataEntry

@objc open func calcPosNegSum()
{
guard let _yVals = _yVals else
{
_positiveSum = 0.0
_negativeSum = 0.0
return
}

var sumNeg: Double = 0.0
var sumPos: Double = 0.0

for f in _yVals
{
if f < 0.0
(_negativeSum, _positiveSum) = _yVals?.reduce(into: (0,0)) { (result, y) in
if y < 0
{
sumNeg += -f
result.0 += -y
}
else
{
sumPos += f
result.1 += y
}
}

_negativeSum = sumNeg
_positiveSum = sumPos
} ?? (0,0)
}

/// Splits up the stack-values of the given bar-entry into Range objects.
Expand All @@ -156,38 +141,32 @@ open class BarChartDataEntry: ChartDataEntry
/// - Returns:
@objc open func calcRanges()
{
let values = yValues
if values?.isEmpty != false
{
return
}

guard let values = yValues, !values.isEmpty else { return }

if _ranges == nil
{
_ranges = [Range]()
}
else
{
_ranges?.removeAll()
_ranges!.removeAll()
}

_ranges?.reserveCapacity(values!.count)
_ranges!.reserveCapacity(values.count)

var negRemain = -negativeSum
var posRemain: Double = 0.0

for i in 0 ..< values!.count
for value in values
{
let value = values![i]

if value < 0
{
_ranges?.append(Range(from: negRemain, to: negRemain - value))
_ranges!.append(Range(from: negRemain, to: negRemain - value))
negRemain -= value
}
else
{
_ranges?.append(Range(from: posRemain, to: posRemain + value))
_ranges!.append(Range(from: posRemain, to: posRemain + value))
posRemain += value
}
}
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
Loading

0 comments on commit 2896791

Please sign in to comment.