diff --git a/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt b/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt index 0f9cbd7..a4b3bf6 100644 --- a/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt +++ b/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt @@ -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), @@ -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 -> @@ -44,6 +44,6 @@ class MainActivity : AppCompatActivity() { } .build() - chart.setPieChart(pieChart0) + chart.setPieChart(pieChart1) } -} \ No newline at end of file +} diff --git a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt index 1beb07f..6dc6461 100644 --- a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt +++ b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt @@ -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 @@ -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 @@ -202,7 +196,7 @@ class ClickablePieChart @JvmOverloads constructor( val halfRadius = rectF!!.centerX() popupView.findViewById(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)) diff --git a/lib/src/main/java/com/faskn/lib/PieChart.kt b/lib/src/main/java/com/faskn/lib/PieChart.kt index a154843..79e2651 100644 --- a/lib/src/main/java/com/faskn/lib/PieChart.kt +++ b/lib/src/main/java/com/faskn/lib/PieChart.kt @@ -1,5 +1,7 @@ package com.faskn.lib +import android.util.Log + /** * Created by turkergoksu on 12-Aug-20 */ @@ -13,7 +15,7 @@ class PieChart private constructor( data class Builder( private var slices: Array, private var clickListener: ((String, Float) -> Unit)? = null, - private var sliceStartPoint: Float? = 90f, + private var sliceStartPoint: Float? = 0f, private var sliceWidth: Float? = 80f ) { init { @@ -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!!, diff --git a/lib/src/main/java/com/faskn/lib/Slice.kt b/lib/src/main/java/com/faskn/lib/Slice.kt index 2dc61d6..f983164 100644 --- a/lib/src/main/java/com/faskn/lib/Slice.kt +++ b/lib/src/main/java/com/faskn/lib/Slice.kt @@ -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(