饼图标注回到6条并在下方完整列出其余切片(v3.13)
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# 变更记录
|
||||
|
||||
## 2026-12-01 · 饼图标注回到6条并在下方完整列出其余切片(v3.13)
|
||||
|
||||
- 同步源码更新 4 个文件(新增/修改 4,删除 0)。
|
||||
|
||||
## 2026-12-01 · 删除改整系列软删除、系列统计与饼图归属修正(v3.12)
|
||||
|
||||
- 项目展开任务行统计按整个系列聚合:`Store.projectTaskRows` 用递归 CTE 把根任务、重复实例、完成记录副本归到同一个 root,最近完成 / 累计专注 / 累计次数(新增 `done_cnt`)都按系列求和。重复任务每轮的专注终于算进同一个任务,不再是 0。
|
||||
|
||||
@@ -539,6 +539,6 @@ class SettingsFragment : Fragment(R.layout.fragment_settings) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val VERSION = "3.12"
|
||||
const val VERSION = "3.13"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.focusbox.app.ui.stats
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Typeface
|
||||
import android.graphics.drawable.GradientDrawable
|
||||
import android.util.AttributeSet
|
||||
import android.util.TypedValue
|
||||
import android.view.Gravity
|
||||
@@ -15,6 +16,7 @@ import com.focusbox.app.util.Dates
|
||||
import com.focusbox.app.util.col
|
||||
import com.focusbox.app.util.dpi
|
||||
import com.focusbox.app.widget.LabelDonutView
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* 「项目分布 / 任务分布」卡片:自带 日 / 周 / 月 / 年 四档与左右翻页。
|
||||
@@ -42,6 +44,12 @@ class PeriodDistributionCard @JvmOverloads constructor(
|
||||
private val btnNext = ImageView(context)
|
||||
private val donut = LabelDonutView(context)
|
||||
|
||||
/**
|
||||
* 环下方明细列表:环只标占比最高的 6 条,剩下的全部在这里逐条列出。
|
||||
* 不合并成「其他」——被合并掉的任务在页面上等于消失,用户根本不知道自己漏了什么。
|
||||
*/
|
||||
private val legend = LinearLayout(context)
|
||||
|
||||
/** 当前粒度,默认按天 */
|
||||
var scale = Scale.DAY
|
||||
private set
|
||||
@@ -145,6 +153,90 @@ class PeriodDistributionCard @JvmOverloads constructor(
|
||||
addView(donut, LayoutParams(LayoutParams.MATCH_PARENT, context.dpi(222f)).apply {
|
||||
topMargin = context.dpi(2f)
|
||||
})
|
||||
|
||||
legend.apply {
|
||||
orientation = VERTICAL
|
||||
visibility = GONE
|
||||
}
|
||||
addView(legend, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT).apply {
|
||||
topMargin = context.dpi(6f)
|
||||
})
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 明细列表
|
||||
|
||||
/**
|
||||
* 把环上没有牵线的切片按「圆点 + 名称 / 时长 · 占比」两列铺开(参考图版式)。
|
||||
* 每格宽度均分,名称超长省略,保证列对齐不来回跳。
|
||||
*/
|
||||
private fun renderLegend(rest: List<LabelDonutView.Slice>, total: Int) {
|
||||
legend.removeAllViews()
|
||||
if (rest.isEmpty() || total <= 0) {
|
||||
legend.visibility = GONE
|
||||
return
|
||||
}
|
||||
legend.visibility = VISIBLE
|
||||
|
||||
rest.chunked(2).forEach { pair ->
|
||||
val row = LinearLayout(context).apply { orientation = HORIZONTAL }
|
||||
legend.addView(row, LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT))
|
||||
pair.forEach { slice ->
|
||||
row.addView(buildLegendCell(slice, total), LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f))
|
||||
}
|
||||
// 单数时补一个空位,最后一行不会因为权重拉伸变成通栏
|
||||
if (pair.size == 1) {
|
||||
row.addView(android.view.View(context), LayoutParams(0, 1, 1f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildLegendCell(slice: LabelDonutView.Slice, total: Int): LinearLayout {
|
||||
val cell = LinearLayout(context).apply {
|
||||
orientation = HORIZONTAL
|
||||
setPadding(0, context.dpi(7f), context.dpi(8f), context.dpi(7f))
|
||||
}
|
||||
|
||||
val dot = android.view.View(context).apply {
|
||||
background = GradientDrawable().apply {
|
||||
shape = GradientDrawable.OVAL
|
||||
setColor(slice.color)
|
||||
}
|
||||
}
|
||||
// 8dp 圆点,下移 5dp 与名称首行的视觉中心对齐
|
||||
cell.addView(dot, LayoutParams(context.dpi(8f), context.dpi(8f)).apply {
|
||||
topMargin = context.dpi(5f)
|
||||
})
|
||||
|
||||
val text = LinearLayout(context).apply {
|
||||
orientation = VERTICAL
|
||||
setPadding(context.dpi(8f), 0, 0, 0)
|
||||
}
|
||||
cell.addView(text, LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f))
|
||||
|
||||
text.addView(TextView(context).apply {
|
||||
this.text = slice.name
|
||||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
|
||||
setTextColor(context.col(R.color.title))
|
||||
maxLines = 1
|
||||
ellipsize = android.text.TextUtils.TruncateAt.END
|
||||
includeFontPadding = false
|
||||
})
|
||||
|
||||
val pct = slice.seconds * 100f / total
|
||||
val pctText = if (pct < 1f) "<1%" else "${pct.roundToInt()}%"
|
||||
text.addView(TextView(context).apply {
|
||||
this.text = context.getString(
|
||||
R.string.legend_meta,
|
||||
Dates.duration(slice.seconds),
|
||||
pctText,
|
||||
)
|
||||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 11f)
|
||||
setTextColor(context.col(R.color.text_secondary))
|
||||
maxLines = 1
|
||||
includeFontPadding = false
|
||||
})
|
||||
|
||||
return cell
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 交互
|
||||
@@ -204,7 +296,9 @@ class PeriodDistributionCard @JvmOverloads constructor(
|
||||
)
|
||||
}
|
||||
)
|
||||
donut.centerText = Dates.durationBrief(raw.sumOf { it.seconds })
|
||||
val total = raw.sumOf { it.seconds }
|
||||
donut.centerText = Dates.durationBrief(total)
|
||||
renderLegend(donut.unlabeled, total)
|
||||
|
||||
tvPeriod.text = periodLabel(from, to)
|
||||
applySegments()
|
||||
|
||||
@@ -44,10 +44,26 @@ class LabelDonutView @JvmOverloads constructor(
|
||||
}
|
||||
|
||||
/**
|
||||
* 最多画几条引线标注,多出来的合并成「其他」,避免标注挤成一团。
|
||||
* 8 条(每侧 4 条)在 222dp 高度里仍排得开;再往上夹逼会把标注压得太密。
|
||||
* 环上最多给几条切片牵引线标注(取占比最高的若干条)。
|
||||
*
|
||||
* 超出部分**不再合并成「其他」**:环本身照旧把每一块都按真实占比画出来,
|
||||
* 只是没牵到线的切片记进 [unlabeled],由调用方在图表下方完整列出。
|
||||
* 6 条(每侧 3 条)在 222dp 高度里间距仍舒展,再往上夹逼会把相邻标注压到一起。
|
||||
*/
|
||||
var maxLabels: Int = 8
|
||||
var maxLabels: Int = 6
|
||||
|
||||
/** 扇区张角小于该值时不牵线:引线会叠在同一个点上,约 0.8% 以下的占比 */
|
||||
private val minLabelSweep = 3f
|
||||
|
||||
/**
|
||||
* 环上没能牵线的切片(占比排在 [maxLabels] 之外,或扇区太窄),按占比从高到低。
|
||||
* 调用方必须把它们全部列在图表下方,否则这些任务/项目在视觉上就"消失"了。
|
||||
*/
|
||||
var unlabeled: List<Slice> = emptyList()
|
||||
private set
|
||||
|
||||
/** 已经牵到线的切片,按同一套规则算出,供绘制使用 */
|
||||
private var labeled: List<Slice> = emptyList()
|
||||
|
||||
private val strokeW = context.dpf(28f)
|
||||
|
||||
@@ -86,9 +102,29 @@ class LabelDonutView @JvmOverloads constructor(
|
||||
|
||||
fun setSlices(list: List<Slice>) {
|
||||
slices = list
|
||||
plan(list)
|
||||
invalidate()
|
||||
}
|
||||
|
||||
/**
|
||||
* 标注计划:按占比排序,前 [maxLabels] 名里扇区够宽的才牵线,
|
||||
* 其余(含前 N 名但太窄的)全部落到 [unlabeled]。
|
||||
*/
|
||||
private fun plan(list: List<Slice>) {
|
||||
val total = list.sumOf { it.seconds }
|
||||
val sorted = list.sortedByDescending { it.seconds }
|
||||
val head = if (total <= 0) emptyList() else sorted.take(maxLabels).filter {
|
||||
it.seconds.toFloat() / total * 360f >= minLabelSweep
|
||||
}
|
||||
labeled = head
|
||||
// 用「逐个摘除」而不是 toSet 差集:同名同色同时长的两条切片也能各算一次
|
||||
val left = head.toMutableList()
|
||||
unlabeled = sorted.filter { s ->
|
||||
val i = left.indexOf(s)
|
||||
if (i >= 0) { left.removeAt(i); false } else true
|
||||
}
|
||||
}
|
||||
|
||||
private data class Tag(
|
||||
val slice: Slice,
|
||||
val midDeg: Float,
|
||||
@@ -113,18 +149,20 @@ class LabelDonutView @JvmOverloads constructor(
|
||||
return
|
||||
}
|
||||
|
||||
val shown = collapse(slices)
|
||||
val box = RectF(cx - r, cy - r, cx + r, cy + r)
|
||||
val gapDeg = if (shown.size > 1) 2.2f else 0f
|
||||
// 不再合并「其他」后切片可能有几十条,固定 2.2° 的缝隙累计起来会吃掉小半个环
|
||||
// (40 条就是 88°),因此按条数摊薄,总缝隙不超过约 14°
|
||||
val gapDeg = if (slices.size > 1) minOf(2.2f, 14f / slices.size) else 0f
|
||||
|
||||
// ---- 弧与百分比
|
||||
// ---- 弧与百分比:每一块都按真实占比画,绝不合并
|
||||
var start = -90f
|
||||
val tags = ArrayList<Tag>(shown.size)
|
||||
shown.forEach { s ->
|
||||
val tags = ArrayList<Tag>(labeled.size)
|
||||
val pending = labeled.toMutableList()
|
||||
slices.forEach { s ->
|
||||
val sweep = s.seconds.toFloat() / total * 360f
|
||||
val midDeg = start + sweep / 2f
|
||||
arcPaint.color = s.color
|
||||
canvas.drawArc(box, start + gapDeg / 2f, (sweep - gapDeg).coerceAtLeast(0.5f), false, arcPaint)
|
||||
canvas.drawArc(box, start + gapDeg / 2f, (sweep - gapDeg).coerceAtLeast(0.2f), false, arcPaint)
|
||||
|
||||
val arcLen = sweep / 360f * (2 * Math.PI.toFloat() * r)
|
||||
val pct = "${(s.seconds * 100f / total).toInt().coerceAtLeast(1)}%"
|
||||
@@ -144,8 +182,8 @@ class LabelDonutView @JvmOverloads constructor(
|
||||
canvas.drawText(pct, px, py - (fm.ascent + fm.descent) / 2f, pctPaint)
|
||||
}
|
||||
|
||||
// 太薄的扇区不牵线,否则几条线会叠在同一个点上(约 0.8% 以下的占比)
|
||||
if (sweep >= 3f) {
|
||||
// 只有进了标注计划的切片才牵线;pending 逐个摘除,重复切片也各算一次
|
||||
if (sweep >= minLabelSweep && pending.remove(s)) {
|
||||
val cosA = cos(radians(midDeg))
|
||||
val sinA = sin(radians(midDeg))
|
||||
tags.add(
|
||||
@@ -167,17 +205,6 @@ class LabelDonutView @JvmOverloads constructor(
|
||||
|
||||
private fun radians(deg: Float): Double = Math.toRadians(deg.toDouble())
|
||||
|
||||
private fun collapse(list: List<Slice>): List<Slice> {
|
||||
if (list.size <= maxLabels) return list
|
||||
val head = list.take(maxLabels - 1)
|
||||
val rest = list.drop(maxLabels - 1)
|
||||
return head + Slice(
|
||||
name = context.getString(R.string.slice_other),
|
||||
seconds = rest.sumOf { it.seconds },
|
||||
color = context.col(R.color.text_tertiary),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 环中心只画一行:累计时间(或空态提示)。
|
||||
* 之前按 cy + 2dp 硬偏移定位,字面重心偏下;改为按字体度量
|
||||
|
||||
@@ -169,6 +169,8 @@
|
||||
<string name="period_month">%1$d年%2$d月</string>
|
||||
<string name="period_year">%1$d年</string>
|
||||
<string name="slice_other">其他</string>
|
||||
<!-- 环图下方明细行:「时长 · 占比」 -->
|
||||
<string name="legend_meta">%1$s · %2$s</string>
|
||||
<string name="no_record">暂无记录</string>
|
||||
<string name="summary_title">累计</string>
|
||||
<string name="streak_days">连续打卡</string>
|
||||
|
||||
Reference in New Issue
Block a user