Skip to content

Commit

Permalink
Added animation, fixed sliceStartPoint, added popup text.
Browse files Browse the repository at this point in the history
  • Loading branch information
furkanaskin committed Aug 16, 2020
1 parent e756245 commit 6f6a0ba
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
14 changes: 7 additions & 7 deletions app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class MainActivity : AppCompatActivity() {
// Example
val pieChart0 = PieChart.Builder(
arrayOf(
Slice(30f, R.color.brown700),
Slice(60f, R.color.materialRed700),
Slice(30f, R.color.colorPrimary),
Slice(60f, R.color.colorPrimaryDark),
Slice(120f, R.color.materialIndigo600),
Slice(150f, R.color.materialRed400)
Slice(150f, R.color.colorAccent)
)
)
).setSliceStartPoint(-90f)
.setClickListener { string, float ->
Log.d("ses", "s " + string)
Log.d("ses", "f " + float.toString())
Expand All @@ -32,10 +32,10 @@ class MainActivity : AppCompatActivity() {
// Example 2
val pieChart1 = PieChart.Builder(
arrayOf(
Slice(Random.nextInt(0, 100).toFloat(), R.color.brown700),
Slice(Random.nextInt(0, 100).toFloat(), R.color.materialRed700),
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.materialRed400)
Slice(Random.nextInt(0, 100).toFloat(), R.color.colorAccent)
)
)
.setClickListener { string, float ->
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<com.faskn.lib.ClickablePieChart
android:id="@+id/chart"
android:layout_width="250dp"
app:popupText="ziyaret"
app:popupText="Ziyaret"
android:layout_centerInParent="true"
android:layout_height="250dp" />
</RelativeLayout>
29 changes: 17 additions & 12 deletions lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ 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
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.*
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.doOnPreDraw
import androidx.core.widget.ImageViewCompat
Expand All @@ -35,8 +38,6 @@ class ClickablePieChart @JvmOverloads constructor(
private var slicePaint: Paint = Paint()
private var centerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
private var rectF: RectF? = null
private var sliceStartPoint = 0F // FIXME: 16-Aug-20 remove if unnecessary
private var sliceWidth = 80f
private var touchX = 0f
private var touchY = 0f

Expand All @@ -49,7 +50,7 @@ class ClickablePieChart @JvmOverloads constructor(
private var currentSweepAngle = 0

// Attributes
private lateinit var popupText: String
private var popupText: String? = null

init {
initAttributes(attrs)
Expand All @@ -72,7 +73,7 @@ class ClickablePieChart @JvmOverloads constructor(
context.theme.obtainStyledAttributes(attrs, R.styleable.ClickablePieChart, 0, 0)

try {
popupText = typedArray.getString(R.styleable.ClickablePieChart_popupText)!!
popupText = typedArray.getString(R.styleable.ClickablePieChart_popupText) ?: ""
} finally {
typedArray.recycle()
}
Expand Down Expand Up @@ -163,18 +164,21 @@ class ClickablePieChart @JvmOverloads constructor(
)
)

// FIXME: 16-Aug-20 Remove subtraction if unnecessary. On runtime sliceStartPoint is always 0f.
// touchAngle -= sliceStartPoint
touchAngle %= 360
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.dataPoint) % 360f
if (touchAngle <= total && !forEachStopper) {
pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
forEachStopper = true
Expand All @@ -194,13 +198,13 @@ 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()!!
var center = slices[index].arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
val halfRadius = rectF!!.centerX()

popupView.findViewById<TextView>(R.id.textViewPopupText).text =
"${center.toInt()} $popupText"
"${slices[index].arc?.average()?.toInt()} $popupText"
ImageViewCompat.setImageTintList(
popupView.findViewById<ImageView>(R.id.imageViewPopupCircleIndicator),
popupView.findViewById(R.id.imageViewPopupCircleIndicator),
ColorStateList.valueOf(ContextCompat.getColor(context, slices[index].color))
)

Expand Down Expand Up @@ -239,6 +243,7 @@ class ClickablePieChart @JvmOverloads constructor(
fun setPieChart(pieChart: PieChart) {
this.pieChart = pieChart
init()
invalidateAndRequestLayout()
}

fun setCenterColor(colorId: Int) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/main/java/com/faskn/lib/PieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package com.faskn.lib
class PieChart private constructor(
var slices: Array<Slice>,
var clickListener: ((String, Float) -> Unit)? = null,
var sliceStartPoint: Float,
var sliceWidth: Float
var sliceStartPoint: Float = 0f,
var sliceWidth: Float = 80f
) {
data class Builder(
private var slices: Array<Slice>,
private var clickListener: ((String, Float) -> Unit)? = null,
private var sliceStartPoint: Float? = 0f,
private var sliceStartPoint: Float? = 90f,
private var sliceWidth: Float? = 80f
) {
init {
Expand Down

0 comments on commit 6f6a0ba

Please sign in to comment.