LiveData Chart #841
-
QuestionI want to implement a linechart showing live data. I get values from a bluetooth device (aprox 10Hz) and create a mutableList of Pairs (value, timestamp). I then use this list in the chart component:
This is not working. I tried to limit the size of the list to even just 10 elements, but didn't help. What I am doing wrong? Thank you! Vico version(s)1.15.0 UI framework(s)Jetpack Compose |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 18 replies
-
Hi @DSanjulian, there are two issues in your code:
Here's a snippet with the required fixes. @Composable
fun LineChartComponent(points: List<Pair<Float, Float>>) {
val chartEntryModelProducer = remember { ChartEntryModelProducer() }
LaunchedEffect(points) {
chartEntryModelProducer.setEntries(points.map { entryOf(it.first, it.second) })
}
Chart(
chart = lineChart(),
chartModelProducer = chartEntryModelProducer,
startAxis = rememberStartAxis(guideline = null),
bottomAxis = rememberBottomAxis(guideline = null),
chartScrollSpec = rememberChartScrollSpec(isScrollEnabled = false),
runInitialAnimation = false,
diffAnimationSpec = null,
)
} |
Beta Was this translation helpful? Give feedback.
-
Hi Patrick, thanks a lot for the answer!
As you see, even if the list of points keeps growing, when I check the entries of the model there's just one. |
Beta Was this translation helpful? Give feedback.
-
That was it, thank you very much @Gowsky! |
Beta Was this translation helpful? Give feedback.
First, there are some issues with your usage of Vico and
Flow
. The main problem however was caused by a growing list of x axis labels. Your axis settings added a label for every entry. The number of entries was very large and drawing hundreds (or more) labels slowed down the main thread. Ultimately, such settings don't make sense in this case as all the labels would get truncated. Here's an updated version ofLiveData.kt
(for Vico 2) with issues fixed and suggested bottom axis settings. This should perform as expected.