nuhkoca
09/14/2022, 12:14 PMnuhkoca
09/14/2022, 12:16 PMpartitions.forEach { partition ->
val sweepAngle = partition.sweepAngle
// Draw Circle
drawArc(
startAngle = startAngle,
sweepAngle = sweepAngle,
size = arcSize,
progressBrush = partition.style,
strokeWidthInPx = progressStrokeWidthInPx
)
// Draw Snippet
val snippet = partition.snippet
if (snippet != null) {
val snippetText = snippet.text
val snippetBackground = snippet.background
val textWidth = snippetText.rect.width()
val textHeight = snippetText.rect.height()
val padding = snippetBackground.paddingInPx
val angleMidpoint = startAngle + (sweepAngle / 2)
val angleMidpointInRadians = angleToRadian(angleMidpoint)
val snippetWidth = textWidth + (padding * 2)
val snippetHeight = textHeight + (padding * 2)
// Find polar coordinates of snippet center and convert them to cartesian coordinates
// A great source for learning about coordinates: <https://varun.ca/polar-coords/>
val snippetCenterXCoordinate =
centerX + (((progressStrokeWidthInPx + snippetWidth) / 2) + radius) * cos(angleMidpointInRadians)
val snippetCenterYCoordinate =
centerY + (((progressStrokeWidthInPx + snippetHeight) / 2) + radius) * sin(
angleMidpointInRadians
)
// Calculate the snippet so it is drawn based on its center points
val snippetXCoordinate = snippetCenterXCoordinate - (snippetWidth / 2)
val snippetYCoordinate = snippetCenterYCoordinate - (snippetHeight / 2)
val textXCoordinate = snippetXCoordinate + padding
val textYCoordinate = snippetYCoordinate + textHeight + padding
// Snippet background stroke
val snippetStroke = snippetBackground.stroke
if (snippetStroke != null) {
drawRoundRect(
brush = snippetStroke.style,
x = snippetXCoordinate,
y = snippetYCoordinate,
width = snippetWidth,
height = snippetHeight,
style = Stroke(width = snippetStroke.widthInPx),
cornerRadiusInPx = snippetBackground.cornerRadiusInPx
)
}
// Snippet background fill
drawRoundRect(
brush = snippetBackground.style,
x = snippetXCoordinate,
y = snippetYCoordinate,
width = snippetWidth,
height = snippetHeight,
style = Fill,
cornerRadiusInPx = snippetBackground.cornerRadiusInPx
)
drawLine(
Color.Red,
start = Offset(centerX, centerY),
end = Offset(textXCoordinate, textYCoordinate)
)
drawLine(
Color.Green,
start = Offset(centerX, centerY),
end = Offset(snippetCenterXCoordinate, snippetCenterYCoordinate)
)
// Snippet text
drawText(
x = textXCoordinate,
y = textYCoordinate,
value = snippetText.value,
style = snippetText.style
)
}
startAngle += sweepAngle
}
Oleksandr Balan
09/14/2022, 12:21 PMarcs
in one for-each and snippets in another one, so that all snippets are drawn “after” all arc
segments.nuhkoca
09/14/2022, 1:00 PMZoltan Demant
09/14/2022, 1:12 PMnuhkoca
09/14/2022, 1:26 PMZoltan Demant
09/14/2022, 1:37 PMZoltan Demant
09/14/2022, 1:48 PM