0

I'm trying to run this MPAndroidChart PieChart example, but getting the error:

Unresolved reference: ViewGroup

LayOutParams() shows Viewgroup as accepted input, but won't accept it. Am I right in understanding that the sizes a LinearLayout to the Column?

 Column(
            modifier = Modifier
                .padding(18.dp)
                .size(320.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            
            Crossfade(targetState = getPieChartData) { pieChartData ->
               
                AndroidView(factory = { context ->
                   
                    PieChart(context).apply {
                        layoutParams = LinearLayout.LayoutParams(
                        // throws error, does not accept this>>
                        ViewGroup.LayoutParams.MATCH_PARENT
                        )

enter image description here

nextural
  • 51
  • 5

1 Answers1

1

There is minor mistake in your code! Just need to use ViewGroup layout params instead of LinearLayout params.

Your AndroidView should look something like this:

AndroidView(factory = {
  PieChart(it).apply {
     layoutParams = ViewGroup.LayoutParams(
         ViewGroup.LayoutParams.MATCH_PARENT,
         ViewGroup.LayoutParams.WRAP_CONTENT
     )
  }
})

Also, i would recommend you to use Vico library for charts in Jetpack compose. Its light-weight version of MPAndroidChart for Jetpack Compose.

Vico library: https://github.com/patrykandpatrick/vico

Megh
  • 831
  • 2
  • 12
  • One of the reasons I strayed to MP Android was my need for a data set to 'draw' an envelope, all points being connected as in this example (https://i0.wp.com/www.aneclecticmind.com/wp-content/uploads/2009/03/wb2.jpg?resize=584%2C338&ssl=1). Do you know of limitations in Vico data handling or drawing that would allow non-sequential x-value connection? E.g. Last value = first value to complete the shape. – nextural Jul 13 '23 at 10:32