Skip to content

Commit

Permalink
Dataset logic cleanup (#3001)
Browse files Browse the repository at this point in the history
* Cleaned up `ChartDataSet` logic

Added TODOs for areas where simple changes can help improve Swift consistency.

* Tidied up logic for `ChartDataSet` subclasses

Minor changes to take advantage of Swift features and help improve readability.

* Added Collection conformances

MutableCollection
RandomAccessCollection
RangeReplaceableCollection

* [#3018]

Refactored use of `ChartData` to use new `Collection` conformances

* Fixed required initializers

* ChartData adopts ExressibleByArrayLiteral

* Modified demos

to take advantage of collection conformance.

* Pulled latest master

* Pulled latest master

* Updates for PR

Also added remove subrange.

* Refactored ChartData

Removed redundancy from min/max logic.
Lots of naming changes.
Cleaner implementations.

* PR review fixes

* Removed unnecessary `get` from subscripts.

* Disabled `remove(at:)` for CombinedChartView

* Removed redundant methods

* Relocated `appendEntry(_:todataSet:)`

* pulled latest 4.0.0

* Disabled Collection support for CombinedChartData

* Removed methods from CombinedChartData

* Pulled latest 4.0

* Fixes after merge

* Removed used of dataSet(forIndex:)

* Fixed merge conflicts

* Fixed merge conflicts

* updated demos

* Pulled latest 4.0.0

* Fixed demos

* Fixed objective c demos

* Moved travis to Xcode 9.3 beta temporarily

* Fixed macOS demo info.plist and tv demo device name

* PR Fixes

* Fixed objective-c naming

* PR Fixes

* PR Fixes
  • Loading branch information
jjatie authored and liuxuan30 committed May 2, 2018
1 parent 7141265 commit 08bd20c
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 209 deletions.
2 changes: 1 addition & 1 deletion Source/Charts/Data/Implementations/ChartBaseDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class ChartBaseDataSet: NSObject, ChartDataSetProtocol
valueColors.append(NSUIColor.black)
}

@objc public init(label: String?)
@objc public init(label: String)
{
super.init()

Expand Down
64 changes: 16 additions & 48 deletions Source/Charts/Data/Implementations/Standard/BarChartDataSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
initialize()
}

public override init(values: [ChartDataEntry]?, label: String?)
public override init(values: [ChartDataEntry], label: String)
{
super.init(values: values, label: label)
initialize()
Expand All @@ -50,68 +50,36 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
{
_entryCountStacks = 0

for i in 0 ..< entries.count
{
if let vals = entries[i].yValues
{
_entryCountStacks += vals.count
}
else
{
_entryCountStacks += 1
}
}
entries.forEach { _entryCountStacks += $0.yValues?.count ?? 1 }
}

/// calculates the maximum stacksize that occurs in the Entries array of this DataSet
private func calcStackSize(entries: [BarChartDataEntry])
{
for i in 0 ..< entries.count
for e in entries where (e.yValues?.count ?? 0) > _stackSize
{
if let vals = entries[i].yValues
{
if vals.count > _stackSize
{
_stackSize = vals.count
}
}
_stackSize = e.yValues!.count
}
}

open override func calcMinMax(entry e: ChartDataEntry)
{
guard let e = e as? BarChartDataEntry
guard let e = e as? BarChartDataEntry,
!e.y.isNaN
else { return }

if !e.y.isNaN
if e.yValues == nil
{
if e.yValues == nil
{
if e.y < _yMin
{
_yMin = e.y
}

if e.y > _yMax
{
_yMax = e.y
}
}
else
{
if -e.negativeSum < _yMin
{
_yMin = -e.negativeSum
}

if e.positiveSum > _yMax
{
_yMax = e.positiveSum
}
}

calcMinMaxX(entry: e)
_yMin = min(e.y, _yMin)
_yMax = max(e.y, _yMax)
}
else
{
_yMin = min(-e.negativeSum, _yMin)
_yMax = max(e.positiveSum, _yMax)
}

calcMinMaxX(entry: e)
}

/// - returns: The maximum number of bars that can be stacked upon another in this DataSet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ open class BubbleChartDataSet: BarLineScatterCandleBubbleChartDataSet, BubbleCha

super.calcMinMax(entry: e)

let size = e.size

if size > _maxSize
{
_maxSize = size
}
_maxSize = max(e.size, maxSize)
}

// MARK: - Styling functions and accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa
super.init()
}

public override init(values: [ChartDataEntry]?, label: String?)
public override init(values: [ChartDataEntry], label: String)
{
super.init(values: values, label: label)
}
Expand All @@ -32,68 +32,42 @@ open class CandleChartDataSet: LineScatterCandleRadarChartDataSet, CandleChartDa
{
guard let e = e as? CandleChartDataEntry
else { return }

if e.low < _yMin
{
_yMin = e.low
}

if e.high > _yMax
{
_yMax = e.high
}


_yMin = min(e.low, _yMin)
_yMax = max(e.high, _yMax)

calcMinMaxX(entry: e)
}

open override func calcMinMaxY(entry e: ChartDataEntry)
{
guard let e = e as? CandleChartDataEntry
else { return }

if e.high < _yMin
{
_yMin = e.high
}
if e.high > _yMax
{
_yMax = e.high
}

if e.low < _yMin
{
_yMin = e.low
}
if e.low > _yMax
{
_yMax = e.low
}

_yMin = min(e.low, _yMin)
_yMax = max(e.high, _yMin)

_yMin = min(e.low, _yMax)
_yMax = max(e.high, _yMax)
}

// MARK: - Styling functions and accessors

/// the space between the candle entries
///
/// **default**: 0.1 (10%)
private var _barSpace = CGFloat(0.1)
private var _barSpace: CGFloat = 0.1

/// the space that is left out on the left and right side of each candle,
/// **default**: 0.1 (10%), max 0.45, min 0.0
open var barSpace: CGFloat
{
set
{
if newValue < 0.0
{
_barSpace = 0.0
}
else if newValue > 0.45
{
_barSpace = 0.45
}
else
{
_barSpace = newValue
switch newValue {
case ..<0: _barSpace = 0.0
case 0.45...: _barSpace = 0.45
default: _barSpace = newValue
}
}
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ open class ChartData: NSObject, ExpressibleByArrayLiteral
}

/// - returns: All colors used across all DataSet objects this object represents.
@objc open func getColors() -> [NSUIColor]
@objc open var colors: [NSUIColor]
{
return reduce(into: []) { $0 += $1.colors }
}
Expand Down
Loading

0 comments on commit 08bd20c

Please sign in to comment.