Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
furkanaskin authored and Furkan committed Sep 15, 2020
1 parent d887856 commit 91d1c50
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 93 deletions.
15 changes: 3 additions & 12 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@ import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.faskn.lib.Slice
import com.faskn.lib.pieChart
import com.faskn.lib.buildChart
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.random.Random

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
chart.setCenterColor(R.color.white)

// Kotlin DSL example
val pieChart1 = pieChart {
val pieChart1 = buildChart {
slices {
arrayOf(
arrayListOf(
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)
)
}

sliceWidth { 80f }
sliceStartPoint { 0f }
clickListener { s, fl ->
Expand Down
8 changes: 5 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<com.faskn.lib.ClickablePieChart
android:id="@+id/chart"
android:layout_width="250dp"
app:popupText="Ziyaret"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:layout_height="250dp" />
app:centerColor="@color/white"
app:popupText="Ziyaret"
app:showPopup="true" />
</RelativeLayout>
39 changes: 24 additions & 15 deletions lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,29 @@ import kotlin.math.atan2
import kotlin.math.cos
import kotlin.math.sin


class ClickablePieChart @JvmOverloads constructor(
context: Context,
private val attrs: AttributeSet? = null,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

private var slicePaint: Paint = Paint()
private var centerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
private var centerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.WHITE
style = Paint.Style.FILL
}
private var rectF: RectF? = null
private var touchX = 0f
private var touchY = 0f

// PieChart variables
private var pieChart: PieChart? = null
private var slices: Array<Slice>? = null
private var slices: ArrayList<Slice>? = null

// Animation variables
private var animator: ValueAnimator? = null
private var currentSweepAngle = 0
private var showPopup = true

// Attributes
private var popupText: String? = null
Expand All @@ -60,9 +63,6 @@ class ClickablePieChart @JvmOverloads constructor(
slicePaint.isDither = true
slicePaint.style = Paint.Style.FILL

centerPaint.color = Color.WHITE
centerPaint.style = Paint.Style.FILL

initSlices()
startAnimation()
}
Expand All @@ -73,6 +73,11 @@ class ClickablePieChart @JvmOverloads constructor(

try {
popupText = typedArray.getString(R.styleable.ClickablePieChart_popupText) ?: ""
centerPaint.color = typedArray.getColor(
R.styleable.ClickablePieChart_centerColor,
ContextCompat.getColor(context, android.R.color.white)
)
showPopup = typedArray.getBoolean(R.styleable.ClickablePieChart_showPopup, true)
} finally {
typedArray.recycle()
}
Expand Down Expand Up @@ -182,16 +187,16 @@ class ClickablePieChart @JvmOverloads constructor(
}

var total = 0.0f
var forEachStopper = false // what a idiot stuff
slices?.forEachIndexed { index, slice ->
total += (slice.scaledValue ?: 0f) % 360f
if (touchAngle <= total && !forEachStopper) {
pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
forEachStopper = true
showInfoPopup(index)
run {
slices?.forEachIndexed { index, slice ->
total += (slice.scaledValue ?: 0f) % 360f
if (touchAngle <= total && showPopup) {
pieChart?.clickListener?.invoke(touchAngle.toString(), index.toFloat())
showInfoPopup(index)
return@run
}
}
}
forEachStopper = false
true
}
else -> false
Expand Down Expand Up @@ -262,6 +267,10 @@ class ClickablePieChart @JvmOverloads constructor(
invalidateAndRequestLayout()
}

fun setShowPopup(show: Boolean) {
showPopup = show
}

private fun invalidateAndRequestLayout() {
invalidate()
requestLayout()
Expand Down
32 changes: 15 additions & 17 deletions lib/src/main/java/com/faskn/lib/PieChartDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ package com.faskn.lib
@DslMarker
annotation class PieChartDsl
data class PieChart(
var slices: Array<Slice>?,
var slices: ArrayList<Slice>,
var clickListener: ((String, Float) -> Unit)?,
var sliceStartPoint: Float?,
var sliceWidth: Float?
)

fun pieChart(block: PieChartBuilder.() -> Unit) = PieChartBuilder().apply(block).build()
fun buildChart(block: PieChartBuilder.() -> Unit) = PieChartBuilder().apply(block).build()

@PieChartDsl
class PieChartBuilder {
private var slices: Array<Slice>? = null
private lateinit var slices: ArrayList<Slice>
private var clickListener: ((String, Float) -> Unit)? = null
private var sliceStartPoint = 0f
private var sliceWidth = 80f

fun slices(block: () -> Array<Slice>) {
fun slices(block: () -> ArrayList<Slice>) {
slices = block()
}

Expand All @@ -44,23 +44,21 @@ class PieChartBuilder {
}

private fun initScaledArcs() {
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)
}
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() ?: 0f
return slices.sumByDouble { slice -> slice.dataPoint.toDouble() }.toFloat()
}
}
7 changes: 5 additions & 2 deletions lib/src/main/java/com/faskn/lib/Slice.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.faskn.lib

import androidx.annotation.ColorRes
import androidx.annotation.Px

/**
* Created by turkergoksu on 12-Aug-20
*/

data class Slice(
val dataPoint: Float,
val color: Int,
@ColorRes val color: Int,
var arc: Arc? = null,
var scaledValue: Float? = 0f
@Px var scaledValue: Float? = 0f
)

data class Arc(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/res/drawable/circle_indicator.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/black" />
<solid android:color="@android:color/holo_red_dark" />
<size
android:width="8dp"
android:height="8dp" />
Expand Down
37 changes: 0 additions & 37 deletions lib/src/main/res/layout/layout_info.xml

This file was deleted.

4 changes: 3 additions & 1 deletion lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ClickablePieChart">
<attr name="popupText" format="string"/>
<attr name="popupText" format="string" />
<attr name="centerColor" format="color" />
<attr name="showPopup" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit 91d1c50

Please sign in to comment.