@@ -42,7 +42,7 @@ class ClickablePieChart @JvmOverloads constructor(
4242
4343 // PieChart variables
4444 private var pieChart: PieChart ? = null
45- private lateinit var slices: List <Slice >
45+ private var slices: List <Slice >? = null
4646
4747 // Animation variables
4848 private var animator: ValueAnimator ? = null
@@ -79,7 +79,7 @@ class ClickablePieChart @JvmOverloads constructor(
7979 }
8080
8181 private fun initSlices () {
82- slices = pieChart?.slices?.toList()!!
82+ slices = pieChart?.slices?.toList()
8383 }
8484
8585 private fun startAnimation () {
@@ -109,8 +109,12 @@ class ClickablePieChart @JvmOverloads constructor(
109109 override fun onDraw (canvas : Canvas ? ) {
110110 super .onDraw(canvas)
111111
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 ->
114118 val arc = slice.arc!!
115119 if (currentSweepAngle > arc.startAngle + arc.sweepAngle) {
116120 slicePaint.color = ContextCompat .getColor(context, slice.color)
@@ -135,16 +139,24 @@ class ClickablePieChart @JvmOverloads constructor(
135139 }
136140 }
137141
138- val centerX = (measuredWidth / 2 ).toFloat()
139- val centerY = (measuredHeight / 2 ).toFloat()
140- val radius = centerX.coerceAtMost(centerY)
141-
142142 canvas!! .drawCircle(
143143 rectF!! .centerX(),
144144 rectF!! .centerY(),
145145 radius - pieChart?.sliceWidth!! ,
146146 centerPaint
147147 )
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+
148160 }
149161 }
150162
@@ -171,7 +183,7 @@ class ClickablePieChart @JvmOverloads constructor(
171183
172184 var total = 0.0f
173185 var forEachStopper = false // what a idiot stuff
174- slices.forEachIndexed { index, slice ->
186+ slices? .forEachIndexed { index, slice ->
175187 total + = (slice.scaledValue ? : 0f ) % 360f
176188 if (touchAngle <= total && ! forEachStopper) {
177189 pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
@@ -192,14 +204,19 @@ class ClickablePieChart @JvmOverloads constructor(
192204 val width = LinearLayout .LayoutParams .WRAP_CONTENT
193205 val height = LinearLayout .LayoutParams .WRAP_CONTENT
194206 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()!!
196208 val halfRadius = rectF!! .centerX()
197209
198210 popupView.findViewById<TextView >(R .id.textViewPopupText).text =
199- " ${slices[ index] .dataPoint.toInt()} $popupText "
211+ " ${slices?.get( index) !! .dataPoint.toInt()} $popupText "
200212 ImageViewCompat .setImageTintList(
201213 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+ )
203220 )
204221
205222 val calculatedX =
0 commit comments