android-jetpack-composempandroidchartandroid-viewgroup

How to resolve reference: ViewGroup in MPAndroidChart


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


Solution

  • 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