-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathBarEntry.kt
More file actions
231 lines (195 loc) · 5.9 KB
/
BarEntry.kt
File metadata and controls
231 lines (195 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.github.mikephil.charting.data
import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import com.github.mikephil.charting.highlight.Range
import kotlin.math.abs
/**
* Entry class for the BarChart. (especially stacked bars)
*/
@SuppressLint("ParcelCreator")
open class BarEntry : Entry {
/**
* Returns the stacked values this BarEntry represents, or null, if only a single value is represented (then, use
* getY()).
*/
var yVals: FloatArray? = null
private set
/**
* Returns the ranges of the individual stack-entries. Will return null if this entry is not stacked.
*/
var ranges: Array<Range> = arrayOf()
private set
/**
* Returns the sum of all negative values this entry (if stacked) contains. (this is a positive number)
*/
var negativeSum: Float = 0f
private set
/**
* Returns the sum of all positive values this entry (if stacked) contains.
*/
var positiveSum: Float = 0f
private set
/**
* Constructor for normal bars (not stacked).
*/
constructor(x: Float, y: Float) : super(x, y)
/**
* Constructor for normal bars (not stacked).
*
* @param x
* @param y
* @param data - Spot for additional data this Entry represents.
*/
constructor(x: Float, y: Float, data: Any?) : super(x, y, data)
/**
* Constructor for normal bars (not stacked).
*
* @param x
* @param y
* @param icon - icon image
*/
constructor(x: Float, y: Float, icon: Drawable?) : super(x, y, icon)
/**
* Constructor for normal bars (not stacked).
*
* @param x
* @param y
* @param icon - icon image
* @param data - Spot for additional data this Entry represents.
*/
constructor(x: Float, y: Float, icon: Drawable?, data: Any?) : super(x, y, icon, data)
/**
* Constructor for stacked bar entries. One data object for whole stack
*
* @param x
* @param vals - the stack values, use at least 2
*/
constructor(x: Float, vals: FloatArray?) : super(x, calcSum(vals)) {
this.yVals = vals
calcPosNegSum()
calcRanges()
}
/**
* Constructor for stacked bar entries. One data object for whole stack
*
* @param x
* @param vals - the stack values, use at least 2
* @param data - Spot for additional data this Entry represents.
*/
constructor(x: Float, vals: FloatArray?, data: Any?) : super(x, calcSum(vals), data) {
this.yVals = vals
calcPosNegSum()
calcRanges()
}
/**
* Constructor for stacked bar entries. One data object for whole stack
*
* @param x
* @param vals - the stack values, use at least 2
* @param icon - icon image
*/
constructor(x: Float, vals: FloatArray?, icon: Drawable?) : super(x, calcSum(vals), icon) {
this.yVals = vals
calcPosNegSum()
calcRanges()
}
/**
* Constructor for stacked bar entries. One data object for whole stack
*
* @param x
* @param vals - the stack values, use at least 2
* @param icon - icon image
* @param data - Spot for additional data this Entry represents.
*/
constructor(x: Float, vals: FloatArray?, icon: Drawable?, data: Any?) : super(x, calcSum(vals), icon, data) {
this.yVals = vals
calcPosNegSum()
calcRanges()
}
/**
* Returns an exact copy of the BarEntry.
*/
override fun copy(): BarEntry {
val copied = BarEntry(x, y, data)
copied.setVals(this.yVals)
return copied
}
/**
* Set the array of values this BarEntry should represent.
*
* @param vals
*/
fun setVals(vals: FloatArray?) {
y = calcSum(vals)
this.yVals = vals
calcPosNegSum()
calcRanges()
}
/**
* Returns the value of this BarEntry. If the entry is stacked, it returns the positive sum of all values.
*/
override var y: Float = 0.0f
get() = super.y
/**
* Returns true if this BarEntry is stacked (has a values array), false if not.
*/
val isStacked: Boolean
get() = this.yVals != null
@Deprecated("getSumBelow(stackIndex)` instead.")
fun getBelowSum(stackIndex: Int): Float {
return getSumBelow(stackIndex)
}
fun getSumBelow(stackIndex: Int): Float {
if (this.yVals == null) return 0f
var remainder = 0f
var index = yVals!!.size - 1
while (index > stackIndex && index >= 0) {
remainder += this.yVals!![index]
index--
}
return remainder
}
private fun calcPosNegSum() {
if (this.yVals == null) {
this.negativeSum = 0f
this.positiveSum = 0f
return
}
var sumNeg = 0f
var sumPos = 0f
for (f in this.yVals) {
if (f <= 0f) sumNeg += abs(f)
else sumPos += f
}
this.negativeSum = sumNeg
this.positiveSum = sumPos
}
protected fun calcRanges() {
val values = this.yVals
if (values == null || values.isEmpty()) return
this.ranges = arrayOf()
var negRemain = -this.negativeSum
var posRemain = 0f
for (i in ranges.indices) {
val value = values[i]
if (value < 0) {
this.ranges[i] = Range(negRemain, negRemain - value)
negRemain -= value
} else {
this.ranges[i] = Range(posRemain, posRemain + value)
posRemain += value
}
}
}
companion object {
/**
* Calculates the sum across all values of the given stack.
*/
private fun calcSum(vals: FloatArray?): Float {
if (vals == null) return 0f
var sum = 0f
for (f in vals) sum += f
return sum
}
}
}