Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transparent Background not working #154

Open
ErickSorto opened this issue Jan 3, 2024 · 2 comments
Open

Transparent Background not working #154

ErickSorto opened this issue Jan 3, 2024 · 2 comments

Comments

@ErickSorto
Copy link

ErickSorto commented Jan 3, 2024

Adding Color.Transparent to

val donutChartConfig = PieChartConfig(
            backgroundColor = Color.Transparent, //transparent color
            strokeWidth = 120f,
            activeSliceAlpha = .9f,
            isAnimationEnable = true,
            labelVisible = true,
            labelColor = Color.Black,
            isEllipsizeEnabled = true,
            labelFontSize = 16.sp, 
            chartPadding = 16,
        )

        Column(
            modifier = Modifier
                .fillMaxWidth().padding(horizontal = 12.dp, vertical = 8.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            DonutPieChart(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.Transparent),
                donutChartData,
                donutChartConfig
            )
            Legend(donutChartData.slices, listOfColor)
        }

Results in a black background behind the circle instead of a transparent background.

@mathklk
Copy link

mathklk commented Jan 31, 2024

I'm facing the same issue. I've set the background on a LineChart to transparent, but it renders as the background color instead of the composable that is actually underneath.

It seems to be a problem with the Modifier.background method, that sets the background color of the Canvas element which is used under the hood.

fun Modifier.background(
    color: Color,
    shape: Shape = RectangleShape
): Modifier {
    val alpha = 1.0f // for solid colors
    return this.then(
        BackgroundElement(
            color = color,
            shape = shape,
            alpha = alpha,
            inspectorInfo = debugInspectorInfo {
                name = "background"
                value = color
                properties["color"] = color
                properties["shape"] = shape
            }
        )
    )
}

The method sets the alpha to 1.0 regardless of the background color actually supplied to the chart.

@ErickSorto
Copy link
Author

ErickSorto commented Feb 3, 2024

@mathklk
You don't actually set transparency through the modifier for canvas. You would actually need to change the background for Surface. Surface is causing the issue. By default the Surface (parent composable) is not transparent and we need to make it transparent like so:

Surface(
     modifier =modifier,
     color = Color.Transparent
)
{ 
     //inner composable that determines background
}

The inner most composable should determine the background colors not surface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants