Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@file:Suppress("MagicNumber", "LongMethod", "ktlint")
@file:Suppress("MagicNumber", "LongMethod", "ktlint", "CyclomaticComplexMethod")

package ir.composenews.designsystem.component

Expand Down Expand Up @@ -36,7 +36,6 @@ import ir.composenews.designsystem.theme.ComposeNewsTheme
import ir.composenews.designsystem.theme.graphColor
import ir.composenews.designsystem.theme.lightGraphColor
import kotlin.math.round
import kotlin.math.roundToInt

@Composable
fun QuadLineChart(
Expand All @@ -48,10 +47,10 @@ fun QuadLineChart(
val columnTextColor = MaterialTheme.colorScheme.onSurface.toArgb()

val upperValue = remember(key1 = data) {
(data.maxOfOrNull { it.second }?.let { it + it * 0.05 })?.roundToInt() ?: 0
(data.maxOfOrNull { it.second }?.let { it + it * 0.05 }) ?: 0.0
}
val lowerValue = remember(key1 = data) {
(data.minOfOrNull { it.second }?.let { it - it * 0.02 }?.toInt() ?: 0)
(data.minOfOrNull { it.second }?.let { it - it * 0.02 }) ?: 0.0
}
val density = LocalDensity.current

Expand Down Expand Up @@ -84,12 +83,18 @@ fun QuadLineChart(
.height(dimensionResource(R.dimen.quad_line_chart_height)),
) {
val spacePerHour = (size.width - spacing) / data.size
val priceStep = (upperValue - lowerValue) / 5f
val priceStep = (upperValue - lowerValue) / 5.0

repeat(5) { i ->
drawContext.canvas.nativeCanvas.apply {
val labelValue = lowerValue + priceStep * i
val label = if (labelValue >= 1.0) {
round(labelValue).toLong().toString()
} else {
"%.6f".format(labelValue).trimEnd('0').trimEnd('.')
}
drawText(
round(lowerValue + priceStep * i).toString(),
label,
textXDimen,
size.height - spacing - i * size.height / 5f,
textPaint,
Expand All @@ -99,12 +104,13 @@ fun QuadLineChart(

var medX: Float
var medY: Float
val range = upperValue - lowerValue
val strokePath = Path().apply {
val height = size.height
data.indices.forEach { i ->
val nextInfo = data.getOrNull(i + 1) ?: data.last()
val firstRatio = (data[i].second - lowerValue) / (upperValue - lowerValue)
val secondRatio = (nextInfo.second - lowerValue) / (upperValue - lowerValue)
val firstRatio = if (range == 0.0) 0.5 else (data[i].second - lowerValue) / range
val secondRatio = if (range == 0.0) 0.5 else (nextInfo.second - lowerValue) / range

val x1 = spacing + i * spacePerHour
val y1 = height - spacing - (firstRatio * height).toFloat()
Expand Down
Loading