Skip to content

Commit 891f77b

Browse files
committed
Add an attribute for changing icon color of all slices
1 parent c71b67f commit 891f77b

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const val DEFAULT_OUTSIDE_LABELS_MARGIN = 28f /* dp */
3838
const val DEFAULT_CENTER_LABEL = ""
3939
const val DEFAULT_SHOULD_CENTER_PIE = true
4040
@ColorInt const val DEFAULT_LABELS_COLOR = Color.WHITE
41+
// If null, the colors of the icon itself is used
42+
@ColorInt val defaultLabelIconsTint: Int? = null
4143
val defaultGapPosition = MIDDLE
4244
val defaultGradientType = RADIAL
4345
val defaultDrawDirection = CLOCKWISE
@@ -220,6 +222,14 @@ class PieChart(context: Context, attrs: AttributeSet) : View(context, attrs) {
220222
field = color
221223
invalidate()
222224
}
225+
/**
226+
* Is overridden by color of the slice if it is assigned a value other than *null*
227+
*/
228+
var labelIconsTint = defaultLabelIconsTint
229+
set(color) {
230+
field = color
231+
invalidate()
232+
}
223233
var slicesPointer = defaultSlicesPointer
224234
set(pointer) {
225235
field = pointer
@@ -285,6 +295,8 @@ class PieChart(context: Context, attrs: AttributeSet) : View(context, attrs) {
285295
labelsSize = it.getDimension(R.styleable.PieChart_labelsSize, spToPx(DEFAULT_LABELS_SIZE))
286296
labelOffset = it.getFloat(R.styleable.PieChart_labelOffset, DEFAULT_LABEL_OFFSET)
287297
labelsColor = it.getColor(R.styleable.PieChart_labelsColor, DEFAULT_LABELS_COLOR)
298+
val iconTint = it.getColor(R.styleable.PieChart_labelIconsTint, /* no value or @null */-1)
299+
if (iconTint == -1) labelIconsTint = null
288300
val fontId = it.getResourceId(R.styleable.PieChart_labelsFont, -1)
289301
labelsFont = if (fontId == -1) defaultLabelsFont else ResourcesCompat.getFont(context, fontId)!!
290302
labelIconsHeight = it.getDimension(R.styleable.PieChart_labelIconsHeight, spToPx(DEFAULT_LABEL_ICONS_HEIGHT))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
<attr name="labelIconsMargin" format="dimension"/>
6363
<attr name="labelOffset" format="float"/>
6464
<attr name="labelsColor" format="color"/>
65+
<!-- The value can be @null too -->
66+
<attr name="labelIconsTint" format="color"/>
6567
<attr name="outsideLabelsMargin" format="dimension"/>
6668
<attr name="labelIconsPlacement" format="enum">
6769
<!-- Positions the icon at the start.

piechart/src/main/res/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<public name="labelIconsMargin" format="dimension"/>
1818
<public name="labelOffset" format="float"/>
1919
<public name="labelsColor" format="color"/>
20+
<public name="labelIconsTint" format="color"/>
2021
<public name="outsideLabelsMargin" format="dimension"/>
2122
<public name="labelType" format="enum"/>
2223
<public name="holeRatio" format="float"/>

0 commit comments

Comments
 (0)