Skip to content

Commit 528b968

Browse files
furkanaskinFurkan
authored andcommitted
Added legend
1 parent 91d1c50 commit 528b968

File tree

7 files changed

+155
-19
lines changed

7 files changed

+155
-19
lines changed

app/src/main/java/com/faskn/clickablepiechart/MainActivity.kt

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,30 @@ class MainActivity : AppCompatActivity() {
1212
override fun onCreate(savedInstanceState: Bundle?) {
1313
super.onCreate(savedInstanceState)
1414
setContentView(R.layout.activity_main)
15-
1615
// Kotlin DSL example
1716
val pieChart1 = buildChart {
1817
slices {
1918
arrayListOf(
20-
Slice(Random.nextInt(1000, 3000).toFloat(), R.color.colorPrimary),
21-
Slice(Random.nextInt(1000, 2000).toFloat(), R.color.colorPrimaryDark),
22-
Slice(Random.nextInt(1000, 5000).toFloat(), R.color.materialIndigo600),
23-
Slice(Random.nextInt(1000, 10000).toFloat(), R.color.colorAccent)
19+
Slice(
20+
Random.nextInt(1000, 3000).toFloat(),
21+
R.color.colorPrimary,
22+
"Google"
23+
),
24+
Slice(
25+
Random.nextInt(1000, 2000).toFloat(),
26+
R.color.colorPrimaryDark,
27+
"Facebook"
28+
),
29+
Slice(
30+
Random.nextInt(1000, 5000).toFloat(),
31+
R.color.materialIndigo600,
32+
"Twitter"
33+
),
34+
Slice(
35+
Random.nextInt(1000, 10000).toFloat(),
36+
R.color.colorAccent,
37+
"Other"
38+
)
2439
)
2540
}
2641
sliceWidth { 80f }
@@ -32,5 +47,6 @@ class MainActivity : AppCompatActivity() {
3247
}
3348

3449
chart.setPieChart(pieChart1)
50+
chart.showLegend(legendLayout)
3551
}
3652
}

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

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,26 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<com.faskn.lib.ClickablePieChart
10-
android:id="@+id/chart"
11-
android:layout_width="250dp"
12-
android:layout_height="250dp"
9+
<LinearLayout
10+
android:layout_width="match_parent"
11+
android:layout_height="wrap_content"
1312
android:layout_centerInParent="true"
14-
app:centerColor="@color/white"
15-
app:popupText="Ziyaret"
16-
app:showPopup="true" />
13+
android:layout_margin="24dp"
14+
android:orientation="horizontal">
15+
16+
<com.faskn.lib.ClickablePieChart
17+
android:id="@+id/chart"
18+
android:layout_width="match_parent"
19+
android:layout_height="match_parent"
20+
android:layout_weight="1"
21+
app:centerColor="@color/white"
22+
app:popupText="Ziyaret"
23+
app:showPopup="true" />
24+
25+
<RelativeLayout
26+
android:id="@+id/legendLayout"
27+
android:layout_width="match_parent"
28+
android:layout_height="wrap_content"
29+
android:layout_weight="1" />
30+
</LinearLayout>
1731
</RelativeLayout>

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ dependencies {
3434
implementation 'androidx.core:core-ktx:1.3.1'
3535
implementation 'androidx.appcompat:appcompat:1.2.0'
3636
implementation "androidx.cardview:cardview:1.0.0"
37+
implementation "androidx.recyclerview:recyclerview:1.1.0"
3738
testImplementation 'junit:junit:4.12'
3839
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3940
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
40-
4141
}

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ import android.view.View
1919
import android.view.animation.LinearInterpolator
2020
import android.widget.LinearLayout
2121
import android.widget.PopupWindow
22+
import android.widget.RelativeLayout
2223
import android.widget.TextView
2324
import androidx.core.content.ContextCompat
2425
import androidx.core.view.doOnPreDraw
2526
import androidx.core.widget.ImageViewCompat
27+
import androidx.recyclerview.widget.LinearLayoutManager
28+
import androidx.recyclerview.widget.RecyclerView
29+
import com.faskn.lib.legend.LegendAdapter
2630
import kotlin.math.atan2
2731
import kotlin.math.cos
2832
import kotlin.math.sin
@@ -33,7 +37,11 @@ class ClickablePieChart @JvmOverloads constructor(
3337
defStyleAttr: Int = 0
3438
) : View(context, attrs, defStyleAttr) {
3539

36-
private var slicePaint: Paint = Paint()
40+
private var slicePaint: Paint = Paint().apply {
41+
isAntiAlias = true
42+
isDither = true
43+
style = Paint.Style.FILL
44+
}
3745
private var centerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
3846
color = Color.WHITE
3947
style = Paint.Style.FILL
@@ -59,10 +67,6 @@ class ClickablePieChart @JvmOverloads constructor(
5967
}
6068

6169
private fun init() {
62-
slicePaint.isAntiAlias = true
63-
slicePaint.isDither = true
64-
slicePaint.style = Paint.Style.FILL
65-
6670
initSlices()
6771
startAnimation()
6872
}
@@ -161,7 +165,6 @@ class ClickablePieChart @JvmOverloads constructor(
161165
radius - width,
162166
centerPaint
163167
)
164-
165168
}
166169
}
167170

@@ -271,6 +274,18 @@ class ClickablePieChart @JvmOverloads constructor(
271274
showPopup = show
272275
}
273276

277+
fun showLegend(rootLayout: RelativeLayout) {
278+
val recyclerView = RecyclerView(context)
279+
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
280+
recyclerView.layoutManager = linearLayoutManager
281+
val adapter = LegendAdapter()
282+
slices?.toMutableList()?.let { adapter.setup(it) }
283+
recyclerView.adapter = adapter
284+
recyclerView.overScrollMode = OVER_SCROLL_NEVER
285+
rootLayout.addView(recyclerView)
286+
invalidateAndRequestLayout()
287+
}
288+
274289
private fun invalidateAndRequestLayout() {
275290
invalidate()
276291
requestLayout()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.annotation.Px
1010
data class Slice(
1111
val dataPoint: Float,
1212
@ColorRes val color: Int,
13+
val name: String,
1314
var arc: Arc? = null,
1415
@Px var scaledValue: Float? = 0f
1516
)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.faskn.lib.legend
2+
3+
import android.content.res.ColorStateList
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.core.content.ContextCompat
8+
import androidx.recyclerview.widget.RecyclerView
9+
import com.faskn.lib.R
10+
import com.faskn.lib.Slice
11+
import kotlinx.android.synthetic.main.item_legend.view.*
12+
13+
class LegendAdapter : RecyclerView.Adapter<LegendAdapter.ItemViewHolder>() {
14+
15+
private val items = mutableListOf<Slice>()
16+
17+
var onItemClickListener: ((Slice?) -> Unit)? = null
18+
19+
override fun onCreateViewHolder(
20+
parent: ViewGroup,
21+
viewType: Int
22+
): LegendAdapter.ItemViewHolder {
23+
return ItemViewHolder(
24+
LayoutInflater.from(parent.context).inflate(
25+
R.layout.item_legend,
26+
parent,
27+
false
28+
)
29+
)
30+
}
31+
32+
override fun onBindViewHolder(holder: LegendAdapter.ItemViewHolder, position: Int) {
33+
holder.bind(items[position])
34+
}
35+
36+
override fun getItemCount(): Int = items.size
37+
38+
fun setup(items: List<Slice>) {
39+
this.items.clear()
40+
this.items.addAll(items)
41+
notifyDataSetChanged()
42+
}
43+
44+
inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
45+
46+
private var boundItem: Slice? = null
47+
48+
init {
49+
itemView.setOnClickListener {
50+
onItemClickListener?.invoke(boundItem)
51+
}
52+
}
53+
54+
fun bind(slice: Slice) {
55+
this.boundItem = slice
56+
itemView.imageViewCircleIndicator.imageTintList =
57+
ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color))
58+
itemView.textViewSliceTitle.text = slice.name
59+
}
60+
}
61+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:layout_width="wrap_content"
5+
android:layout_height="wrap_content"
6+
android:layout_margin="16dp">
7+
8+
<ImageView
9+
android:id="@+id/imageViewCircleIndicator"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:layout_centerVertical="true"
13+
android:layout_marginStart="11dp"
14+
android:src="@drawable/circle_indicator" />
15+
16+
<TextView
17+
android:id="@+id/textViewSliceTitle"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:layout_centerVertical="true"
21+
android:layout_marginStart="4dp"
22+
android:layout_marginEnd="11dp"
23+
android:layout_toEndOf="@+id/imageViewCircleIndicator"
24+
android:ellipsize="end"
25+
android:maxLines="1"
26+
android:textColor="@android:color/black"
27+
android:textSize="12sp"
28+
tools:text="1648 ziyaret" />
29+
</RelativeLayout>

0 commit comments

Comments
 (0)