@@ -42,7 +42,7 @@ class ClickablePieChart @JvmOverloads constructor(
42
42
43
43
// PieChart variables
44
44
private var pieChart: PieChart ? = null
45
- private lateinit var slices: List <Slice >
45
+ private var slices: List <Slice >? = null
46
46
47
47
// Animation variables
48
48
private var animator: ValueAnimator ? = null
@@ -79,7 +79,7 @@ class ClickablePieChart @JvmOverloads constructor(
79
79
}
80
80
81
81
private fun initSlices () {
82
- slices = pieChart?.slices?.toList()!!
82
+ slices = pieChart?.slices?.toList()
83
83
}
84
84
85
85
private fun startAnimation () {
@@ -109,8 +109,12 @@ class ClickablePieChart @JvmOverloads constructor(
109
109
override fun onDraw (canvas : Canvas ? ) {
110
110
super .onDraw(canvas)
111
111
112
- if (pieChart != null ) {
113
- slices.forEach { slice ->
112
+ val centerX = (measuredWidth / 2 ).toFloat()
113
+ val centerY = (measuredHeight / 2 ).toFloat()
114
+ val radius = centerX.coerceAtMost(centerY)
115
+
116
+ if (slices.isNullOrEmpty().not ()) {
117
+ slices?.forEach { slice ->
114
118
val arc = slice.arc!!
115
119
if (currentSweepAngle > arc.startAngle + arc.sweepAngle) {
116
120
slicePaint.color = ContextCompat .getColor(context, slice.color)
@@ -135,16 +139,24 @@ class ClickablePieChart @JvmOverloads constructor(
135
139
}
136
140
}
137
141
138
- val centerX = (measuredWidth / 2 ).toFloat()
139
- val centerY = (measuredHeight / 2 ).toFloat()
140
- val radius = centerX.coerceAtMost(centerY)
141
-
142
142
canvas!! .drawCircle(
143
143
rectF!! .centerX(),
144
144
rectF!! .centerY(),
145
145
radius - pieChart?.sliceWidth!! ,
146
146
centerPaint
147
147
)
148
+
149
+ } else {
150
+ val width = pieChart?.sliceWidth ? : 80f
151
+ slicePaint.color = ContextCompat .getColor(context, R .color.semiGray)
152
+ canvas!! .drawArc(rectF!! , 0f , 360f , true , slicePaint)
153
+ canvas.drawCircle(
154
+ rectF!! .centerX(),
155
+ rectF!! .centerY(),
156
+ radius - width,
157
+ centerPaint
158
+ )
159
+
148
160
}
149
161
}
150
162
@@ -171,7 +183,7 @@ class ClickablePieChart @JvmOverloads constructor(
171
183
172
184
var total = 0.0f
173
185
var forEachStopper = false // what a idiot stuff
174
- slices.forEachIndexed { index, slice ->
186
+ slices? .forEachIndexed { index, slice ->
175
187
total + = (slice.scaledValue ? : 0f ) % 360f
176
188
if (touchAngle <= total && ! forEachStopper) {
177
189
pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
@@ -192,14 +204,19 @@ class ClickablePieChart @JvmOverloads constructor(
192
204
val width = LinearLayout .LayoutParams .WRAP_CONTENT
193
205
val height = LinearLayout .LayoutParams .WRAP_CONTENT
194
206
val popupWindow = PopupWindow (popupView, width, height, true )
195
- var center = slices[ index] .arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
207
+ var center = slices?.get( index)? .arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
196
208
val halfRadius = rectF!! .centerX()
197
209
198
210
popupView.findViewById<TextView >(R .id.textViewPopupText).text =
199
- " ${slices[ index] .dataPoint.toInt()} $popupText "
211
+ " ${slices?.get( index) !! .dataPoint.toInt()} $popupText "
200
212
ImageViewCompat .setImageTintList(
201
213
popupView.findViewById(R .id.imageViewPopupCircleIndicator),
202
- ColorStateList .valueOf(ContextCompat .getColor(context, slices[index].color))
214
+ ColorStateList .valueOf(
215
+ ContextCompat .getColor(
216
+ context,
217
+ slices?.get(index)?.color ? : R .color.semiGray
218
+ )
219
+ )
203
220
)
204
221
205
222
val calculatedX =
0 commit comments