forked from PhilJay/MPAndroidChart
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathBarDataSet.kt
More file actions
195 lines (161 loc) · 5.57 KB
/
BarDataSet.kt
File metadata and controls
195 lines (161 loc) · 5.57 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
package info.appdev.charting.data
import android.graphics.Color
import androidx.annotation.ColorInt
import info.appdev.charting.interfaces.datasets.IBarDataSet
import info.appdev.charting.utils.Fill
open class BarDataSet(yVals: MutableList<BarEntry>, label: String) : BarLineScatterCandleBubbleDataSet<BarEntry>(yVals, label), IBarDataSet {
/**
* the maximum number of bars that are stacked upon each other, this value
* is calculated from the Entries that are added to the DataSet
*/
private var mStackSize = 1
/**
* the color used for drawing the bar shadows
*/
@ColorInt
private var mBarShadowColor = Color.rgb(215, 215, 215)
private var mBarBorderWidth = 0.0f
@ColorInt
private var mBarBorderColor = Color.BLACK
/**
* the alpha value used to draw the highlight indicator bar
*/
private var mHighLightAlpha = 120
/**
* the overall entry count, including counting each stack-value individually
*/
var entryCountStacks: Int = 0
private set
/**
* array of labels used to describe the different values of the stacked bars
*/
private var mStackLabels: MutableList<String> = mutableListOf()
@get:Deprecated("Use getFills() instead")
var gradients: MutableList<Fill> = mutableListOf()
protected set
init {
highLightColor = Color.rgb(0, 0, 0)
calcStackSize(yVals)
calcEntryCountIncludingStacks(yVals)
}
override fun copy(): DataSet<BarEntry>? {
val entries: MutableList<BarEntry> = mutableListOf()
for (i in mEntries.indices) {
entries.add(mEntries[i].copy())
}
val copied = BarDataSet(entries, label)
copy(copied)
return copied
}
protected fun copy(barDataSet: BarDataSet) {
super.copy((barDataSet as BaseDataSet<*>?)!!)
barDataSet.mStackSize = mStackSize
barDataSet.mBarShadowColor = mBarShadowColor
barDataSet.mBarBorderWidth = mBarBorderWidth
barDataSet.mStackLabels = mStackLabels
barDataSet.mHighLightAlpha = mHighLightAlpha
}
override var fills: MutableList<Fill> = mutableListOf()
set(value) {
this.gradients = value
}
override fun getFill(index: Int): Fill? {
return fills[index % fills.size]
}
@Deprecated("Use getFill(...) instead")
fun getGradient(index: Int): Fill? {
return getFill(index)
}
/**
* Sets the start and end color for gradient color, ONLY color that should be used for this DataSet.
*/
fun setGradientColor(@ColorInt startColor: Int, @ColorInt endColor: Int) {
fills.clear()
fills.add(Fill(startColor, endColor))
}
@Deprecated("Use setFills(...) instead")
fun setGradientColors(gradientColors: MutableList<Fill>) {
this.gradients = gradientColors
}
/**
* Calculates the total number of entries this DataSet represents, including
* stacks. All values belonging to a stack are calculated separately.
*/
private fun calcEntryCountIncludingStacks(yVals: MutableList<BarEntry>) {
this.entryCountStacks = 0
for (i in yVals.indices) {
val vals = yVals.get(i).yVals
if (vals == null) this.entryCountStacks++
else this.entryCountStacks += vals.size
}
}
/**
* calculates the maximum stackSize that occurs in the Entries array of this DataSet
*/
private fun calcStackSize(yVals: MutableList<BarEntry>) {
for (i in yVals.indices) {
val vals = yVals[i].yVals
if (vals != null && vals.size > mStackSize) mStackSize = vals.size
}
}
override fun calcMinMax(entry: BarEntry) {
if (!entry.y.isNaN()) {
if (entry.yVals == null) {
if (entry.y < yMin) yMin = entry.y
if (entry.y > yMax) yMax = entry.y
} else {
if (-entry.negativeSum < yMin) yMin = -entry.negativeSum
if (entry.positiveSum > yMax) yMax = entry.positiveSum
}
calcMinMaxX(entry)
}
}
override var stackSize: Int
get() = mStackSize
set(value) {
mStackSize = value
}
override val isStacked: Boolean
get() = mStackSize > 1
/**
* Sets the color used for drawing the bar-shadows. The bar shadows is a
* surface behind the bar that indicates the maximum value. Don't for get to
* use getResources().getColor(...) to set this. Or Color.rgb(...).
*/
override var barShadowColor: Int
get() = mBarShadowColor
set(value) {
mBarShadowColor = value
}
override var barBorderColor: Int
get() = mBarBorderColor
set(value) {
mBarBorderColor = value
}
/**
* The width used for drawing borders around the bars.
* If borderWidth == 0, no border will be drawn.
*/
override var barBorderWidth: Float
get() = mBarBorderWidth
set(value) {
mBarBorderWidth = value
}
/**
* Set the alpha value (transparency) that is used for drawing the highlight
* indicator bar. min = 0 (fully transparent), max = 255 (fully opaque)
*/
override var highLightAlpha: Int
get() = mHighLightAlpha
set(value) {
mHighLightAlpha = value
}
/**
* Sets labels for different values of bar-stacks, in case there are one.
*/
override var stackLabels: MutableList<String>
get() = mStackLabels
set(value) {
mStackLabels = value
}
}