Skip to content

Commit

Permalink
KChart指标文字太多时支持换行
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyiqian committed Jul 15, 2022
1 parent c78eb1e commit 0b26785
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -594,25 +595,37 @@ 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) {
indexTextPaint.color = indexColors[lineIdx]
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,
-tmpFontMetrics.top + top,
indexTextPaint
)
left += indexTextPaint.measureText(text) + index.textSpace
drawnIndexTextHeight =
tmpFontMetrics.bottom - tmpFontMetrics.top
}
}
}
Expand Down

0 comments on commit 0b26785

Please sign in to comment.