饼图标注回到6条并在下方完整列出其余切片(v3.13)

This commit is contained in:
XIGE
2026-12-01 19:00:43 +08:00
parent c9c7a79349
commit 177c965894
5 changed files with 151 additions and 24 deletions

View File

@@ -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()