Vico daily digital media use
import androidx.compose.foundation.shape.CircleShape
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.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.patrykandpatrick.vico.compose.cartesian.CartesianChartHost
import com.patrykandpatrick.vico.compose.cartesian.axis.HorizontalAxis
import com.patrykandpatrick.vico.compose.cartesian.axis.VerticalAxis
import com.patrykandpatrick.vico.compose.cartesian.data.CartesianChartModelProducer
import com.patrykandpatrick.vico.compose.cartesian.data.CartesianValueFormatter
import com.patrykandpatrick.vico.compose.cartesian.data.columnModel
import com.patrykandpatrick.vico.compose.cartesian.layer.CartesianLayerPadding
import com.patrykandpatrick.vico.compose.cartesian.layer.ColumnCartesianLayer
import com.patrykandpatrick.vico.compose.cartesian.layer.rememberColumnCartesianLayer
import com.patrykandpatrick.vico.compose.cartesian.marker.CartesianMarkerController
import com.patrykandpatrick.vico.compose.cartesian.marker.DefaultCartesianMarker
import com.patrykandpatrick.vico.compose.cartesian.rememberCartesianChart
import com.patrykandpatrick.vico.compose.cartesian.rememberVicoZoomState
import com.patrykandpatrick.vico.compose.common.Fill
import com.patrykandpatrick.vico.compose.common.Insets
import com.patrykandpatrick.vico.compose.common.LegendItem
import com.patrykandpatrick.vico.compose.common.component.ShapeComponent
import com.patrykandpatrick.vico.compose.common.component.rememberLineComponent
import com.patrykandpatrick.vico.compose.common.component.rememberTextComponent
import com.patrykandpatrick.vico.compose.common.data.ExtraStore
import com.patrykandpatrick.vico.compose.common.rememberHorizontalLegend
import com.patrykandpatrick.vico.compose.common.vicoTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.text.style.TextAlign
import com.patrykandpatrick.vico.compose.cartesian.axis.rememberAxisGuidelineComponent
import com.patrykandpatrick.vico.compose.cartesian.marker.CartesianMarker
import com.patrykandpatrick.vico.compose.cartesian.marker.rememberDefaultCartesianMarker
import com.patrykandpatrick.vico.compose.common.LayeredComponent
import com.patrykandpatrick.vico.compose.common.MarkerCornerBasedShape
import com.patrykandpatrick.vico.compose.common.component.TextComponent
import com.patrykandpatrick.vico.compose.common.component.rememberShapeComponent
/*
* Copyright 2026 by Patryk Goworowski and Patrick Michalik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
private val LegendLabelKey = ExtraStore.Key<Set<String>>()
private val StartAxisValueFormatter = CartesianValueFormatter.decimal(suffix = " h")
private val StartAxisItemPlacer = VerticalAxis.ItemPlacer.step({ 0.5 })
private val MarkerValueFormatter = DefaultCartesianMarker.ValueFormatter.default(suffix = " h")
private val x = (2008..2018).toList()
private val y =
mapOf(
"Laptop/desktop" to listOf<Number>(2.2, 2.3, 2.4, 2.6, 2.5, 2.3, 2.2, 2.2, 2.2, 2.1, 2),
"Mobile" to listOf(0.3, 0.3, 0.4, 0.8, 1.6, 2.3, 2.6, 2.8, 3.1, 3.3, 3.6),
"Other" to listOf(0.2, 0.3, 0.4, 0.3, 0.3, 0.3, 0.3, 0.4, 0.4, 0.6, 0.7),
)
@Composable
private fun ComposeDailyDigitalMediaUse(
modelProducer: CartesianChartModelProducer,
modifier: Modifier = Modifier,
) {
val columnColors = listOf(Color(0xff6438a7), Color(0xff3490de), Color(0xff73e8dc))
val legendItemLabelComponent = rememberTextComponent(TextStyle(vicoTheme.textColor, 12.sp))
CartesianChartHost(
chart =
rememberCartesianChart(
rememberColumnCartesianLayer(
columnProvider =
ColumnCartesianLayer.ColumnProvider.series(
columnColors.map { color ->
rememberLineComponent(fill = Fill(color), thickness = 16.dp)
}
),
columnCollectionSpacing = 32.dp,
mergeMode = { ColumnCartesianLayer.MergeMode.Stacked },
),
startAxis =
VerticalAxis.rememberStart(
valueFormatter = StartAxisValueFormatter,
itemPlacer = StartAxisItemPlacer,
),
bottomAxis =
HorizontalAxis.rememberBottom(
itemPlacer = remember { HorizontalAxis.ItemPlacer.segmented() }
),
marker = rememberMarker(MarkerValueFormatter),
layerPadding = { CartesianLayerPadding(scalableStart = 16.dp, scalableEnd = 16.dp) },
legend =
rememberHorizontalLegend(
items = { extraStore ->
extraStore[LegendLabelKey].forEachIndexed { index, label ->
add(
LegendItem(
ShapeComponent(Fill(columnColors[index]), CircleShape),
legendItemLabelComponent,
label,
)
)
}
},
padding = Insets(top = 16.dp),
),
markerController = CartesianMarkerController.rememberToggleOnTap(),
),
modelProducer = modelProducer,
modifier = modifier,
zoomState = rememberVicoZoomState(zoomEnabled = false),
)
}
@Composable
fun ComposeDailyDigitalMediaUse(modifier: Modifier = Modifier) {
val modelProducer = remember { CartesianChartModelProducer() }
LaunchedEffect(Unit) {
modelProducer.runTransaction {
// Learn more: https://patrykandpatrick.com/3aqy4o.
columnModel { y.values.forEach { series(x, it) } }
extras { it[LegendLabelKey] = y.keys }
}
}
ComposeDailyDigitalMediaUse(modelProducer, modifier)
}
/*
* Copyright 2026 by Patryk Goworowski and Patrick Michalik.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
internal val LocalMarkersEnabled = staticCompositionLocalOf { true }
@Composable
internal fun rememberMarker(
valueFormatter: DefaultCartesianMarker.ValueFormatter =
DefaultCartesianMarker.ValueFormatter.default(),
showIndicator: Boolean = true,
): CartesianMarker? {
if (!LocalMarkersEnabled.current) return null
val labelBackgroundShape = MarkerCornerBasedShape(CircleShape)
val labelBackground =
rememberShapeComponent(
fill = Fill(MaterialTheme.colorScheme.background),
shape = labelBackgroundShape,
strokeFill = Fill(MaterialTheme.colorScheme.outline),
strokeThickness = 1.dp,
)
val label =
rememberTextComponent(
style =
TextStyle(
color = MaterialTheme.colorScheme.onSurface,
textAlign = TextAlign.Center,
fontSize = 12.sp,
),
padding = Insets(8.dp, 4.dp),
background = labelBackground,
minWidth = TextComponent.MinWidth.fixed(40.dp),
)
val indicatorFrontComponent =
rememberShapeComponent(Fill(MaterialTheme.colorScheme.surface), CircleShape)
val guideline = rememberAxisGuidelineComponent()
return rememberDefaultCartesianMarker(
label = label,
valueFormatter = valueFormatter,
indicator =
if (showIndicator) {
{ color ->
LayeredComponent(
back = ShapeComponent(Fill(color.copy(alpha = 0.15f)), CircleShape),
front =
LayeredComponent(
back = ShapeComponent(fill = Fill(color), shape = CircleShape),
front = indicatorFrontComponent,
padding = Insets(5.dp),
),
padding = Insets(10.dp),
)
}
} else {
null
},
indicatorSize = 36.dp,
guideline = guideline,
)
}
@Preview(name = "Vico daily digital media use")
@Composable
fun CommunityComposeDailyDigitalMediaUsePreview() {
ComposeDailyDigitalMediaUse()
}
build.gradle.kts
dependencies {
implementation("com.patrykandpatrick.vico:compose:3.3.1")
implementation("org.jetbrains.compose.runtime:runtime:1.12.0")
implementation("org.jetbrains.compose.foundation:foundation:1.12.0")
implementation("org.jetbrains.compose.ui:ui:1.12.0")
implementation("org.jetbrains.compose.material3:material3:1.9.0")
}