Skip to content

Commit c3c2e0d

Browse files
author
Furkan
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
2 parents 2142be9 + 2531865 commit c3c2e0d

File tree

9 files changed

+136
-43
lines changed

9 files changed

+136
-43
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ allprojects {
1818
Step 2. Add the dependency
1919
```gradle
2020
dependencies {
21-
implementation 'com.github.furkanaskin:ClickablePieChart:1.0.7'
21+
implementation 'com.github.furkanaskin:ClickablePieChart:1.0.8'
2222
}
2323
```
2424

@@ -47,6 +47,12 @@ To setup with legend you need an root layout for legend.
4747
```kotlin
4848
chart.showLegend(legendLayout)
4949
```
50+
Or use with custom legend adapter by inheriting from [LegendAdapter](https://github.com/furkanaskin/ClickablePieChart/blob/master/lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt)
51+
```kotlin
52+
chart.showLegend(legendLayout, CustomLegendAdapter())
53+
```
54+
Sample Custom Adapter can be found [here](https://github.com/furkanaskin/ClickablePieChart/blob/master/app/src/main/java/com/faskn/clickablepiechart/CustomLegendAdapter.kt)
55+
5056
## XML Attributes
5157
<table>
5258
<thead>

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies {
3535
implementation 'androidx.core:core-ktx:1.3.1'
3636
implementation 'androidx.appcompat:appcompat:1.2.0'
3737
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
38+
implementation "androidx.recyclerview:recyclerview:1.1.0"
3839
testImplementation 'junit:junit:4.12'
3940
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
4041
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.faskn.clickablepiechart
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 com.faskn.lib.Slice
9+
import com.faskn.lib.legend.LegendAdapter
10+
import com.faskn.lib.legend.LegendItemViewHolder
11+
import kotlinx.android.synthetic.main.custom_item_legend.view.*
12+
13+
14+
class CustomLegendAdapter: LegendAdapter() {
15+
16+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomLegendItemViewHolder {
17+
return CustomLegendItemViewHolder(
18+
LayoutInflater.from(parent.context).inflate(R.layout.custom_item_legend, parent, false)
19+
)
20+
}
21+
22+
// CREATE YOUR OWN ITEM VIEW HOLDER
23+
class CustomLegendItemViewHolder(view: View) : LegendItemViewHolder(view) {
24+
override fun bind(slice: Slice) {
25+
this.boundItem = slice
26+
itemView.imageViewCircleIndicator.imageTintList =
27+
ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color))
28+
itemView.textViewSliceTitle.text = slice.name
29+
}
30+
}
31+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class MainActivity : AppCompatActivity() {
3232

3333
chart.setPieChart(pieChart)
3434
chart.showLegend(legendLayout)
35+
36+
//OR SET WITH CUSTOMER LEGEND ADAPTER
37+
chart2.setPieChart(pieChart)
38+
chart2.showLegend(legendLayout2,CustomLegendAdapter())
3539
}
3640

3741
private fun provideSlices(): ArrayList<Slice> {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="48dp" android:viewportHeight="1280"
2+
android:viewportWidth="1280" android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
3+
<path android:fillColor="#000000"
4+
android:pathData="M628,197.7c-7.9,24.7 -33.4,103.7 -56.6,175.8l-42.2,131 -7.3,0.1c-4.1,0.1 -65.4,0.7 -136.4,1.4 -71,0.6 -129.6,1.3 -130.3,1.6 -0.7,0.2 -20.5,0.4 -44,0.4 -23.5,-0 -50.2,0.3 -59.3,0.7l-16.7,0.6 126.2,93.9c69.3,51.6 140.6,104.6 158.3,117.8l32.2,23.9 -1.9,5.8c-6.2,18.5 -104.9,325.7 -104.7,325.9 0.1,0.1 13.3,-9 29.2,-20.1 16,-11.1 83.8,-58.2 150.7,-104.7l121.7,-84.6 43.3,30.7c23.8,16.9 92.1,65.3 151.8,107.5 59.7,42.3 109.2,77.5 110.1,78.2 0.8,0.8 1.8,1.4 2.1,1.4 0.2,-0 -9.1,-27.8 -20.8,-61.8 -11.8,-33.9 -23.2,-66.9 -25.3,-73.2 -63.6,-184.2 -78.1,-226.6 -77.8,-227.3 0.2,-0.8 29,-20.4 214.9,-146.4 51,-34.5 92.7,-63.2 92.8,-63.7 0,-0.5 -8.2,-0.6 -18.7,-0.2 -10.4,0.3 -91.9,0.9 -181.3,1.2l-162.5,0.6 -17.7,-48.3c-9.7,-26.6 -35.9,-98.4 -58.3,-159.4 -22.4,-61.1 -44.1,-120.6 -48.3,-132.2 -4.2,-11.7 -7.9,-21.3 -8.2,-21.3 -0.3,-0 -7.1,20.1 -15,44.7z" android:strokeColor="#00000000"/>
5+
</vector>
Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
android:weightSum="2"
79
tools:context=".MainActivity">
810

911
<LinearLayout
1012
android:layout_width="match_parent"
1113
android:layout_height="wrap_content"
12-
android:layout_centerInParent="true"
1314
android:layout_margin="24dp"
15+
android:layout_weight="1"
1416
android:orientation="horizontal">
1517

1618
<com.faskn.lib.ClickablePieChart
1719
android:id="@+id/chart"
1820
android:layout_width="match_parent"
1921
android:layout_height="match_parent"
2022
android:layout_weight="1"
23+
app:animationDuration="2000"
2124
app:centerColor="@color/white"
2225
app:popupText="Ziyaret"
2326
app:showPercentage="true"
24-
app:animationDuration="2000"
2527
app:showPopup="true" />
2628

2729
<FrameLayout
@@ -30,4 +32,32 @@
3032
android:layout_height="wrap_content"
3133
android:layout_weight="1.5" />
3234
</LinearLayout>
33-
</RelativeLayout>
35+
36+
37+
<LinearLayout
38+
android:layout_width="match_parent"
39+
android:layout_height="wrap_content"
40+
android:layout_margin="24dp"
41+
42+
android:layout_weight="1"
43+
android:orientation="horizontal">
44+
45+
<com.faskn.lib.ClickablePieChart
46+
android:id="@+id/chart2"
47+
android:layout_width="match_parent"
48+
android:layout_height="match_parent"
49+
android:layout_weight="1"
50+
app:animationDuration="2000"
51+
app:centerColor="@color/white"
52+
app:popupText="Ziyaret"
53+
app:showPercentage="true"
54+
app:showPopup="true" />
55+
56+
<FrameLayout
57+
android:id="@+id/legendLayout2"
58+
android:layout_width="match_parent"
59+
android:layout_height="wrap_content"
60+
android:layout_weight="1.5" />
61+
</LinearLayout>
62+
63+
</LinearLayout>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
7+
<ImageView
8+
android:id="@+id/imageViewCircleIndicator"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:layout_centerVertical="true"
12+
android:layout_marginStart="11dp"
13+
android:src="@drawable/ic_star" />
14+
15+
<TextView
16+
android:id="@+id/textViewSliceTitle"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:layout_centerVertical="true"
20+
android:layout_marginStart="4dp"
21+
android:layout_marginEnd="11dp"
22+
android:layout_toEndOf="@+id/imageViewCircleIndicator"
23+
android:ellipsize="end"
24+
android:maxLines="1"
25+
android:textColor="@android:color/black"
26+
android:textSize="20sp"
27+
tools:text="1648 ziyaret" />
28+
</RelativeLayout>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,13 @@ class ClickablePieChart @JvmOverloads constructor(
297297
popupWindow?.dismiss()
298298
}
299299

300-
fun showLegend(rootLayout: ViewGroup) {
300+
fun showLegend(rootLayout: ViewGroup, adapter: LegendAdapter = LegendAdapter()) {
301301
val recyclerView = RecyclerView(context)
302-
val linearLayoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
302+
val linearLayoutManager =
303+
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
303304
recyclerView.layoutManager = linearLayoutManager
304-
val adapter = LegendAdapter()
305-
slices?.toMutableList()?.let { adapter.setup(it) }
306305
recyclerView.adapter = adapter
306+
slices?.toMutableList()?.let { adapter.setup(it) }
307307
recyclerView.overScrollMode = OVER_SCROLL_NEVER
308308
rootLayout.addView(recyclerView)
309309
invalidateAndRequestLayout()

lib/src/main/java/com/faskn/lib/legend/LegendAdapter.kt

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,40 @@ import com.faskn.lib.R
1010
import com.faskn.lib.Slice
1111
import kotlinx.android.synthetic.main.item_legend.view.*
1212

13-
class LegendAdapter : RecyclerView.Adapter<LegendAdapter.ItemViewHolder>() {
14-
15-
private val items = mutableListOf<Slice>()
13+
open class LegendAdapter : RecyclerView.Adapter<LegendItemViewHolder>() {
1614

15+
protected val items = mutableListOf<Slice>()
1716
var onItemClickListener: ((Slice?) -> Unit)? = null
1817

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-
3818
fun setup(items: List<Slice>) {
3919
this.items.clear()
4020
this.items.addAll(items)
4121
notifyDataSetChanged()
4222
}
4323

44-
inner class ItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
24+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LegendItemViewHolder {
25+
return LegendItemViewHolder(
26+
LayoutInflater.from(parent.context).inflate(R.layout.item_legend, parent, false)
27+
)
28+
}
4529

46-
private var boundItem: Slice? = null
30+
override fun getItemCount(): Int = items.size
4731

48-
init {
49-
itemView.setOnClickListener {
50-
onItemClickListener?.invoke(boundItem)
51-
}
52-
}
32+
override fun onBindViewHolder(holder: LegendItemViewHolder, position: Int) {
33+
holder.bind(items[position])
5334

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
35+
holder.itemView.setOnClickListener {
36+
onItemClickListener?.invoke(items[position])
5937
}
6038
}
39+
}
40+
41+
open class LegendItemViewHolder(view: View) : RecyclerView.ViewHolder(view) {
42+
var boundItem: Slice? = null
43+
open fun bind(slice: Slice) {
44+
boundItem = slice
45+
itemView.imageViewCircleIndicator.imageTintList =
46+
ColorStateList.valueOf(ContextCompat.getColor(itemView.context, slice.color))
47+
itemView.textViewSliceTitle.text = slice.name
48+
}
6149
}

0 commit comments

Comments
 (0)