diff --git a/app/src/main/java/com/yovinchen/bookkeeping/ui/components/CategoryPieChart.kt b/app/src/main/java/com/yovinchen/bookkeeping/ui/components/CategoryPieChart.kt index 0dc7325..7410719 100644 --- a/app/src/main/java/com/yovinchen/bookkeeping/ui/components/CategoryPieChart.kt +++ b/app/src/main/java/com/yovinchen/bookkeeping/ui/components/CategoryPieChart.kt @@ -1,13 +1,17 @@ package com.yovinchen.bookkeeping.ui.components -import android.graphics.Color +import android.graphics.Color as AndroidColor +import androidx.compose.foundation.isSystemInDarkTheme 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.toArgb import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend import com.github.mikephil.charting.data.PieData import com.github.mikephil.charting.data.PieDataSet import com.github.mikephil.charting.data.PieEntry @@ -19,6 +23,9 @@ fun CategoryPieChart( categoryData: List>, modifier: Modifier = Modifier ) { + val isDarkTheme = isSystemInDarkTheme() + val textColor = MaterialTheme.colorScheme.onSurface.toArgb() + AndroidView( modifier = modifier .fillMaxWidth() @@ -28,11 +35,29 @@ fun CategoryPieChart( description.isEnabled = false setUsePercentValues(true) setDrawEntryLabels(true) - legend.isEnabled = true + + // 配置图例 + legend.apply { + isEnabled = true + this.textColor = textColor // 使用Material Theme的文字颜色 + textSize = 12f + form = Legend.LegendForm.CIRCLE + formSize = 12f + formToTextSpace = 8f + xEntrySpace = 16f + } + isDrawHoleEnabled = true holeRadius = 40f - setHoleColor(Color.TRANSPARENT) + setHoleColor(AndroidColor.TRANSPARENT) setTransparentCircleRadius(45f) + + // 设置标签文字颜色为白色(因为标签在彩色扇形上) + setEntryLabelColor(AndroidColor.WHITE) + setEntryLabelTextSize(12f) + + // 设置中心文字颜色跟随主题 + setCenterTextColor(textColor) } }, update = { chart -> @@ -44,7 +69,7 @@ fun CategoryPieChart( colors = ColorTemplate.MATERIAL_COLORS.toList() valueTextSize = 14f valueFormatter = PercentFormatter(chart) - valueTextColor = Color.WHITE + valueTextColor = AndroidColor.WHITE // 扇形上的数值文字保持白色 setDrawValues(true) }