@@ -12,12 +12,15 @@ import android.graphics.Color
12
12
import android.graphics.Paint
13
13
import android.graphics.RectF
14
14
import android.util.AttributeSet
15
+ import android.util.Log
15
16
import android.view.Gravity
16
17
import android.view.LayoutInflater
17
18
import android.view.MotionEvent
18
19
import android.view.View
19
20
import android.view.animation.LinearInterpolator
20
- import android.widget.*
21
+ import android.widget.LinearLayout
22
+ import android.widget.PopupWindow
23
+ import android.widget.TextView
21
24
import androidx.core.content.ContextCompat
22
25
import androidx.core.view.doOnPreDraw
23
26
import androidx.core.widget.ImageViewCompat
@@ -35,8 +38,6 @@ class ClickablePieChart @JvmOverloads constructor(
35
38
private var slicePaint: Paint = Paint ()
36
39
private var centerPaint: Paint = Paint (Paint .ANTI_ALIAS_FLAG )
37
40
private var rectF: RectF ? = null
38
- private var sliceStartPoint = 0F // FIXME: 16-Aug-20 remove if unnecessary
39
- private var sliceWidth = 80f
40
41
private var touchX = 0f
41
42
private var touchY = 0f
42
43
@@ -49,7 +50,7 @@ class ClickablePieChart @JvmOverloads constructor(
49
50
private var currentSweepAngle = 0
50
51
51
52
// Attributes
52
- private lateinit var popupText: String
53
+ private var popupText: String? = null
53
54
54
55
init {
55
56
initAttributes(attrs)
@@ -72,7 +73,7 @@ class ClickablePieChart @JvmOverloads constructor(
72
73
context.theme.obtainStyledAttributes(attrs, R .styleable.ClickablePieChart , 0 , 0 )
73
74
74
75
try {
75
- popupText = typedArray.getString(R .styleable.ClickablePieChart_popupText )!!
76
+ popupText = typedArray.getString(R .styleable.ClickablePieChart_popupText ) ? : " "
76
77
} finally {
77
78
typedArray.recycle()
78
79
}
@@ -163,18 +164,21 @@ class ClickablePieChart @JvmOverloads constructor(
163
164
)
164
165
)
165
166
166
- // FIXME: 16-Aug-20 Remove subtraction if unnecessary. On runtime sliceStartPoint is always 0f.
167
- // touchAngle -= sliceStartPoint
168
- touchAngle % = 360
167
+ Log .v(" qqq" , touchAngle.toString())
168
+
169
+
170
+ touchAngle - = pieChart?.sliceStartPoint ? : 0f
169
171
170
172
if (touchAngle < 0 ) {
171
173
touchAngle + = 360.0
172
174
}
173
175
176
+ Log .v(" qqq" , touchAngle.toString())
177
+
174
178
var total = 0.0f
175
179
var forEachStopper = false // what a idiot stuff
176
180
slices.forEachIndexed { index, slice ->
177
- total + = slice.dataPoint % 360f
181
+ total + = ( slice.dataPoint) % 360f
178
182
if (touchAngle <= total && ! forEachStopper) {
179
183
pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
180
184
forEachStopper = true
@@ -194,13 +198,13 @@ class ClickablePieChart @JvmOverloads constructor(
194
198
val width = LinearLayout .LayoutParams .WRAP_CONTENT
195
199
val height = LinearLayout .LayoutParams .WRAP_CONTENT
196
200
val popupWindow = PopupWindow (popupView, width, height, true )
197
- var center = slices[index].arc?.average()!!
201
+ var center = slices[index].arc?.average()!! + pieChart?.sliceStartPoint?.toDouble() !!
198
202
val halfRadius = rectF!! .centerX()
199
203
200
204
popupView.findViewById<TextView >(R .id.textViewPopupText).text =
201
- " ${center .toInt()} $popupText "
205
+ " ${slices[index].arc?.average()? .toInt()} $popupText "
202
206
ImageViewCompat .setImageTintList(
203
- popupView.findViewById< ImageView > (R .id.imageViewPopupCircleIndicator),
207
+ popupView.findViewById(R .id.imageViewPopupCircleIndicator),
204
208
ColorStateList .valueOf(ContextCompat .getColor(context, slices[index].color))
205
209
)
206
210
@@ -239,6 +243,7 @@ class ClickablePieChart @JvmOverloads constructor(
239
243
fun setPieChart (pieChart : PieChart ) {
240
244
this .pieChart = pieChart
241
245
init ()
246
+ invalidateAndRequestLayout()
242
247
}
243
248
244
249
fun setCenterColor (colorId : Int ) {
0 commit comments