From f1343046464dd8fb4b66c1d1cfe79b722314bfba Mon Sep 17 00:00:00 2001 From: yovinchen Date: Thu, 28 Nov 2024 14:34:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BA=E9=A5=BC=E5=9B=BE=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E7=82=B9=E5=87=BB=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 为CategoryPieChart添加点击事件处理 2. 点击饼图可跳转到对应类别详情页面 --- .../ui/components/CategoryPieChart.kt | 21 ++++++++++++- .../bookkeeping/ui/screen/AnalysisScreen.kt | 30 +++++++++++-------- 2 files changed, 38 insertions(+), 13 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 e0853bc..9a23fb1 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 @@ -12,16 +12,20 @@ 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.Entry import com.github.mikephil.charting.data.PieData import com.github.mikephil.charting.data.PieDataSet import com.github.mikephil.charting.data.PieEntry import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.highlight.Highlight +import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate @Composable fun CategoryPieChart( categoryData: List>, - modifier: Modifier = Modifier + modifier: Modifier = Modifier, + onCategoryClick: (String) -> Unit = {} ) { val isDarkTheme = isSystemInDarkTheme() val textColor = MaterialTheme.colorScheme.onSurface.toArgb() @@ -50,6 +54,21 @@ fun CategoryPieChart( // 设置中心文字颜色跟随主题 setCenterTextColor(textColor) + + // 添加点击事件监听器 + setOnChartValueSelectedListener(object : OnChartValueSelectedListener { + override fun onValueSelected(e: Entry?, h: Highlight?) { + e?.let { + if (it is PieEntry) { + onCategoryClick(it.label ?: return) + } + } + } + + override fun onNothingSelected() { + // 不需要处理 + } + }) } }, update = { chart -> diff --git a/app/src/main/java/com/yovinchen/bookkeeping/ui/screen/AnalysisScreen.kt b/app/src/main/java/com/yovinchen/bookkeeping/ui/screen/AnalysisScreen.kt index dc7708b..20ed14c 100644 --- a/app/src/main/java/com/yovinchen/bookkeeping/ui/screen/AnalysisScreen.kt +++ b/app/src/main/java/com/yovinchen/bookkeeping/ui/screen/AnalysisScreen.kt @@ -70,22 +70,28 @@ fun AnalysisScreen( } } - // 饼图 - if (selectedAnalysisType != AnalysisType.TREND) { - CategoryPieChart( - categoryData = categoryStats.map { Pair(it.category, it.percentage.toFloat()) }, - modifier = Modifier - .fillMaxWidth() - .height(200.dp) - .padding(16.dp) - ) - } - - // 分类统计列表 + // 使用LazyColumn包含饼图和列表 LazyColumn( modifier = Modifier.fillMaxSize(), contentPadding = PaddingValues(16.dp) ) { + // 添加饼图作为第一个项目 + if (selectedAnalysisType != AnalysisType.TREND) { + item { + CategoryPieChart( + categoryData = categoryStats.map { Pair(it.category, it.percentage.toFloat()) }, + modifier = Modifier + .fillMaxWidth() + .height(200.dp) + .padding(bottom = 16.dp), + onCategoryClick = { category -> + onNavigateToCategoryDetail(category, selectedMonth) + } + ) + } + } + + // 添加分类统计列表项目 items(categoryStats) { stat -> CategoryStatItem( stat = stat,