diff --git a/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt b/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt index a4b3bf6..bb37434 100644 --- a/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt +++ b/app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt @@ -15,19 +15,19 @@ class MainActivity : AppCompatActivity() { chart.setCenterColor(R.color.white) // Example - /* val pieChart0 = PieChart.Builder( - arrayOf( - Slice(30f, R.color.colorPrimary), - Slice(60f, R.color.colorPrimaryDark), - Slice(120f, R.color.materialIndigo600), - Slice(150f, R.color.colorAccent) - ) - ).setSliceStartPoint(-90f) - .setClickListener { string, float -> - Log.d("ses", "s " + string) - Log.d("ses", "f " + float.toString()) - } - .build()*/ + /* val pieChart0 = PieChart.Builder( + arrayOf( + Slice(30f, R.color.colorPrimary), + Slice(60f, R.color.colorPrimaryDark), + Slice(120f, R.color.materialIndigo600), + Slice(150f, R.color.colorAccent) + ) + ).setSliceStartPoint(-90f) + .setClickListener { string, float -> + Log.d("ses", "s " + string) + Log.d("ses", "f " + float.toString()) + } + .build()*/ // Example 2 val pieChart1 = PieChart.Builder( @@ -44,6 +44,16 @@ class MainActivity : AppCompatActivity() { } .build() + // Example 3 + /*val pieChart3 = PieChart.Builder( + null + ) + .setClickListener { string, float -> + Log.d("ses", "s " + string) + Log.d("ses", "f " + float.toString()) + } + .build()*/ + chart.setPieChart(pieChart1) } } diff --git a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt index 8fe5876..2b17bdf 100644 --- a/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt +++ b/lib/src/main/java/com/faskn/lib/ClickablePieChart.kt @@ -42,7 +42,7 @@ class ClickablePieChart @JvmOverloads constructor( // PieChart variables private var pieChart: PieChart? = null - private lateinit var slices: List + private var slices: List? = null // Animation variables private var animator: ValueAnimator? = null @@ -79,7 +79,7 @@ class ClickablePieChart @JvmOverloads constructor( } private fun initSlices() { - slices = pieChart?.slices?.toList()!! + slices = pieChart?.slices?.toList() } private fun startAnimation() { @@ -109,8 +109,12 @@ class ClickablePieChart @JvmOverloads constructor( override fun onDraw(canvas: Canvas?) { super.onDraw(canvas) - if (pieChart != null) { - slices.forEach { slice -> + val centerX = (measuredWidth / 2).toFloat() + val centerY = (measuredHeight / 2).toFloat() + val radius = centerX.coerceAtMost(centerY) + + if (slices.isNullOrEmpty().not()) { + slices?.forEach { slice -> val arc = slice.arc!! if (currentSweepAngle > arc.startAngle + arc.sweepAngle) { slicePaint.color = ContextCompat.getColor(context, slice.color) @@ -135,16 +139,24 @@ class ClickablePieChart @JvmOverloads constructor( } } - val centerX = (measuredWidth / 2).toFloat() - val centerY = (measuredHeight / 2).toFloat() - val radius = centerX.coerceAtMost(centerY) - canvas!!.drawCircle( rectF!!.centerX(), rectF!!.centerY(), radius - pieChart?.sliceWidth!!, centerPaint ) + + } else { + val width = pieChart?.sliceWidth ?: 80f + slicePaint.color = ContextCompat.getColor(context, R.color.semiGray) + canvas!!.drawArc(rectF!!, 0f, 360f, true, slicePaint) + canvas.drawCircle( + rectF!!.centerX(), + rectF!!.centerY(), + radius - width, + centerPaint + ) + } } @@ -171,7 +183,7 @@ class ClickablePieChart @JvmOverloads constructor( var total = 0.0f var forEachStopper = false // what a idiot stuff - slices.forEachIndexed { index, slice -> + slices?.forEachIndexed { index, slice -> total += (slice.scaledValue ?: 0f) % 360f if (touchAngle <= total && !forEachStopper) { pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat()) @@ -192,14 +204,19 @@ class ClickablePieChart @JvmOverloads constructor( val width = LinearLayout.LayoutParams.WRAP_CONTENT val height = LinearLayout.LayoutParams.WRAP_CONTENT val popupWindow = PopupWindow(popupView, width, height, true) - var center = slices[index].arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!! + var center = slices?.get(index)?.arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!! val halfRadius = rectF!!.centerX() popupView.findViewById(R.id.textViewPopupText).text = - "${slices[index].dataPoint.toInt()} $popupText" + "${slices?.get(index)!!.dataPoint.toInt()} $popupText" ImageViewCompat.setImageTintList( popupView.findViewById(R.id.imageViewPopupCircleIndicator), - ColorStateList.valueOf(ContextCompat.getColor(context, slices[index].color)) + ColorStateList.valueOf( + ContextCompat.getColor( + context, + slices?.get(index)?.color ?: R.color.semiGray + ) + ) ) val calculatedX = diff --git a/lib/src/main/java/com/faskn/lib/PieChart.kt b/lib/src/main/java/com/faskn/lib/PieChart.kt index 79e2651..cbca980 100644 --- a/lib/src/main/java/com/faskn/lib/PieChart.kt +++ b/lib/src/main/java/com/faskn/lib/PieChart.kt @@ -1,19 +1,17 @@ package com.faskn.lib -import android.util.Log - /** * Created by turkergoksu on 12-Aug-20 */ class PieChart private constructor( - var slices: Array, + var slices: Array?, var clickListener: ((String, Float) -> Unit)? = null, var sliceStartPoint: Float = 0f, - var sliceWidth: Float = 80f + var sliceWidth: Float? = 80f ) { data class Builder( - private var slices: Array, + private var slices: Array? = null, private var clickListener: ((String, Float) -> Unit)? = null, private var sliceStartPoint: Float? = 0f, private var sliceWidth: Float? = 80f @@ -42,22 +40,24 @@ 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!!, - slices[i - 1].arc?.sweepAngle!!.plus(scaledValue) - ) - } else { - slice.arc = Arc(0f, scaledValue) + if (slices != null) { + slices!!.forEachIndexed { i, slice -> + val scaledValue = (slice.dataPoint / getSumOfDataPoints()) * 360 + slice.scaledValue = scaledValue + if (i != 0) { + slice.arc = Arc( + slices!![i - 1].arc?.sweepAngle!!, + slices!![i - 1].arc?.sweepAngle!!.plus(scaledValue) + ) + } else { + slice.arc = Arc(0f, scaledValue) + } } } } private fun getSumOfDataPoints(): Float { - return slices.sumByDouble { slice -> slice.dataPoint.toDouble() }.toFloat() + return slices?.sumByDouble { slice -> slice.dataPoint.toDouble() }?.toFloat() ?: 0f } } } \ No newline at end of file diff --git a/lib/src/main/res/values/colors.xml b/lib/src/main/res/values/colors.xml new file mode 100644 index 0000000..e2e0d56 --- /dev/null +++ b/lib/src/main/res/values/colors.xml @@ -0,0 +1,4 @@ + + + #cecfd6 + \ No newline at end of file