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,