Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.github.mikephil.charting.utils.Transformer
import com.github.mikephil.charting.utils.Utils
import com.github.mikephil.charting.utils.ViewPortHandler
import com.github.mikephil.charting.utils.convertDpToPixel
import com.github.mikephil.charting.utils.drawXAxisValue
import kotlin.math.roundToInt

open class XAxisRenderer(
Expand Down Expand Up @@ -102,21 +103,25 @@ open class XAxisRenderer(
pointF.y = 1.0f
drawLabels(canvas, viewPortHandler.contentTop() - yOffset, pointF)
}

XAxisPosition.TOP_INSIDE -> {
pointF.x = 0.5f
pointF.y = 1.0f
drawLabels(canvas, viewPortHandler.contentTop() + yOffset + xAxis.mLabelHeight, pointF)
}

XAxisPosition.BOTTOM -> {
pointF.x = 0.5f
pointF.y = 0.0f
drawLabels(canvas, viewPortHandler.contentBottom() + yOffset, pointF)
}

XAxisPosition.BOTTOM_INSIDE -> {
pointF.x = 0.5f
pointF.y = 0.0f
drawLabels(canvas, viewPortHandler.contentBottom() - yOffset - xAxis.mLabelHeight, pointF)
}

else -> { // BOTH SIDED
pointF.x = 0.5f
pointF.y = 1.0f
Expand Down Expand Up @@ -222,7 +227,7 @@ open class XAxisRenderer(
}

protected fun drawLabel(canvas: Canvas, formattedLabel: String?, x: Float, y: Float, anchor: MPPointF, angleDegrees: Float) {
formattedLabel?.let { Utils.drawXAxisValue(canvas, it, x, y, paintAxisLabels, anchor, angleDegrees) }
formattedLabel?.let { canvas.drawXAxisValue(it, x, y, paintAxisLabels, anchor, angleDegrees) }
}

protected open var renderGridLinesPath: Path = Path()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ import android.graphics.Paint
import android.graphics.Paint.Align
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.text.StaticLayout
import android.text.TextPaint
import com.github.mikephil.charting.utils.Utils.FDEG2RAD
import kotlin.math.abs
import kotlin.math.cos
import kotlin.math.sin

private val mDrawableBoundsCache = Rect()
val DEG2RAD: Double = (Math.PI / 180.0)
val FDEG2RAD: Float = (Math.PI.toFloat() / 180f)

/**
* Utilities class that has some helper methods. Needs to be initialized by
Expand Down Expand Up @@ -117,84 +113,6 @@ fun Canvas.drawXAxisValue(
paint.textAlign = originalTextAlign
}

fun Canvas.drawMultilineText(
textLayout: StaticLayout,
x: Float, y: Float,
paint: TextPaint,
anchor: MPPointF, angleDegrees: Float
) {
var drawOffsetX = 0f
var drawOffsetY = 0f
val drawWidth: Float
val drawHeight: Float

val lineHeight = paint.getFontMetrics(mFontMetricsBuffer)

drawWidth = textLayout.width.toFloat()
drawHeight = textLayout.lineCount * lineHeight

// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left.toFloat()

// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY += drawHeight

// To have a consistent point of reference, we always draw left-aligned
val originalTextAlign = paint.textAlign
paint.textAlign = Align.LEFT

if (angleDegrees != 0f) {
// Move the text drawing rect in a way that it always rotates around its center

drawOffsetX -= drawWidth * 0.5f
drawOffsetY -= drawHeight * 0.5f

var translateX = x
var translateY = y

// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
val rotatedSize = getSizeOfRotatedRectangleByDegrees(
drawWidth,
drawHeight,
angleDegrees
)

translateX -= rotatedSize.width * (anchor.x - 0.5f)
translateY -= rotatedSize.height * (anchor.y - 0.5f)
FSize.recycleInstance(rotatedSize)
}

this.save()
this.translate(translateX, translateY)
this.rotate(angleDegrees)

this.translate(drawOffsetX, drawOffsetY)
textLayout.draw(this)

this.restore()
} else {
if (anchor.x != 0f || anchor.y != 0f) {
drawOffsetX -= drawWidth * anchor.x
drawOffsetY -= drawHeight * anchor.y
}

drawOffsetX += x
drawOffsetY += y

this.save()

this.translate(drawOffsetX, drawOffsetY)
textLayout.draw(this)

this.restore()
}

paint.textAlign = originalTextAlign
}

/**
* Returns a recyclable FSize instance.
* Represents size of a rotated rectangle by degrees.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package com.github.mikephil.charting.utils
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Paint.Align
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.MotionEvent
import android.view.VelocityTracker
import android.view.ViewConfiguration
import com.github.mikephil.charting.formatter.DefaultValueFormatter
import com.github.mikephil.charting.formatter.IValueFormatter
import com.github.mikephil.charting.utils.FSize.Companion.recycleInstance
import com.github.mikephil.charting.utils.MPPointF.Companion.instance
import java.lang.Double
import java.lang.Float
import kotlin.Int
import kotlin.IntArray
Expand All @@ -27,21 +24,14 @@ import kotlin.math.sin
/**
* Utilities class that has some helper methods. Needs to be initialized by
* calling Utils.init(...) before usage. Inside the Chart.init() method, this is
* done, if the Utils are used before that, Utils.init(...) needs to be called
* manually.
*
* @author Philipp Jahoda
* done, if the Utils are used before that, Utils.init(...) needs to be called manually.
*/
object Utils {
var minimumFlingVelocity: Int = 50
var maximumFlingVelocity: Int = 8000
val DEG2RAD: kotlin.Double = (Math.PI / 180.0)
val FDEG2RAD: kotlin.Float = (Math.PI.toFloat() / 180f)
const val DEG2RAD: Double = (Math.PI / 180.0)
const val FDEG2RAD: kotlin.Float = (Math.PI.toFloat() / 180f)

@Suppress("unused")
val DOUBLE_EPSILON: kotlin.Double = Double.longBitsToDouble(1)

@Suppress("unused")
val FLOAT_EPSILON: kotlin.Float = Float.intBitsToFloat(1)

/**
Expand Down Expand Up @@ -231,76 +221,6 @@ object Utils {
canvas.restoreToCount(saveId)
}

private val mDrawTextRectBuffer = Rect()
private val mFontMetricsBuffer = Paint.FontMetrics()

fun drawXAxisValue(
canvas: Canvas, text: String, x: kotlin.Float, y: kotlin.Float,
paint: Paint,
anchor: MPPointF, angleDegrees: kotlin.Float
) {
var drawOffsetX = 0f
var drawOffsetY = 0f

val lineHeight = paint.getFontMetrics(mFontMetricsBuffer)
paint.getTextBounds(text, 0, text.length, mDrawTextRectBuffer)

// Android sometimes has pre-padding
drawOffsetX -= mDrawTextRectBuffer.left.toFloat()

// Android does not snap the bounds to line boundaries,
// and draws from bottom to top.
// And we want to normalize it.
drawOffsetY -= mFontMetricsBuffer.ascent

// To have a consistent point of reference, we always draw left-aligned
val originalTextAlign = paint.textAlign
paint.textAlign = Align.LEFT

if (angleDegrees != 0f) {
// Move the text drawing rect in a way that it always rotates around its center

drawOffsetX -= mDrawTextRectBuffer.width() * 0.5f
drawOffsetY -= lineHeight * 0.5f

var translateX = x
var translateY = y

// Move the "outer" rect relative to the anchor, assuming its centered
if (anchor.x != 0.5f || anchor.y != 0.5f) {
val rotatedSize = getSizeOfRotatedRectangleByDegrees(
mDrawTextRectBuffer.width().toFloat(),
lineHeight,
angleDegrees
)

translateX -= rotatedSize.width * (anchor.x - 0.5f)
translateY -= rotatedSize.height * (anchor.y - 0.5f)
recycleInstance(rotatedSize)
}

canvas.save()
canvas.translate(translateX, translateY)
canvas.rotate(angleDegrees)

canvas.drawText(text, drawOffsetX, drawOffsetY, paint)

canvas.restore()
} else {
if (anchor.x != 0f || anchor.y != 0f) {
drawOffsetX -= mDrawTextRectBuffer.width() * anchor.x
drawOffsetY -= lineHeight * anchor.y
}

drawOffsetX += x
drawOffsetY += y

canvas.drawText(text, drawOffsetX, drawOffsetY, paint)
}

paint.textAlign = originalTextAlign
}

/**
* Returns a recyclable FSize instance.
* Represents size of a rotated rectangle by degrees.
Expand Down
Loading