@@ -24,9 +24,13 @@ import androidx.compose.runtime.LaunchedEffect
2424import androidx.compose.runtime.mutableStateOf
2525import androidx.compose.runtime.remember
2626import androidx.compose.ui.graphics.Color
27+ import androidx.compose.ui.platform.LocalContext
28+ import androidx.compose.ui.res.stringResource
2729import androidx.compose.ui.text.TextStyle
2830import androidx.compose.ui.text.style.TextAlign
2931import androidx.compose.ui.unit.dp
32+ import com.health.openscale.R
33+ import com.health.openscale.core.data.AggregationLevel
3034import com.health.openscale.core.data.MeasurementType
3135import com.health.openscale.core.data.UnitType
3236import com.health.openscale.core.data.UserGoals
@@ -56,6 +60,7 @@ import com.patrykandpatrick.vico.compose.common.data.ExtraStore
5660import java.time.LocalDate
5761import java.time.ZoneId
5862import java.time.format.DateTimeFormatter
63+ import java.time.temporal.ChronoUnit
5964import kotlin.math.ceil
6065import kotlin.math.floor
6166
@@ -374,14 +379,30 @@ internal fun rememberMarkerVisibilityListener(
374379
375380/* *
376381 * Remembers a [CartesianValueFormatter] for the X-axis that formats epoch day floats
377- * back to human-readable date strings.
382+ * back to human-readable date strings. The date format adapts to the current [aggregationLevel]:
378383 */
379384@Composable
380- internal fun rememberXAxisValueFormatter (chartSeries : List <ChartSeries >): CartesianValueFormatter =
381- remember(chartSeries) {
385+ internal fun rememberXAxisValueFormatter (
386+ chartSeries : List <ChartSeries >,
387+ aggregationLevel : AggregationLevel = AggregationLevel .NONE ,
388+ ): CartesianValueFormatter {
389+ val weekAbbrev = stringResource(R .string.calendar_week_abbrev)
390+ return remember(chartSeries, aggregationLevel, weekAbbrev) {
382391 val xToDatesMap = chartSeries.flatMap { it.points }.associate { it.x to it.date }
392+
393+ val pattern = when (aggregationLevel) {
394+ AggregationLevel .NONE -> " d MMM"
395+ AggregationLevel .DAY -> " d MMM"
396+ AggregationLevel .WEEK -> " '${weekAbbrev} 'w MMM"
397+ AggregationLevel .MONTH -> " MMM yy"
398+ AggregationLevel .YEAR -> " yyyy"
399+ }
400+
401+ val formatter = DateTimeFormatter .ofPattern(pattern)
402+
383403 CartesianValueFormatter { _, value, _ ->
384404 (xToDatesMap[value.toFloat()] ? : LocalDate .ofEpochDay(value.toLong()))
385- .format(DateTimeFormatter .ofPattern( " d MMM " ) )
405+ .format(formatter )
386406 }
387- }
407+ }
408+ }
0 commit comments