From 2933603ee0940395a80a91bb23e50cbe8ba9baa5 Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期三, 05 三月 2025 10:03:16 +0800
Subject: [PATCH] fix: 2
---
app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt | 105 +++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 91 insertions(+), 14 deletions(-)
diff --git a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
index f0e308a..bfd5bb8 100644
--- a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
+++ b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
@@ -20,8 +20,10 @@
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
+import com.github.mikephil.charting.formatter.ValueFormatter
import java.util.*
import java.text.SimpleDateFormat
+import android.util.Log
class DashboardFragment : Fragment() {
@@ -132,53 +134,118 @@
setupPieChart()
}
private fun setupBarChart() {
- // 配置柱状图
barChart.apply {
description.isEnabled = false
setDrawGridBackground(false)
legend.isEnabled = false
+
+ // 增大图表高度
+ minimumHeight = (resources.displayMetrics.density * 300).toInt()
// X轴设置
xAxis.apply {
position = XAxis.XAxisPosition.BOTTOM
setDrawGridLines(false)
- valueFormatter = IndexAxisValueFormatter(getDayLabels())
+ granularity = 1f
+ labelRotationAngle = -45f
+ textSize = 10f
}
// Y轴设置
axisLeft.apply {
setDrawGridLines(true)
axisMinimum = 0f
+ granularity = 1f
+ valueFormatter = object : ValueFormatter() {
+ override fun getFormattedValue(value: Float): String {
+ return value.toInt().toString()
+ }
+ }
}
axisRight.isEnabled = false
+
+ // 设置图表交互
+ setTouchEnabled(true)
+ isDragEnabled = true
+ setScaleEnabled(true)
}
updateBarChartData()
}
private fun updateBarChartData() {
viewModel.getDailyStats(currentDate.timeInMillis).observe(viewLifecycleOwner) { stats ->
+ if (stats.isEmpty()) return@observe
+
val entries = stats.mapIndexed { index, stat ->
BarEntry(index.toFloat(), stat.count.toFloat())
}
val dataSet = BarDataSet(entries, "包裹数量")
dataSet.color = resources.getColor(R.color.purple_500)
+ dataSet.valueTextSize = 12f
- barChart.data = BarData(dataSet)
- barChart.invalidate()
+ val barData = BarData(dataSet)
+ barChart.data = barData
+
+ // 设置X轴标签
+ barChart.xAxis.apply {
+ valueFormatter = object : ValueFormatter() {
+ override fun getFormattedValue(value: Float): String {
+ val position = value.toInt()
+ if (position >= 0 && position < stats.size) {
+ return "第${stats[position].date}周"
+ }
+ return ""
+ }
+ }
+ labelCount = stats.size
+ granularity = 1f
+ labelRotationAngle = -45f
+ textSize = 10f
+ setDrawGridLines(false)
+ }
+
+ // 调整图表显示
+ barChart.apply {
+ setVisibleXRangeMaximum(5f)
+ moveViewToX(0f)
+
+ axisLeft.apply {
+ axisMinimum = 0f
+ granularity = 1f
+ setDrawGridLines(true)
+ }
+
+ barData.barWidth = 0.6f
+ description.isEnabled = false
+ legend.isEnabled = false
+
+ invalidate()
+ }
}
}
private fun setupPieChart() {
- // 配置饼图
pieChart.apply {
description.isEnabled = false
- setUsePercentValues(true)
+ setUsePercentValues(false) // 改为显示实际数量
setDrawEntryLabels(false)
-
- legend.isEnabled = true
- legend.verticalAlignment = Legend.LegendVerticalAlignment.CENTER
- legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
- legend.orientation = Legend.LegendOrientation.VERTICAL
+
+ // 增大饼图尺寸
+ setExtraOffsets(20f, 10f, 80f, 10f)
+ minimumHeight = (resources.displayMetrics.density * 400).toInt() // 设置最小高度
+
+ // 配置图例
+ legend.apply {
+ isEnabled = true
+ verticalAlignment = Legend.LegendVerticalAlignment.CENTER
+ horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
+ orientation = Legend.LegendOrientation.VERTICAL
+ setDrawInside(false)
+ xEntrySpace = 7f
+ yEntrySpace = 0f
+ yOffset = 0f
+ textSize = 12f // 增大图例文字大小
+ }
}
updatePieChartData()
@@ -186,16 +253,26 @@
private fun updatePieChartData() {
viewModel.getCourierStats(currentDate.timeInMillis).observe(viewLifecycleOwner) { stats ->
val entries = stats.map { stat ->
- PieEntry(stat.count.toFloat(), stat.courierName)
+ PieEntry(stat.count.toFloat(), "${stat.courierName}(${stat.count})")
}
val dataSet = PieDataSet(entries, "快递公司分布")
dataSet.colors = listOf(
resources.getColor(R.color.purple_500),
- resources.getColor(R.color.teal_200)
+ resources.getColor(R.color.teal_200),
+ resources.getColor(R.color.purple_200),
+ resources.getColor(R.color.teal_700)
)
+ dataSet.valueTextSize = 14f // 增大数值文字大小
- pieChart.data = PieData(dataSet)
+ val pieData = PieData(dataSet)
+ pieData.setValueFormatter(object : ValueFormatter() {
+ override fun getFormattedValue(value: Float): String {
+ return value.toInt().toString()
+ }
+ })
+
+ pieChart.data = pieData
pieChart.invalidate()
}
}
--
Gitblit v1.9.3