fix: barchart: text overflowing if bar is too small (#41)

This commit is contained in:
Florian Bouillon 2023-01-16 00:50:34 +01:00 committed by GitHub
parent 4f5fb6fae4
commit b9379d6526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 7 deletions

View File

@ -52,7 +52,7 @@ class XAxis(
}
override fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double {
val result = drawableSpace.left + drawableSpace.width() * (entry.x - x) / getDataWidth()
val result = getPositionOnRect(entry.x, drawableSpace)
if (view.type == ChartType.GROUPED) {
val serie = view.series.find { it.entries.contains(entry) }
val index = view.series.indexOf(serie)
@ -61,6 +61,10 @@ class XAxis(
return result
}
override fun getPositionOnRect(position: Double, drawableSpace: RectF): Double {
return drawableSpace.left + drawableSpace.width() * (position - x) / getDataWidth()
}
override fun getXMax(): Double {
return view.series.maxOf { serie ->
if (serie.entries.isEmpty()) {
@ -79,7 +83,7 @@ class XAxis(
}
}
var onValueFormat: (value: Double) -> String = { it -> it.roundToInt().toString() }
override var onValueFormat: (value: Double) -> String = { it -> it.roundToInt().toString() }
override fun onDraw(canvas: Canvas, space: RectF): Float {
if (!enabled) {

View File

@ -41,6 +41,8 @@ sealed interface XAxisInterface {
*/
var scrollEnabled: Boolean
var onValueFormat: (value: Double) -> String
/**
* run when manually refreshing the system
*
@ -51,10 +53,23 @@ sealed interface XAxisInterface {
/**
* get the entry position on the rect
*
* @param entry the entry to place
* @param drawableSpace the space it should go into
*
* @return the left side of the position of the entry
*/
fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double
/**
* get the entry position on the rect
*
* @param position the position of your point
* @param drawableSpace the space it should go into
*
* @return the left side of the position of the entry
*/
fun getPositionOnRect(position: Double, drawableSpace: RectF): Double
/**
* get the maximum the X can get to
*/

View File

@ -36,7 +36,7 @@ class YAxis(
strokeWidth = 4f
}
var onValueFormat: (value: Float) -> String = { it -> it.roundToInt().toString() }
override var onValueFormat: (value: Float) -> String = { it -> it.roundToInt().toString() }
override var labelCount = 5

View File

@ -39,6 +39,8 @@ sealed interface YAxisInterface {
*/
var scrollEnabled: Boolean
var onValueFormat: (value: Float) -> String
/**
* do the Zero line gets drawn?
*/

View File

@ -5,11 +5,11 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import com.dzeio.charts.ChartView
import com.dzeio.charts.ChartViewInterface
import com.dzeio.charts.utils.drawRoundRect
class BarSerie(
private val view: ChartView
private val view: ChartViewInterface
) : BaseSerie(view) {
private companion object {
@ -48,6 +48,7 @@ class BarSerie(
val barWidth = view.xAxis.getEntryWidth(drawableSpace).toFloat()
val zero = view.yAxis.getPositionOnRect(0f, drawableSpace)
.coerceIn(drawableSpace.top, drawableSpace.bottom)
var needUpdate = false
@ -134,6 +135,10 @@ class BarSerie(
textPaint.getTextBounds(text, 0, text.length, rect)
if (barWidth < rect.width()) {
continue
}
// text center X
val textX = (posX + barWidth / 2)

View File

@ -4,11 +4,11 @@ import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import com.dzeio.charts.ChartView
import com.dzeio.charts.ChartViewInterface
import kotlin.math.abs
class LineSerie(
private val view: ChartView
private val view: ChartViewInterface
) : BaseSerie(view) {
private companion object {