fix: XAxis not respecting right offset

This commit is contained in:
Florian Bouillon 2023-01-10 14:46:07 +01:00
parent af2329805b
commit 732aa65c19
3 changed files with 18 additions and 5 deletions

View File

@ -108,15 +108,17 @@ class ChartView @JvmOverloads constructor(context: Context?, attrs: AttributeSet
} }
val bottom = xAxis.onDraw(canvas, rect.apply { var bottom = xAxis.getHeight() ?: 0f
set(padding, 0f, width.toFloat() - padding, height.toFloat() - padding)
})
// right distance from the yAxis // right distance from the yAxis
val rightDistance = yAxis.onDraw(canvas, rect.apply { val rightDistance = yAxis.onDraw(canvas, rect.apply {
set(padding, padding, width.toFloat() - padding, height.toFloat() - bottom - padding) set(padding, padding, width.toFloat() - padding, height.toFloat() - bottom - padding)
}) })
bottom = xAxis.onDraw(canvas, rect.apply {
set(padding, 0f, width.toFloat() - rightDistance - padding, height.toFloat() - padding)
})
// chart draw rectangle // chart draw rectangle
rect.apply { rect.apply {
set( set(

View File

@ -5,7 +5,6 @@ import android.graphics.Color
import android.graphics.Paint import android.graphics.Paint
import android.graphics.Rect import android.graphics.Rect
import android.graphics.RectF import android.graphics.RectF
import android.util.Log
import com.dzeio.charts.ChartViewInterface import com.dzeio.charts.ChartViewInterface
import com.dzeio.charts.Entry import com.dzeio.charts.Entry
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -53,6 +52,12 @@ class XAxis(
private val rect = Rect() private val rect = Rect()
private var height: Float? = null
override fun getHeight(): Float? {
return height
}
override fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double { override fun getPositionOnRect(entry: Entry, drawableSpace: RectF): Double {
return translatePositionToRect(entry.x, drawableSpace) return translatePositionToRect(entry.x, drawableSpace)
} }
@ -89,7 +94,7 @@ class XAxis(
var maxHeight = 0f var maxHeight = 0f
val graphIncrement = space.width() / (labelCount - 1) val graphIncrement = space.width() / (labelCount - 1)
val valueIncrement = (getDataWidth() / (labelCount - 1)).toDouble() val valueIncrement = getDataWidth() / (labelCount - 1)
for (index in 0 until labelCount) { for (index in 0 until labelCount) {
val text = onValueFormat(x + valueIncrement * index) val text = onValueFormat(x + valueIncrement * index)
textPaint.getTextBounds(text, 0, text.length, rect) textPaint.getTextBounds(text, 0, text.length, rect)
@ -108,6 +113,7 @@ class XAxis(
textPaint textPaint
) )
} }
height = maxHeight + 32f
return maxHeight + 32f return maxHeight + 32f
} }

View File

@ -81,4 +81,9 @@ sealed interface XAxisInterface {
* @return the final height of the XAxis * @return the final height of the XAxis
*/ */
fun onDraw(canvas: Canvas, space: RectF): Float fun onDraw(canvas: Canvas, space: RectF): Float
/**
* return the height of the XAxis (available after first draw)
*/
fun getHeight(): Float?
} }