修复饼图标注重叠:周月年切片多时间距自动压缩,不再文字数字重合(v0.14)
This commit is contained in:
@@ -199,22 +199,40 @@ class LabelDonutView @JvmOverloads constructor(
|
||||
|
||||
/** 同侧标签按落点排序后做最小间距夹逼,再整体收回可视区 */
|
||||
private fun layoutTags(tags: List<Tag>) {
|
||||
val minGap = context.dpf(29f)
|
||||
// 上下留出两行字的高度:时间在 y 上方、名称在 y 下方,压到边界会被裁掉
|
||||
val top = context.dpf(15f)
|
||||
val bottom = height - context.dpf(17f)
|
||||
val avail = (bottom - top).coerceAtLeast(0f)
|
||||
val ideal = context.dpf(29f)
|
||||
for (side in intArrayOf(-1, 1)) {
|
||||
val group = tags.filter { it.side == side }.sortedBy { it.anchorY }
|
||||
if (group.isEmpty()) continue
|
||||
|
||||
// 理想间距 29dp;周/月/年切片多时一侧放不下,
|
||||
// 按可用高度压缩间距(间距 = 可用高度 / 间隔数),
|
||||
// 保证相邻标签永不重叠——原先"溢出整体上移+钳回边界"
|
||||
// 会把多个标签压到同一 y,文字数字叠在一起。
|
||||
val minGap = if (group.size > 1) {
|
||||
minOf(ideal, avail / (group.size - 1))
|
||||
} else ideal
|
||||
|
||||
group.forEachIndexed { i, t ->
|
||||
t.y = if (i == 0) maxOf(t.anchorY, top) else maxOf(t.anchorY, group[i - 1].y + minGap)
|
||||
}
|
||||
// 少量溢出(间距未压缩仍超界):整体上移,首端不低于 top
|
||||
val overflow = group.last().y - bottom
|
||||
if (overflow > 0f) group.forEach { it.y -= overflow }
|
||||
// 极端情况下(标注多、控件矮)再压回上界,宁可间距略小也不出画布
|
||||
val lo = minOf(top, bottom)
|
||||
val hi = maxOf(top, bottom)
|
||||
group.forEach { it.y = it.y.coerceIn(lo, hi) }
|
||||
if (overflow > 0f) {
|
||||
group.forEach { it.y -= overflow }
|
||||
if (group.first().y < top) {
|
||||
// 上下都顶住仍放不下(极端小控件):在 [top, bottom] 内均匀铺开
|
||||
val n = group.size
|
||||
if (n == 1) {
|
||||
group[0].y = top
|
||||
} else {
|
||||
val step = avail / (n - 1)
|
||||
group.forEachIndexed { i, t -> t.y = top + i * step }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user