@@ -5,6 +5,7 @@ import androidx.compose.ui.graphics.Color
5
5
import androidx.compose.ui.graphics.Color.Companion.Gray
6
6
import androidx.compose.ui.graphics.toArgb
7
7
import androidx.compose.ui.res.stringResource
8
+ import br.com.colman.petals.R
8
9
import br.com.colman.petals.R.string.grams_distribution_over_days_since_first_use
9
10
import br.com.colman.petals.statistics.graph.formatter.GramsValueFormatter
10
11
import br.com.colman.petals.use.repository.Use
@@ -14,25 +15,41 @@ import java.math.BigDecimal
14
15
import java.time.LocalDate.now
15
16
16
17
@Composable
17
- fun createAllTimeDistribution (uses : List <Use >): LineDataSet {
18
- val label = stringResource(grams_distribution_over_days_since_first_use)
19
- return createAllTimeLineDataSet(calculateAllTimeGramsDistribution(uses), label)
18
+ fun createAllTimeDistributionWithMovingAverage (uses : List <Use >): List <LineDataSet > {
19
+ val gramsLabel = stringResource(grams_distribution_over_days_since_first_use)
20
+ val gramsDataSet = createAllTimeLineDataSet(calculateAllTimeGramsDistribution(uses), gramsLabel)
21
+
22
+ val movingAverageLabel = stringResource(R .string.moving_average_grams_distribution)
23
+ val movingAverageDataSet = createAllTimeAverageDataSet(
24
+ calculateMovingAverage(calculateAllTimeGramsDistribution(uses)),
25
+ movingAverageLabel
26
+ )
27
+
28
+ return listOf (gramsDataSet, movingAverageDataSet)
20
29
}
21
30
22
31
fun createAllTimeLineDataSet (entryList : List <Entry >, label : String ): LineDataSet {
23
32
return LineDataSet (entryList, label).apply {
24
33
valueFormatter = GramsValueFormatter
25
34
lineWidth = 6f
26
- setDrawCircles(true )
27
35
setDrawFilled(true )
28
- setDrawValues( true )
36
+ setDrawCircles( false )
29
37
fillColor = Color .Cyan .copy(alpha = 0.3f ).toArgb()
30
38
color = Color .Cyan .toArgb()
31
39
valueTextColor = Gray .toArgb()
32
40
valueTextSize = 14f
33
41
}
34
42
}
35
43
44
+ fun createAllTimeAverageDataSet (entryList : List <Entry >, label : String ): LineDataSet {
45
+ return LineDataSet (entryList, label).apply {
46
+ setDrawValues(false )
47
+ setDrawCircles(false )
48
+ lineWidth = 6f
49
+ color = Color .Green .toArgb()
50
+ }
51
+ }
52
+
36
53
private fun calculateAllTimeGramsDistribution (uses : List <Use >): List <Entry > {
37
54
if (uses.isEmpty()) return emptyList()
38
55
@@ -46,3 +63,18 @@ private fun calculateAllTimeGramsDistribution(uses: List<Use>): List<Entry> {
46
63
Entry ((day - firstUseDay).toFloat(), totalGrams.toFloat())
47
64
}
48
65
}
66
+
67
+ private fun calculateMovingAverage (entries : List <Entry >): List <Entry > {
68
+ if (entries.isEmpty()) return emptyList()
69
+
70
+ val result = mutableListOf<Entry >()
71
+ for (i in entries.indices) {
72
+ val start = maxOf(0 , i - 14 + 1 )
73
+ val end = i + 1
74
+ val windowEntries = entries.subList(start, end)
75
+
76
+ val averageValue = windowEntries.map { it.y }.average().toFloat()
77
+ result.add(Entry (entries[i].x, averageValue))
78
+ }
79
+ return result
80
+ }
0 commit comments