-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.jetchart.pie | ||
|
||
import androidx.compose.animation.core.Animatable | ||
import androidx.compose.animation.core.AnimationSpec | ||
import androidx.compose.foundation.Canvas | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas | ||
import io.jetchart.common.animation.fadeInAnimation | ||
import io.jetchart.pie.renderer.FilledSliceDrawer | ||
import io.jetchart.pie.renderer.SliceDrawer | ||
|
||
@Composable | ||
fun PieChart( | ||
pies: Pies, | ||
modifier: Modifier = Modifier, | ||
animation: AnimationSpec<Float> = fadeInAnimation(), | ||
sliceDrawer: SliceDrawer = FilledSliceDrawer() | ||
) { | ||
val transitionProgress = remember(pies.slices) { Animatable(initialValue = 0f) } | ||
|
||
LaunchedEffect(pies.slices) { | ||
transitionProgress.animateTo(1f, animationSpec = animation) | ||
} | ||
|
||
DrawChart( | ||
pies = pies, | ||
modifier = modifier.fillMaxSize(), | ||
progress = transitionProgress.value, | ||
sliceDrawer = sliceDrawer | ||
) | ||
} | ||
|
||
@Composable | ||
private fun DrawChart( | ||
pies: Pies, | ||
modifier: Modifier, | ||
progress: Float, | ||
sliceDrawer: SliceDrawer | ||
) { | ||
val slices = pies.slices | ||
|
||
Canvas(modifier = modifier) { | ||
drawIntoCanvas { | ||
var startArc = 0f | ||
|
||
slices.forEach { slice -> | ||
val arc = angleOf( | ||
value = slice.value, | ||
totalValue = pies.totalSize(), | ||
progress = progress | ||
) | ||
|
||
sliceDrawer.drawSlice( | ||
drawScope = this, | ||
canvas = drawContext.canvas, | ||
area = size, | ||
startAngle = startArc, | ||
sweepAngle = arc, | ||
slice = slice | ||
) | ||
|
||
startArc += arc | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun angleOf(value: Float, totalValue: Float, progress: Float): Float { | ||
return 360f * (value * progress) / totalValue | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.jetchart.pie | ||
|
||
data class Pies(val slices: List<Slice>) { | ||
fun totalSize(): Float = slices.map { it.value }.sum() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package io.jetchart.pie | ||
|
||
import androidx.compose.ui.graphics.Color | ||
|
||
data class Slice(val value: Float, val color: Color) |
59 changes: 59 additions & 0 deletions
59
JetChart/src/main/java/io/jetchart/pie/renderer/FilledSliceDrawer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.jetchart.pie.renderer | ||
|
||
import androidx.compose.ui.geometry.Rect | ||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Canvas | ||
import androidx.compose.ui.graphics.Paint | ||
import androidx.compose.ui.graphics.PaintingStyle | ||
import androidx.compose.ui.graphics.drawscope.DrawScope | ||
import io.jetchart.pie.Slice | ||
|
||
class FilledSliceDrawer(private val thickness: Float = 30f) : SliceDrawer { | ||
|
||
private val sectionPaint = Paint().apply { | ||
isAntiAlias = true | ||
style = PaintingStyle.Stroke | ||
} | ||
|
||
override fun drawSlice( | ||
drawScope: DrawScope, | ||
canvas: Canvas, | ||
area: Size, | ||
startAngle: Float, | ||
sweepAngle: Float, | ||
slice: Slice | ||
) { | ||
val sliceThickness = calculateSectorThickness(area = area) | ||
val drawableArea = calculateDrawableArea(area = area) | ||
|
||
canvas.drawArc( | ||
rect = drawableArea, | ||
paint = sectionPaint.apply { | ||
color = slice.color | ||
strokeWidth = sliceThickness | ||
}, | ||
startAngle = startAngle, | ||
sweepAngle = sweepAngle, | ||
useCenter = false | ||
) | ||
} | ||
|
||
private fun calculateSectorThickness(area: Size): Float { | ||
val minSize = minOf(area.width, area.height) | ||
|
||
return minSize * (thickness / 200f) | ||
} | ||
|
||
private fun calculateDrawableArea(area: Size): Rect { | ||
val sliceThicknessOffset = | ||
calculateSectorThickness(area = area) / 2f | ||
val offsetHorizontally = (area.width - area.height) / 2f | ||
|
||
return Rect( | ||
left = sliceThicknessOffset + offsetHorizontally, | ||
top = sliceThicknessOffset, | ||
right = area.width - sliceThicknessOffset - offsetHorizontally, | ||
bottom = area.height - sliceThicknessOffset | ||
) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
JetChart/src/main/java/io/jetchart/pie/renderer/SliceDrawer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.jetchart.pie.renderer | ||
|
||
import androidx.compose.ui.geometry.Size | ||
import androidx.compose.ui.graphics.Canvas | ||
import androidx.compose.ui.graphics.drawscope.DrawScope | ||
import io.jetchart.pie.Slice | ||
|
||
interface SliceDrawer { | ||
fun drawSlice( | ||
drawScope: DrawScope, | ||
canvas: Canvas, | ||
area: Size, | ||
startAngle: Float, | ||
sweepAngle: Float, | ||
slice: Slice | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.