@@ -11,6 +11,7 @@ import android.graphics.Canvas
1111import android.graphics.Color
1212import android.graphics.Paint
1313import android.graphics.RectF
14+ import android.graphics.drawable.ColorDrawable
1415import android.util.AttributeSet
1516import android.view.*
1617import android.view.animation.LinearInterpolator
@@ -23,6 +24,7 @@ import androidx.core.widget.ImageViewCompat
2324import androidx.recyclerview.widget.LinearLayoutManager
2425import androidx.recyclerview.widget.RecyclerView
2526import com.faskn.lib.legend.LegendAdapter
27+ import kotlin.math.abs
2628import kotlin.math.atan2
2729import kotlin.math.cos
2830import kotlin.math.sin
@@ -54,9 +56,11 @@ class ClickablePieChart @JvmOverloads constructor(
5456 private var animator: ValueAnimator ? = null
5557 private var currentSweepAngle = 0
5658 private var showPopup = true
59+ private var animationDuration: Int = 1000
5760
5861 // Attributes
5962 private var popupText: String? = null
63+ private var showPercentage = false
6064
6165 init {
6266 initAttributes(attrs)
@@ -73,10 +77,18 @@ class ClickablePieChart @JvmOverloads constructor(
7377
7478 try {
7579 popupText = typedArray.getString(R .styleable.ClickablePieChart_popupText ) ? : " "
80+
81+ showPercentage =
82+ typedArray.getBoolean(R .styleable.ClickablePieChart_showPercentage , false )
83+
84+ animationDuration =
85+ abs(typedArray.getInt(R .styleable.ClickablePieChart_animationDuration , 0 ))
86+
7687 centerPaint.color = typedArray.getColor(
7788 R .styleable.ClickablePieChart_centerColor ,
7889 ContextCompat .getColor(context, android.R .color.white)
7990 )
91+
8092 showPopup = typedArray.getBoolean(R .styleable.ClickablePieChart_showPopup , true )
8193 } finally {
8294 typedArray.recycle()
@@ -90,7 +102,7 @@ class ClickablePieChart @JvmOverloads constructor(
90102 private fun startAnimation () {
91103 animator?.cancel()
92104 animator = ValueAnimator .ofInt(0 , 360 ).apply {
93- duration = 1000
105+ duration = animationDuration.toLong()
94106 interpolator = LinearInterpolator ()
95107 addUpdateListener { valueAnimator ->
96108 currentSweepAngle = valueAnimator.animatedValue as Int
@@ -211,8 +223,11 @@ class ClickablePieChart @JvmOverloads constructor(
211223 val center = slices?.get(index)?.arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
212224 val halfRadius = rectF!! .centerX()
213225
214- popupView.findViewById<TextView >(R .id.textViewPopupText).text =
215- " ${slices?.get(index)!! .dataPoint.toInt()} $popupText "
226+ var popupText = " ${slices?.get(index)!! .dataPoint.toInt()} $popupText "
227+ if (showPercentage) {
228+ popupText = " $popupText (%${slices?.get(index)!! .percentage} )"
229+ }
230+ popupView.findViewById<TextView >(R .id.textViewPopupText).text = popupText
216231
217232 ImageViewCompat .setImageTintList(
218233 popupView.findViewById(R .id.imageViewPopupCircleIndicator),
@@ -239,6 +254,8 @@ class ClickablePieChart @JvmOverloads constructor(
239254 val popupWindowY =
240255 (currentViewLocation[1 ] + halfRadius.toInt()) + calculatedY -
241256 (if (calculatedY < 0 ) - halfOfSliceWidth else halfOfSliceWidth)
257+
258+ popupWindow.setBackgroundDrawable(ColorDrawable ())
242259 popupWindow.showAtLocation(
243260 this ,
244261 Gravity .NO_GRAVITY ,
0 commit comments