@@ -34,7 +34,7 @@ fun renderLineChartBitmap(
3434 x to y
3535 }
3636
37- val linePath = buildSmoothPath(points)
37+ val linePath = buildSmoothPath(points, yMin = padding, yMax = padding + drawHeight )
3838
3939 val linePaint = Paint (Paint .ANTI_ALIAS_FLAG ).apply {
4040 color = lineColor
@@ -48,7 +48,11 @@ fun renderLineChartBitmap(
4848 return bitmap
4949}
5050
51- private fun buildSmoothPath (points : List <Pair <Float , Float >>): Path = Path ().apply {
51+ private fun buildSmoothPath (
52+ points : List <Pair <Float , Float >>,
53+ yMin : Float ,
54+ yMax : Float ,
55+ ): Path = Path ().apply {
5256 moveTo(points[0 ].first, points[0 ].second)
5357 for (i in 0 until points.size - 1 ) {
5458 val p0 = points[(i - 1 ).coerceAtLeast(0 )]
@@ -57,9 +61,9 @@ private fun buildSmoothPath(points: List<Pair<Float, Float>>): Path = Path().app
5761 val p3 = points[(i + 2 ).coerceAtMost(points.lastIndex)]
5862
5963 val cp1x = p1.first + (p2.first - p0.first) * SMOOTHING
60- val cp1y = p1.second + (p2.second - p0.second) * SMOOTHING
64+ val cp1y = ( p1.second + (p2.second - p0.second) * SMOOTHING ).coerceIn(yMin, yMax)
6165 val cp2x = p2.first - (p3.first - p1.first) * SMOOTHING
62- val cp2y = p2.second - (p3.second - p1.second) * SMOOTHING
66+ val cp2y = ( p2.second - (p3.second - p1.second) * SMOOTHING ).coerceIn(yMin, yMax)
6367
6468 cubicTo(cp1x, cp1y, cp2x, cp2y, p2.first, p2.second)
6569 }
0 commit comments