Skip to content

Commit

Permalink
Add an attribute for changing icon color of all slices
Browse files Browse the repository at this point in the history
  • Loading branch information
mahozad committed Jun 25, 2021
1 parent c71b67f commit 891f77b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt
Expand Up @@ -38,6 +38,8 @@ const val DEFAULT_OUTSIDE_LABELS_MARGIN = 28f /* dp */
const val DEFAULT_CENTER_LABEL = ""
const val DEFAULT_SHOULD_CENTER_PIE = true
@ColorInt const val DEFAULT_LABELS_COLOR = Color.WHITE
// If null, the colors of the icon itself is used
@ColorInt val defaultLabelIconsTint: Int? = null
val defaultGapPosition = MIDDLE
val defaultGradientType = RADIAL
val defaultDrawDirection = CLOCKWISE
Expand Down Expand Up @@ -220,6 +222,14 @@ class PieChart(context: Context, attrs: AttributeSet) : View(context, attrs) {
field = color
invalidate()
}
/**
* Is overridden by color of the slice if it is assigned a value other than *null*
*/
var labelIconsTint = defaultLabelIconsTint
set(color) {
field = color
invalidate()
}
var slicesPointer = defaultSlicesPointer
set(pointer) {
field = pointer
Expand Down Expand Up @@ -285,6 +295,8 @@ class PieChart(context: Context, attrs: AttributeSet) : View(context, attrs) {
labelsSize = it.getDimension(R.styleable.PieChart_labelsSize, spToPx(DEFAULT_LABELS_SIZE))
labelOffset = it.getFloat(R.styleable.PieChart_labelOffset, DEFAULT_LABEL_OFFSET)
labelsColor = it.getColor(R.styleable.PieChart_labelsColor, DEFAULT_LABELS_COLOR)
val iconTint = it.getColor(R.styleable.PieChart_labelIconsTint, /* no value or @null */-1)
if (iconTint == -1) labelIconsTint = null
val fontId = it.getResourceId(R.styleable.PieChart_labelsFont, -1)
labelsFont = if (fontId == -1) defaultLabelsFont else ResourcesCompat.getFont(context, fontId)!!
labelIconsHeight = it.getDimension(R.styleable.PieChart_labelIconsHeight, spToPx(DEFAULT_LABEL_ICONS_HEIGHT))
Expand Down
2 changes: 2 additions & 0 deletions piechart/src/main/res/values/attrs.xml
Expand Up @@ -62,6 +62,8 @@
<attr name="labelIconsMargin" format="dimension"/>
<attr name="labelOffset" format="float"/>
<attr name="labelsColor" format="color"/>
<!-- The value can be @null too -->
<attr name="labelIconsTint" format="color"/>
<attr name="outsideLabelsMargin" format="dimension"/>
<attr name="labelIconsPlacement" format="enum">
<!-- Positions the icon at the start.
Expand Down
1 change: 1 addition & 0 deletions piechart/src/main/res/values/public.xml
Expand Up @@ -17,6 +17,7 @@
<public name="labelIconsMargin" format="dimension"/>
<public name="labelOffset" format="float"/>
<public name="labelsColor" format="color"/>
<public name="labelIconsTint" format="color"/>
<public name="outsideLabelsMargin" format="dimension"/>
<public name="labelType" format="enum"/>
<public name="holeRatio" format="float"/>
Expand Down

0 comments on commit 891f77b

Please sign in to comment.