Skip to content

Commit 2f02360

Browse files
authored
Merge pull request #4 from furkanaskin/dev
Added percentage and animationDuration attributes.
2 parents d288302 + 104c6e0 commit 2f02360

7 files changed

Lines changed: 80 additions & 7 deletions

File tree

.idea/codeStyles/Project.xml

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,15 @@ chart.showLegend(legendLayout)
7272
<td>boolean</td>
7373
<td>Show popup when user clicks on pie chart.</td>
7474
</tr>
75+
<tr>
76+
<td>app:showPercentage</td>
77+
<td>boolean</td>
78+
<td>Show percentage of slice beside popupText.</td>
79+
</tr>
80+
<tr>
81+
<td>app:animationDuration</td>
82+
<td>integer</td>
83+
<td>Animation duration with milliseconds.</td>
84+
</tr>
7585
</tbody>
7686
</table>

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
android:layout_weight="1"
2121
app:centerColor="@color/white"
2222
app:popupText="Ziyaret"
23+
app:showPercentage="true"
24+
app:animationDuration="2000"
2325
app:showPopup="true" />
2426

2527
<FrameLayout

lib/src/main/java/com/faskn/lib/ClickablePieChart.kt

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.graphics.Canvas
1111
import android.graphics.Color
1212
import android.graphics.Paint
1313
import android.graphics.RectF
14+
import android.graphics.drawable.ColorDrawable
1415
import android.util.AttributeSet
1516
import android.view.*
1617
import android.view.animation.LinearInterpolator
@@ -23,6 +24,7 @@ import androidx.core.widget.ImageViewCompat
2324
import androidx.recyclerview.widget.LinearLayoutManager
2425
import androidx.recyclerview.widget.RecyclerView
2526
import com.faskn.lib.legend.LegendAdapter
27+
import kotlin.math.abs
2628
import kotlin.math.atan2
2729
import kotlin.math.cos
2830
import kotlin.math.sin
@@ -54,9 +56,11 @@ class ClickablePieChart @JvmOverloads constructor(
5456
private var animator: ValueAnimator? = null
5557
private var currentSweepAngle = 0
5658
private var showPopup = true
59+
private var animationDuration: Int = 1000
5760

5861
// Attributes
5962
private var popupText: String? = null
63+
private var showPercentage = false
6064

6165
init {
6266
initAttributes(attrs)
@@ -73,10 +77,18 @@ class ClickablePieChart @JvmOverloads constructor(
7377

7478
try {
7579
popupText = typedArray.getString(R.styleable.ClickablePieChart_popupText) ?: ""
80+
81+
showPercentage =
82+
typedArray.getBoolean(R.styleable.ClickablePieChart_showPercentage, false)
83+
84+
animationDuration =
85+
abs(typedArray.getInt(R.styleable.ClickablePieChart_animationDuration, 0))
86+
7687
centerPaint.color = typedArray.getColor(
7788
R.styleable.ClickablePieChart_centerColor,
7889
ContextCompat.getColor(context, android.R.color.white)
7990
)
91+
8092
showPopup = typedArray.getBoolean(R.styleable.ClickablePieChart_showPopup, true)
8193
} finally {
8294
typedArray.recycle()
@@ -90,7 +102,7 @@ class ClickablePieChart @JvmOverloads constructor(
90102
private fun startAnimation() {
91103
animator?.cancel()
92104
animator = ValueAnimator.ofInt(0, 360).apply {
93-
duration = 1000
105+
duration = animationDuration.toLong()
94106
interpolator = LinearInterpolator()
95107
addUpdateListener { valueAnimator ->
96108
currentSweepAngle = valueAnimator.animatedValue as Int
@@ -211,8 +223,11 @@ class ClickablePieChart @JvmOverloads constructor(
211223
val center = slices?.get(index)?.arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
212224
val halfRadius = rectF!!.centerX()
213225

214-
popupView.findViewById<TextView>(R.id.textViewPopupText).text =
215-
"${slices?.get(index)!!.dataPoint.toInt()} $popupText"
226+
var popupText = "${slices?.get(index)!!.dataPoint.toInt()} $popupText"
227+
if (showPercentage) {
228+
popupText = "$popupText (%${slices?.get(index)!!.percentage})"
229+
}
230+
popupView.findViewById<TextView>(R.id.textViewPopupText).text = popupText
216231

217232
ImageViewCompat.setImageTintList(
218233
popupView.findViewById(R.id.imageViewPopupCircleIndicator),
@@ -239,6 +254,8 @@ class ClickablePieChart @JvmOverloads constructor(
239254
val popupWindowY =
240255
(currentViewLocation[1] + halfRadius.toInt()) + calculatedY -
241256
(if (calculatedY < 0) -halfOfSliceWidth else halfOfSliceWidth)
257+
258+
popupWindow.setBackgroundDrawable(ColorDrawable())
242259
popupWindow.showAtLocation(
243260
this,
244261
Gravity.NO_GRAVITY,

lib/src/main/java/com/faskn/lib/PieChartDsl.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ data class PieChart(
1414
) {
1515
fun build(): PieChart {
1616
initScaledArcs()
17+
initPercentages()
1718
return PieChart(slices, clickListener, sliceStartPoint, sliceWidth)
1819
}
1920

@@ -32,6 +33,21 @@ data class PieChart(
3233
}
3334
}
3435

36+
private fun initPercentages() {
37+
var remainder = 100
38+
slices.forEach { slice ->
39+
val percentage = (100 * slice.scaledValue!!.toInt()) / 360
40+
slice.percentage = percentage
41+
remainder -= percentage
42+
}
43+
var i = 0
44+
while (remainder != 0) {
45+
slices[i].percentage = slices[i].percentage!! + 1
46+
remainder -= 1
47+
i = (i + 1) % 4
48+
}
49+
}
50+
3551
private fun getSumOfDataPoints(): Float {
3652
return slices.sumByDouble { slice -> slice.dataPoint.toDouble() }.toFloat()
3753
}
@@ -64,6 +80,7 @@ class PieChartBuilder {
6480

6581
fun build(): PieChart {
6682
initScaledArcs()
83+
initPercentages()
6784
return PieChart(slices, clickListener, sliceStartPoint, sliceWidth)
6885
}
6986

@@ -82,6 +99,21 @@ class PieChartBuilder {
8299
}
83100
}
84101

102+
private fun initPercentages() {
103+
var remainder = 100
104+
slices.forEach { slice ->
105+
val percentage = (100 * slice.scaledValue!!.toInt()) / 360
106+
slice.percentage = percentage
107+
remainder -= percentage
108+
}
109+
var i = 0
110+
while (remainder != 0) {
111+
slices[i].percentage = slices[i].percentage!! + 1
112+
remainder -= 1
113+
i = (i + 1) % 4
114+
}
115+
}
116+
85117
private fun getSumOfDataPoints(): Float {
86118
return slices.sumByDouble { slice -> slice.dataPoint.toDouble() }.toFloat()
87119
}

lib/src/main/java/com/faskn/lib/Slice.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ data class Slice(
1212
@ColorRes val color: Int,
1313
val name: String,
1414
var arc: Arc? = null,
15-
@Px var scaledValue: Float? = 0f
15+
@Px var scaledValue: Float? = 0f,
16+
var percentage: Int? = null
1617
)
1718

1819
data class Arc(

lib/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
<attr name="popupText" format="string" />
55
<attr name="centerColor" format="color" />
66
<attr name="showPopup" format="boolean" />
7+
<attr name="showPercentage" format="boolean" />
8+
<attr name="animationDuration" format="integer" />
79
</declare-styleable>
810
</resources>

0 commit comments

Comments
 (0)