Skip to content

Commit 89951de

Browse files
committed
Slice popup positioned
1 parent 45cdc16 commit 89951de

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

lib/src/main/java/com/faskn/lib/ClickablePieChart.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import android.graphics.Color
1212
import android.graphics.Paint
1313
import android.graphics.RectF
1414
import android.util.AttributeSet
15+
import android.util.Log
1516
import android.view.Gravity
1617
import android.view.LayoutInflater
1718
import android.view.MotionEvent
1819
import android.view.View
1920
import android.widget.*
2021
import androidx.core.content.ContextCompat
22+
import androidx.core.view.doOnPreDraw
2123
import androidx.core.widget.ImageViewCompat
2224
import kotlin.math.atan2
2325
import kotlin.math.cos
@@ -150,13 +152,29 @@ class ClickablePieChart @JvmOverloads constructor(
150152
val currentViewLocation = IntArray(2)
151153
this.getLocationOnScreen(currentViewLocation)
152154

155+
val halfOfSliceWidth = (sliceWidth / 2).toInt()
156+
val popupWindowX =
157+
(currentViewLocation[0] + halfRadius.toInt()) + calculatedX -
158+
(if (calculatedX < 0) -halfOfSliceWidth else halfOfSliceWidth)
159+
val popupWindowY =
160+
(currentViewLocation[1] + halfRadius.toInt()) + calculatedY -
161+
(if (calculatedY < 0) -halfOfSliceWidth else halfOfSliceWidth)
153162
popupWindow.showAtLocation(
154163
this,
155164
Gravity.NO_GRAVITY,
156-
(currentViewLocation[0] + halfRadius.toInt()) + calculatedX,
157-
(currentViewLocation[1] + halfRadius.toInt()) + calculatedY
165+
popupWindowX,
166+
popupWindowY
158167
)
159168

169+
popupView.doOnPreDraw {
170+
popupWindow.update(
171+
(popupWindowX - (it.width / 2)),
172+
popupWindowY,
173+
popupWindow.width,
174+
popupWindow.height
175+
)
176+
}
177+
160178
val currentData = dataPoints[index]
161179

162180
}

0 commit comments

Comments
 (0)