| | |
| | | updateBarChartData() |
| | | } |
| | | private fun updateBarChartData() { |
| | | viewModel.getDailyStats().observe(viewLifecycleOwner) { stats -> |
| | | // 添加调试日志 |
| | | Log.d("DashboardFragment", "Stats size: ${stats.size}") |
| | | stats.forEach { stat -> |
| | | Log.d("DashboardFragment", "Week: ${stat.date}, Count: ${stat.count}, Start: ${stat.week_start}") |
| | | } |
| | | |
| | | if (stats.isEmpty()) { |
| | | Log.d("DashboardFragment", "No data found") |
| | | return@observe |
| | | } |
| | | viewModel.getDailyStats(currentDate.timeInMillis).observe(viewLifecycleOwner) { stats -> |
| | | if (stats.isEmpty()) return@observe |
| | | |
| | | val entries = stats.mapIndexed { index, stat -> |
| | | BarEntry(index.toFloat(), stat.count.toFloat()) |
| | |
| | | override fun getFormattedValue(value: Float): String { |
| | | val position = value.toInt() |
| | | if (position >= 0 && position < stats.size) { |
| | | val weekNum = stats[position].date.toIntOrNull() ?: 0 |
| | | // 获取月份信息 |
| | | val monthDay = stats[position].week_start?.let { |
| | | SimpleDateFormat("MM-dd", Locale.getDefault()).format( |
| | | SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).parse(it) |
| | | ) |
| | | } ?: "" |
| | | return "${monthDay}\n第${weekNum}周" |
| | | return "第${stats[position].date}周" |
| | | } |
| | | return "" |
| | | } |
| | | } |
| | | labelCount = stats.size |
| | | granularity = 1f |
| | | labelRotationAngle = -45f |
| | | textSize = 10f |
| | | setDrawGridLines(false) |
| | | } |
| | | |
| | | barChart.notifyDataSetChanged() |
| | | barChart.invalidate() |
| | | // 调整图表显示 |
| | | 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() { |