Skip to content

Commit

Permalink
Add label offset property to Slice class
Browse files Browse the repository at this point in the history
  • Loading branch information
mahozad committed Aug 10, 2021
1 parent b953046 commit a8755a6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ class PieChartTest {
pieChart.startAngleResource = resourceId
assertThat(pieChart.startAngle).isEqualTo(expected)
}

@Test fun changeChartLabelOffsetResourceShouldChangeLabelOffsetAsWell() {
val resourceId = ir.mahozad.android.test.R.fraction.testLabelsOffset
val expected = resources.getFraction(resourceId, 1, 1)
pieChart.labelsOffsetResource = resourceId
assertThat(pieChart.labelsOffset).isEqualTo(expected)
}
}
1 change: 1 addition & 0 deletions piechart/src/androidTest/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<integer name="testStartAngle">88</integer>
<fraction name="testLabelsOffset">37%</fraction>
</resources>
16 changes: 12 additions & 4 deletions piechart/src/main/kotlin/ir/mahozad/android/PieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const val DEFAULT_LEGENDS_PERCENTAGE_STATUS = DISABLED
@Dimension(unit = SP) const val DEFAULT_LABEL_ICONS_HEIGHT = DEFAULT_LABELS_SIZE
@Dimension(unit = SP) const val DEFAULT_LEGEND_ICONS_HEIGHT = DEFAULT_LEGENDS_SIZE
@Dimension(unit = DP) const val DEFAULT_LABEL_ICONS_MARGIN = 8f
const val DEFAULT_LABEL_OFFSET = 0.75f
@FloatRange(from = 0.0, to = 1.0) const val DEFAULT_LABELS_OFFSET = 0.75f
@Dimension(unit = DP) const val DEFAULT_OUTSIDE_LABELS_MARGIN = 28f
const val DEFAULT_CENTER_LABEL = ""
@Dimension(unit = SP) const val DEFAULT_CENTER_LABEL_SIZE = 16f
Expand Down Expand Up @@ -159,6 +159,7 @@ class PieChart @JvmOverloads constructor(
@Dimension val labelIconHeight: Float? = null,
@Dimension val labelIconMargin: Float? = null,
@ColorInt val labelIconTint: Int? = null,
@FloatRange(from = 0.0, to = 1.0) val labelOffset: Float? = null,
val labelIconPlacement: IconPlacement? = null,

/**
Expand Down Expand Up @@ -590,11 +591,18 @@ class PieChart @JvmOverloads constructor(
field = alpha
invalidate()
}
var labelOffset = DEFAULT_LABEL_OFFSET
@FloatRange(from = 0.0, to = 1.0)
var labelsOffset = DEFAULT_LABELS_OFFSET
set(offset) {
field = offset.coerceIn(0f, 1f)
invalidate()
}
@FractionRes
var labelsOffsetResource = 0
set(resourceId) {
field = resourceId
labelsOffset = resources.getFraction(resourceId, 1, 1)
}
var labelIconsHeight = spToPx(DEFAULT_LABEL_ICONS_HEIGHT)
set(height /* px */) {
field = height
Expand Down Expand Up @@ -720,7 +728,7 @@ class PieChart @JvmOverloads constructor(
overlayAlpha = getFloat(R.styleable.PieChart_overlayAlpha, DEFAULT_OVERLAY_ALPHA)
gap = getDimension(R.styleable.PieChart_gap, DEFAULT_GAP)
labelsSize = getDimension(R.styleable.PieChart_labelsSize, spToPx(DEFAULT_LABELS_SIZE))
labelOffset = getFloat(R.styleable.PieChart_labelOffset, DEFAULT_LABEL_OFFSET)
labelsOffset = getFloat(R.styleable.PieChart_labelsOffset, DEFAULT_LABELS_OFFSET)
labelsColor = getColor(R.styleable.PieChart_labelsColor, DEFAULT_LABELS_COLOR)
labelIconsTint = getIconTint(this, R.styleable.PieChart_labelIconsTint)
labelsFont = getFont(this, R.styleable.PieChart_labelsFont, defaultLabelsFont)
Expand Down Expand Up @@ -848,7 +856,7 @@ class PieChart @JvmOverloads constructor(

val legendBox = LegendBuilder().createLegendBox(context, maxAvailableWidthForLegendBox, maxAvailableHeightForLegendBox, slices,legendIconsTintArray, legendsTitle, legendsTitleSize, legendsTitleColor, legendTitleMargin, legendsTitleAlignment, legendsIcon, legendIconsHeight, legendIconsAlpha, legendsSize, legendsColor, legendIconsMargin, legendsPercentageMargin, isLegendsPercentageEnabled, legendsPercentageSize, legendsPercentageColor, legendsMargin, legendArrangement, legendsAlignment, legendBoxBackgroundColor, legendBoxPadding, legendBoxBorder, legendBoxBorderColor, legendBoxBorderAlpha, legendBoxBorderCornerRadius, legendBoxBorderType, legendBoxBorderDashArray, legendBoxMargin, legendPosition, legendLinesMargin, legendsWrapping, isLegendBoxBorderEnabled)
val (pieWidth, pieHeight) = calculatePieDimensions(width, height, Paddings(paddingTop, paddingBottom, paddingStart, paddingEnd), isLegendEnabled, legendBoxMargin, legendPosition, legendBox.width, legendBox.height)
pie = Pie(context, pieWidth, pieHeight, null, null, startAngle, slices, labelType, outsideLabelsMargin, labelsSize, labelsColor, labelsFont, labelIconsHeight, labelIconsMargin, labelIconsPlacement, labelIconsTint, labelOffset, shouldCenterPie, drawDirection, overlayRatio, overlayAlpha, gradientType, holeRatio, slicesPointer, gap, gapPosition)
pie = Pie(context, pieWidth, pieHeight, null, null, startAngle, slices, labelType, outsideLabelsMargin, labelsSize, labelsColor, labelsFont, labelIconsHeight, labelIconsMargin, labelIconsPlacement, labelIconsTint, labelsOffset, shouldCenterPie, drawDirection, overlayRatio, overlayAlpha, gradientType, holeRatio, slicesPointer, gap, gapPosition)
val chartDirection = determineChartDirection(legendPosition)
val chartComponents = makeChartComponentList(pie, isLegendEnabled, legendBox, legendPosition)
chartBox = Container(chartComponents, width.toFloat(), height.toFloat(), chartDirection, legendBoxAlignment, paddings = Paddings(paddingTop, paddingBottom, paddingStart, paddingEnd))
Expand Down
6 changes: 3 additions & 3 deletions piechart/src/main/kotlin/ir/mahozad/android/component/Pie.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class Pie(
val labelIconsMargin: Float,
val labelIconsPlacement: PieChart.IconPlacement,
val labelIconsTint: Int?,
val labelOffset: Float,
val labelsOffset: Float,
val shouldCenterPie: Boolean,
val pieDrawDirection: PieChart.DrawDirection,
var overlayRatio: Float,
Expand Down Expand Up @@ -238,11 +238,11 @@ internal class Pie(
slice.labelIconTint?.let { tint -> labelIcon?.setTint(tint) }
}
val iconPlacement = slice.labelIconPlacement ?: labelIconsPlacement
val labelOffset = slice.labelOffset ?: labelsOffset
val iconMargin = slice.labelIconMargin ?: labelIconsMargin
val iconHeight = slice.labelIconHeight ?: labelIconsHeight
val labelOffset = /* TODO: add slice.LabelOffset ?:*/ labelOffset
val labelBounds = calculateLabelBounds(slice.label, mainPaint)
val iconBounds = calculateIconBounds(labelIcon, iconHeight)
val labelBounds = calculateLabelBounds(slice.label, mainPaint)
val labelAndIconCombinedBounds = calculateLabelAndIconCombinedBounds(labelBounds, iconBounds, iconMargin, iconPlacement)
val absoluteCombinedBounds = calculateAbsoluteBoundsForInsideLabelAndIcon(labelAndIconCombinedBounds, middleAngle, center, radius, labelOffset)
val iconAbsoluteBounds = calculateBoundsForOutsideLabelIcon(absoluteCombinedBounds, iconBounds, iconPlacement)
Expand Down
2 changes: 1 addition & 1 deletion piechart/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<attr name="labelsFont" format="reference"/>
<attr name="labelIconsHeight" format="dimension"/>
<attr name="labelIconsMargin" format="dimension"/>
<attr name="labelOffset" format="float"/>
<attr name="labelsOffset" format="float"/>
<attr name="labelsColor" format="color"/>
<!-- The value can be @null too -->
<attr name="labelIconsTint" format="color"/>
Expand Down
2 changes: 1 addition & 1 deletion piechart/src/main/res/values/public.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<public name="labelIconsPlacement" format="enum"/>
<public name="labelIconsHeight" format="dimension"/>
<public name="labelIconsMargin" format="dimension"/>
<public name="labelOffset" format="float"/>
<public name="labelsOffset" format="float"/>
<public name="labelsColor" format="color"/>
<public name="labelIconsTint" format="color"/>
<public name="outsideLabelsMargin" format="dimension"/>
Expand Down

0 comments on commit a8755a6

Please sign in to comment.