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