From 0b26785994ee313279cccb45a467a220c41cca11 Mon Sep 17 00:00:00 2001 From: wangyiqian Date: Fri, 15 Jul 2022 18:06:45 +0800 Subject: [PATCH] =?UTF-8?q?KChart=E6=8C=87=E6=A0=87=E6=96=87=E5=AD=97?= =?UTF-8?q?=E5=A4=AA=E5=A4=9A=E6=97=B6=E6=94=AF=E6=8C=81=E6=8D=A2=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stockchart/childchart/kchart/KChart.kt | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kchart/KChart.kt b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kchart/KChart.kt index 3052a0e..ea6a156 100644 --- a/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kchart/KChart.kt +++ b/lib/src/main/java/com/github/wangyiqian/stockchart/childchart/kchart/KChart.kt @@ -583,8 +583,9 @@ open class KChart( highlight?.getIdx() ?: stockChart.findLastNotEmptyKEntityIdxInDisplayArea() indexTextPaint.textSize = index.textSize var left = index.textMarginLeft - val top = index.textMarginTop + var top = index.textMarginTop indexTextPaint.getFontMetrics(tmpFontMetrics) + val textHeight = tmpFontMetrics.bottom - tmpFontMetrics.top if (!index.startText.isNullOrEmpty()) { indexTextPaint.color = index.startTextColor canvas.drawText( @@ -594,9 +595,9 @@ open class KChart( indexTextPaint ) left += indexTextPaint.measureText(index.startText) + index.textSpace - drawnIndexTextHeight = - tmpFontMetrics.bottom - tmpFontMetrics.top + drawnIndexTextHeight = textHeight + index.textMarginTop } + var isFirstLine = true indexList.forEachIndexed { lineIdx, pointList -> chartConfig.indexColors?.let { indexColors -> if (lineIdx < indexColors.size) { @@ -604,6 +605,20 @@ open class KChart( val value = if (indexIdx != null && indexIdx in pointList.indices && pointList[indexIdx] != null) pointList[indexIdx] else null val text = index.textFormatter.invoke(lineIdx, value) + val textWidth = indexTextPaint.measureText(text) + + if(left + textWidth > getChartDisplayArea().width()){ + // 需要换行 + isFirstLine = false + left = index.textMarginLeft + top += textHeight + drawnIndexTextHeight += textHeight + } + + if(isFirstLine){ + drawnIndexTextHeight = textHeight + index.textMarginTop + } + canvas.drawText( text, left, @@ -611,8 +626,6 @@ open class KChart( indexTextPaint ) left += indexTextPaint.measureText(text) + index.textSpace - drawnIndexTextHeight = - tmpFontMetrics.bottom - tmpFontMetrics.top } } }