Skip to content

Commit b305bac

Browse files
committed
Modernize BarChartDataEntry internal calculations
1 parent d0ef92f commit b305bac

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

Source/Charts/Data/Implementations/Standard/BarChartDataEntry.swift

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,13 @@ open class BarChartDataEntry: ChartDataEntry
122122

123123
@objc open func calcPosNegSum()
124124
{
125-
guard let _yVals = _yVals else
126-
{
127-
_positiveSum = 0.0
128-
_negativeSum = 0.0
129-
return
130-
}
131-
132-
var sumNeg: Double = 0.0
133-
var sumPos: Double = 0.0
134-
135-
for f in _yVals
136-
{
137-
if f < 0.0
138-
{
139-
sumNeg += -f
125+
(_negativeSum, _positiveSum) = _yVals?.reduce(into: (0,0)) { (result, y) in
126+
if y < 0 {
127+
result.0 += -y
128+
} else {
129+
result.1 += y
140130
}
141-
else
142-
{
143-
sumPos += f
144-
}
145-
}
146-
147-
_negativeSum = sumNeg
148-
_positiveSum = sumPos
131+
} ?? (0,0)
149132
}
150133

151134
/// Splits up the stack-values of the given bar-entry into Range objects.
@@ -155,38 +138,34 @@ open class BarChartDataEntry: ChartDataEntry
155138
/// - Returns:
156139
@objc open func calcRanges()
157140
{
158-
let values = yValues
159-
if values?.isEmpty != false
160-
{
141+
guard let values = yValues, !values.isEmpty else {
161142
return
162143
}
163-
144+
164145
if _ranges == nil
165146
{
166147
_ranges = [Range]()
167148
}
168149
else
169150
{
170-
_ranges?.removeAll()
151+
_ranges!.removeAll()
171152
}
172153

173-
_ranges?.reserveCapacity(values!.count)
154+
_ranges!.reserveCapacity(values.count)
174155

175156
var negRemain = -negativeSum
176157
var posRemain: Double = 0.0
177158

178-
for i in 0 ..< values!.count
159+
for value in values
179160
{
180-
let value = values![i]
181-
182161
if value < 0
183162
{
184-
_ranges?.append(Range(from: negRemain, to: negRemain - value))
163+
_ranges!.append(Range(from: negRemain, to: negRemain - value))
185164
negRemain -= value
186165
}
187166
else
188167
{
189-
_ranges?.append(Range(from: posRemain, to: posRemain + value))
168+
_ranges!.append(Range(from: posRemain, to: posRemain + value))
190169
posRemain += value
191170
}
192171
}

0 commit comments

Comments
 (0)