๐Ÿ“ŠJetpack Compose ํ‘œ ๋งŒ๋“ค๊ธฐ

: ) YOUNGยท2023๋…„ 9์›” 13์ผ
1

์•ˆ๋“œ๋กœ์ด๋“œ

๋ชฉ๋ก ๋ณด๊ธฐ
14/30
post-thumbnail
post-custom-banner

Jetpack Compose์—์„œ ํ‘œ๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.
๋จผ์ € ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

implementation 'co.yml:ycharts:2.1.0'

๊ทธ๋ฆฌ๊ณ  ์Šคํฌ๋ฆฐ์—์„œ ๋‚˜์˜ค๊ฒŒํ•  Composable ํ•จ์ˆ˜ ํ•˜๋‚˜๋งŒ ์ƒ์„ฑํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

๋จผ์ € PieChart๋ฅผ ๋งŒ๋“ค๊ฑด๋ฐ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒํ•  Screenํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.


@Composable
fun PieChartScreen() {


}

๊ฐ€์žฅ ๋จผ์ € ํ•  ์ผ์€ ํ•ด๋‹น ํŒŒ์ด์ฐจํŠธ์—์„œ ๋ณด์—ฌ์งˆ ๋‚ด์šฉ์ฆ‰, value๋“ค์ธ๋ฐ slices์— listํ˜•ํƒœ๋กœ ๋“ค์–ด๊ฐ„๋‹ค.

์šฐ๋ฆฌ๋Š” ๋ฉ”๋‰ด๋ฅผ ์„ ์ •ํ•˜๋Š”๋ฐ, ์–ด๋–ค ๋ฉ”๋‰ด๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ์„ ํƒ๋˜์—ˆ๋Š”์ง€ ํ•œ๋ฒˆ ํ™•์ธํ•ด๋ณด๋Š”๊ฑธ๋กœ ํ•ด๋ณด์ž.


    val pieChartData = PieChartData(
        slices = listOf(
            PieChartData.Slice(
                label = "๋ˆ๊นŒ์Šค",
                value = 65f,
                color = Color(0xFFDF5D5D)
            ),
            PieChartData.Slice(
                label = "์ œ์œก",
                value = 35f,
                color = Color(0xFFFFC39A)
            ),
            PieChartData.Slice(
                label = "์งœ์žฅ๋ฉด",
                value = 10f,
                color = Color(0xFFFDB6CE)
            ),
            PieChartData.Slice(
                label = "์ดˆ๋ฐฅ",
                value = 40f,
                color = Color(0xFFE96D74)
            ),
            PieChartData.Slice(
                label = "๋–ก๋ณถ์ด",
                value = 40f,
                color = Color(0xFFFF9367)
            )
        ),
        plotType = PlotType.Pie
    )

์„ ํƒ์ง€๋Š” 5๊ฐ€์ง€๋กœ label์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„์ด ๋‚ด์šฉ์˜ ํ…์ŠคํŠธ์ด๋‹ค. ๋ˆ๊นŒ์Šค, ์ œ์œก, ์งœ์žฅ๋ฉด, ์ดˆ๋ฐฅ, ๋–ก๋ณถ์ด๊ฐ€ ๋˜๊ณ , value๊ฐ€ %๋กœ ํ‘œ์‹œ๋˜๋Š” ๋ถ€๋ถ„์ธ๋ฐ, ๊ฐ์ž ๋งž๋Š” ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. color๋Š” ํ•ด๋‹น ํŒŒ์ด์ฐจํŠธ์—์„œ ์˜์—ญ๋ณ„ ์ƒ‰์ƒ์ด๋‹ค.

plotType = PlotType.Pie๋Š” ์šฐ๋ฆฌ๊ฐ€ ํŒŒ์ด์ฐจํŠธ์—์„œ ์–ด๋–ป๊ฒŒ ๋ณด์—ฌ์ค„์ง€ ์˜ต์…˜์„ ์„ ํƒํ•˜๋Š” ๊ฒƒ์ธ๋ฐ, Donut, Bar, Pie ๋“ฑ๋“ฑ์˜ ๋‹ค์–‘ํ•œ ํ˜•ํƒœ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

์ด์ œ ํŒŒ์ด์ฐจํŠธ์— ๋“ค์–ด๊ฐˆ ๊ฐ’์„ ๋„ฃ์—ˆ์œผ๋‹ˆ. ํ•ด๋‹น ์ฐจํŠธ์˜ ์ƒ์„ธ ์˜ต์…˜์„ ํ•œ๋ฒˆ ์„ค์ •ํ•ด์ฃผ๋ฉด๋œ๋‹ค.

์ œ๊ณตํ•˜๋Š” PieChartConfig() data class์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


    val pieChartConfig = PieChartConfig(
        isAnimationEnable = true,
        showSliceLabels = true,
        isSumVisible = true,
        activeSliceAlpha = 0.5f,
        chartPadding = 5,
        sliceLabelTextColor = Color.Black,
        sliceLabelTextSize = 30.sp,
        animationDuration = 1400,
    )

์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ ์„ ํƒํ•œ ์˜ต์…˜์€

์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋ณด์ด๊ฒŒ ํ•˜๊ณ ์‹ถ์—ˆ๊ณ , isAnimationEnable = true

๋ผ๋ฒจ์„ ๋ณด์ด๊ฒŒ ํ•˜๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ, ๊ฒ€์ •์ƒ‰์œผ๋กœ ํฌ๊ฒŒ ๋ณด์ด๊ฒŒ ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค

        showSliceLabels = true,
        sliceLabelTextColor = Color.Black,
        sliceLabelTextSize = 30.sp,

์ถ”๊ฐ€๋กœ ์ œ๊ณตํ•˜๋Š” ์˜ต์…˜๋“ค๊ณผ ์„ค๋ช…๋“ค์ด ์žˆ์œผ๋‹ˆ ๊ฐ์ž ๊ณจ๋ผ์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค!

๋งˆ์ง€๋ง‰์œผ๋กœ PieChart ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹คํ–‰๋งŒ ํ•ด์ฃผ๋ฉด๋œ๋‹ค.


    PieChart(
        modifier = Modifier
            .width(440.dp)
            .height(440.dp),
        pieChartData = pieChartData,
        pieChartConfig = pieChartConfig
    )

modifier = Modifier .width(440.dp) .height(440.dp), ์ฐจํŠธ์˜ ํฌ๊ธฐ๋ฅผ ์„ค์ •ํ•ด์ฃผ๊ณ ,
pieChartData = pieChartData, ๋“ค์–ด๊ฐˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์ •ํ•ด์ค€๋’ค
pieChartConfig = pieChartConfig ์˜ต์…˜ ์„ค์ •์„ ํ•ด์ฃผ๋ฉด ๋์ด๋‹ค.

์ด๋ ‡๊ฒŒ ํ•œ ํ›„ ์ „์ฒด PieChartScreen() ์„ ํ•„์š”ํ•œ ๊ณง์—์„œ ํ˜ธ์ถœ๋งŒ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.




์ „์ฒด์ฝ”๋“œ


import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import co.yml.charts.common.model.PlotType
import co.yml.charts.ui.piechart.charts.PieChart
import co.yml.charts.ui.piechart.models.PieChartConfig
import co.yml.charts.ui.piechart.models.PieChartData

@Composable
fun PieChartScreen() {
    val pieChartData = PieChartData(
        slices = listOf(
            PieChartData.Slice(
                label = "๋ˆ๊นŒ์Šค",
                value = 65f,
                color = Color(0xFFDF5D5D)
            ),
            PieChartData.Slice(
                label = "์ œ์œก",
                value = 35f,
                color = Color(0xFFFFC39A)
            ),
            PieChartData.Slice(
                label = "์งœ์žฅ๋ฉด",
                value = 10f,
                color = Color(0xFFFDB6CE)
            ),
            PieChartData.Slice(
                label = "์ดˆ๋ฐฅ",
                value = 40f,
                color = Color(0xFFE96D74)
            ),
            PieChartData.Slice(
                label = "๋–ก๋ณถ์ด",
                value = 40f,
                color = Color(0xFFFF9367)
            )
        ),
        plotType = PlotType.Pie
    )

    val pieChartConfig = PieChartConfig(
        isAnimationEnable = true,
        showSliceLabels = true,
        isSumVisible = true,
        activeSliceAlpha = 0.5f,
        chartPadding = 5,
        sliceLabelTextColor = Color.Black,
        sliceLabelTextSize = 30.sp,
        animationDuration = 1400,
    )

    PieChart(
        modifier = Modifier
            .width(440.dp)
            .height(440.dp),
        pieChartData = pieChartData,
        pieChartConfig = pieChartConfig
    )
} // End of PieChartScreen()



๋ฒˆ์™ธ ๋ผ์ธ์ฐจํŠธ

์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ œ๊ณตํ•˜๋Š” ํ™•๋Œ€, ์Šฌ๋ผ์ด๋“œ๋„ ์ œ๊ณตํ•˜๋Š” ๋ผ์ธ์ฐจํŠธ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค!!


์ „์ฒด ์ฝ”๋“œ


import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import co.yml.charts.axis.AxisData
import co.yml.charts.common.model.Point
import co.yml.charts.ui.linechart.LineChart
import co.yml.charts.ui.linechart.model.*


@Composable
fun LineChartScreen() {
    val steps = 5
    val pointsData = listOf(
        Point(0f, 40f),
        Point(1f, 90f),
        Point(2f, 10f),
        Point(3f, 20f),
        Point(4f, 30f),
    )


    val xAxisData = AxisData.Builder().axisStepSize(100.dp).backgroundColor(Color.Transparent)
        .steps(pointsData.size - 1).labelData { idx ->
            idx.toString()
        }.labelAndAxisLinePadding(15.dp).axisLineColor(MaterialTheme.colorScheme.error)
        .axisLabelColor(MaterialTheme.colorScheme.error).build()


    val yAxisData = AxisData.Builder().steps(steps).backgroundColor(Color.Transparent)
        .labelAndAxisLinePadding(20.dp).labelData { idx ->
            val yScale = 100 / steps
            (idx * yScale).toString()
        }.axisLineColor(MaterialTheme.colorScheme.error)
        .axisLabelColor(MaterialTheme.colorScheme.error)
        .build()

    val lineChartData = LineChartData(
        linePlotData = LinePlotData(
            lines = listOf(
                Line(
                    dataPoints = pointsData,
                    LineStyle(
                        color = MaterialTheme.colorScheme.error,
                        lineType = LineType.SmoothCurve(isDotted = false)
                    ),
                    IntersectionPoint(
                        color = MaterialTheme.colorScheme.error
                    ),
                    SelectionHighlightPoint(
                        color = Color.Yellow
                    ),
                    ShadowUnderLine(
                        alpha = 0.5f,
                        brush = Brush.verticalGradient(
                            colors = listOf(
                                Color.Blue,
                                Color.Transparent
                            )
                        )
                    ),
                    SelectionHighlightPopUp()
                )
            )
        ),
        backgroundColor = MaterialTheme.colorScheme.surface,
        xAxisData = xAxisData,
        yAxisData = yAxisData,
        gridLines = GridLines(
            color = MaterialTheme.colorScheme.outlineVariant
        )
    )

    LineChart(
        modifier = Modifier
            .fillMaxWidth()
            .height(500.dp),
        lineChartData = lineChartData
    )
}


์ฐธ๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
https://github.com/yml-org/YCharts

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€