How can I draw dynamic line chart based time and data with Compose in Android Studio?

Viewed 750

The Code A generate continuous datas whom's range is 0 and 100. I hope to draw dynamic line chart based time and data.

The X axis is time (The initial range is 0 and 1 minutes, and it can be increased by time) , and Y axis is data (The range is 0 and 100).

I hope to draw dynamic line chart in the coordinate system.

It's just like Image A.

How can I do? Is there sample code ?

Code A

private fun soundDbFlow() = flow {
          while (true) {
             val data=(0..100).random().toDouble()
             emit(data)
             delay(100)
          }
}

Image A

enter image description here

1 Answers

The library for reference in the Android world regarding plotting is MPAndroidCharts.

That is lib is not "jetpack compose compliant" but jetpack compose allows to use good-old android views by using the AndroidView composable which you may be able to use with a line chart as shown in this example

AndroidView(factory = { ctx ->  
  com.github.mikephil.charting.charts.LineChart(ctx).apply {
    // set properties until it looks like you want it to look like
  }
})

Here's a link to the documentation for the MPAndroidCharts lib

Related