Skip to content

Commit

Permalink
[Fixed - #2] - Pop-up position fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
furkanaskin committed Aug 19, 2020
1 parent 6f6a0ba commit 9c18f2a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
16 changes: 8 additions & 8 deletions app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MainActivity : AppCompatActivity() {
chart.setCenterColor(R.color.white)

// Example
val pieChart0 = PieChart.Builder(
/* val pieChart0 = PieChart.Builder(
arrayOf(
Slice(30f, R.color.colorPrimary),
Slice(60f, R.color.colorPrimaryDark),
Expand All @@ -27,15 +27,15 @@ class MainActivity : AppCompatActivity() {
Log.d("ses", "s " + string)
Log.d("ses", "f " + float.toString())
}
.build()
.build()*/

// Example 2
val pieChart1 = PieChart.Builder(
arrayOf(
Slice(Random.nextInt(0, 100).toFloat(), R.color.colorPrimary),
Slice(Random.nextInt(0, 100).toFloat(), R.color.colorPrimaryDark),
Slice(Random.nextInt(0, 100).toFloat(), R.color.materialIndigo600),
Slice(Random.nextInt(0, 100).toFloat(), R.color.colorAccent)
Slice(Random.nextInt(1000, 3000).toFloat(), R.color.colorPrimary),
Slice(Random.nextInt(1000, 2000).toFloat(), R.color.colorPrimaryDark),
Slice(Random.nextInt(1000, 5000).toFloat(), R.color.materialIndigo600),
Slice(Random.nextInt(1000, 10000).toFloat(), R.color.colorAccent)
)
)
.setClickListener { string, float ->
Expand All @@ -44,6 +44,6 @@ class MainActivity : AppCompatActivity() {
}
.build()

chart.setPieChart(pieChart0)
chart.setPieChart(pieChart1)
}
}
}
10 changes: 2 additions & 8 deletions lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
Expand Down Expand Up @@ -164,21 +163,16 @@ class ClickablePieChart @JvmOverloads constructor(
)
)

Log.v("qqq", touchAngle.toString())


touchAngle -= pieChart?.sliceStartPoint ?: 0f

if (touchAngle < 0) {
touchAngle += 360.0
}

Log.v("qqq", touchAngle.toString())

var total = 0.0f
var forEachStopper = false // what a idiot stuff
slices.forEachIndexed { index, slice ->
total += (slice.dataPoint) % 360f
total += (slice.scaledValue ?: 0f) % 360f
if (touchAngle <= total && !forEachStopper) {
pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
forEachStopper = true
Expand All @@ -202,7 +196,7 @@ class ClickablePieChart @JvmOverloads constructor(
val halfRadius = rectF!!.centerX()

popupView.findViewById<TextView>(R.id.textViewPopupText).text =
"${slices[index].arc?.average()?.toInt()} $popupText"
"${slices[index].dataPoint} $popupText"
ImageViewCompat.setImageTintList(
popupView.findViewById(R.id.imageViewPopupCircleIndicator),
ColorStateList.valueOf(ContextCompat.getColor(context, slices[index].color))
Expand Down
5 changes: 4 additions & 1 deletion lib/src/main/java/com/faskn/lib/PieChart.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.faskn.lib

import android.util.Log

/**
* Created by turkergoksu on 12-Aug-20
*/
Expand All @@ -13,7 +15,7 @@ class PieChart private constructor(
data class Builder(
private var slices: Array<Slice>,
private var clickListener: ((String, Float) -> Unit)? = null,
private var sliceStartPoint: Float? = 90f,
private var sliceStartPoint: Float? = 0f,
private var sliceWidth: Float? = 80f
) {
init {
Expand Down Expand Up @@ -42,6 +44,7 @@ class PieChart private constructor(
private fun initScaledArcs() {
slices.forEachIndexed { i, slice ->
val scaledValue = (slice.dataPoint / getSumOfDataPoints()) * 360
slice.scaledValue = scaledValue
if (i != 0) {
slice.arc = Arc(
slices[i - 1].arc?.sweepAngle!!,
Expand Down
3 changes: 2 additions & 1 deletion lib/src/main/java/com/faskn/lib/Slice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package com.faskn.lib
data class Slice(
val dataPoint: Float,
val color: Int,
var arc: Arc? = null
var arc: Arc? = null,
var scaledValue : Float? = 0f
)

data class Arc(
Expand Down

0 comments on commit 9c18f2a

Please sign in to comment.