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 16, 2020
1 parent 528b968 commit 24dc32e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
34 changes: 32 additions & 2 deletions app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.faskn.clickablepiechart
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.faskn.lib.PieChart
import com.faskn.lib.Slice
import com.faskn.lib.buildChart
import kotlinx.android.synthetic.main.activity_main.*
Expand All @@ -12,8 +13,9 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Kotlin DSL example
val pieChart1 = buildChart {
val pieChartDSL = buildChart {
slices {
arrayListOf(
Slice(
Expand Down Expand Up @@ -46,7 +48,35 @@ class MainActivity : AppCompatActivity() {
}
}

chart.setPieChart(pieChart1)
chart.setPieChart(pieChartDSL)
chart.showLegend(legendLayout)

val pieChart = PieChart(
arrayListOf(
Slice(
Random.nextInt(1000, 3000).toFloat(),
R.color.colorPrimary,
"Google"
),
Slice(
Random.nextInt(1000, 2000).toFloat(),
R.color.colorPrimaryDark,
"Facebook"
),
Slice(
Random.nextInt(1000, 5000).toFloat(),
R.color.materialIndigo600,
"Twitter"
),
Slice(
Random.nextInt(1000, 10000).toFloat(),
R.color.colorAccent,
"Other"
)
), clickListener = null, sliceStartPoint = 0f, sliceWidth = 80f
).build()

chart.setPieChart(pieChart)
chart.showLegend(legendLayout)
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
app:popupText="Ziyaret"
app:showPopup="true" />

<RelativeLayout
<FrameLayout
android:id="@+id/legendLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
android:layout_weight="1.5" />
</LinearLayout>
</RelativeLayout>
13 changes: 5 additions & 8 deletions lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.*
import android.view.animation.LinearInterpolator
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.doOnPreDraw
Expand Down Expand Up @@ -212,11 +208,12 @@ 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?.get(index)?.arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
val center = slices?.get(index)?.arc?.average()!! + pieChart?.sliceStartPoint?.toDouble()!!
val halfRadius = rectF!!.centerX()

popupView.findViewById<TextView>(R.id.textViewPopupText).text =
"${slices?.get(index)!!.dataPoint.toInt()} $popupText"

ImageViewCompat.setImageTintList(
popupView.findViewById(R.id.imageViewPopupCircleIndicator),
ColorStateList.valueOf(
Expand Down Expand Up @@ -270,11 +267,11 @@ class ClickablePieChart @JvmOverloads constructor(
invalidateAndRequestLayout()
}

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

fun showLegend(rootLayout: RelativeLayout) {
fun showLegend(rootLayout: ViewGroup) {
val recyclerView = RecyclerView(context)
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
recyclerView.layoutManager = linearLayoutManager
Expand Down
26 changes: 25 additions & 1 deletion lib/src/main/java/com/faskn/lib/PieChartDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,31 @@ data class PieChart(
var clickListener: ((String, Float) -> Unit)?,
var sliceStartPoint: Float?,
var sliceWidth: Float?
)
) {
fun build(): PieChart {
initScaledArcs()
return PieChart(slices, clickListener, sliceStartPoint, sliceWidth)
}

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)
}
}
}

private fun getSumOfDataPoints(): Float {
return slices.sumByDouble { slice -> slice.dataPoint.toDouble() }.toFloat()
}
}

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

Expand Down

0 comments on commit 24dc32e

Please sign in to comment.