From 47e202fa617bc6f854e27a5b6b48bfdeaac4e5af Mon Sep 17 00:00:00 2001 From: yovinchen Date: Wed, 27 Nov 2024 18:07:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E9=A5=BC=E5=9B=BE?= =?UTF-8?q?=E5=9C=A8=E6=B5=85=E8=89=B2=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=9B=BE?= =?UTF-8?q?=E4=BE=8B=E6=96=87=E5=AD=97=E9=A2=9C=E8=89=B2=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用 Material Theme 的 onSurface 颜色来设置图例文字颜色 - 确保文字颜色正确跟随系统主题 - 优化代码结构和注释 --- .../ui/components/CategoryPieChart.kt | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) 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) }