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
20 changes: 10 additions & 10 deletions chartLib/src/main/kotlin/info/appdev/charting/components/Legend.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import android.graphics.DashPathEffect
import android.graphics.Paint
import info.appdev.charting.utils.ColorTemplate
import info.appdev.charting.utils.FSize
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.calcTextHeight
import info.appdev.charting.utils.calcTextWidth
import info.appdev.charting.utils.convertDpToPixel
import info.appdev.charting.utils.calcTextSize
import info.appdev.charting.utils.getLineHeight
import info.appdev.charting.utils.getLineSpacing
import kotlin.Array
import kotlin.Boolean
import kotlin.IntArray
Expand Down Expand Up @@ -203,7 +206,7 @@ class Legend() : ComponentBase() {

val label = entry.label ?: continue

val length = Utils.calcTextWidth(p, label).toFloat()
val length = p.calcTextWidth(label).toFloat()

if (length > max) max = length
}
Expand Down Expand Up @@ -309,16 +312,13 @@ class Legend() : ComponentBase() {
/**
* the total width of the legend (needed width space)
*/
@JvmField
var neededWidth: Float = 0f

/**
* the total height of the legend (needed height space)
*/
@JvmField
var neededHeight: Float = 0f

@JvmField
var mTextHeightMax: Float = 0f

var mTextWidthMax: Float = 0f
Expand Down Expand Up @@ -375,7 +375,7 @@ class Legend() : ComponentBase() {
var maxWidth = 0f
var maxHeight = 0f
var width = 0f
val labelLineHeight = Utils.getLineHeight(labelpaint)
val labelLineHeight = labelpaint.getLineHeight()
var wasStacked = false

var i = 0
Expand Down Expand Up @@ -407,7 +407,7 @@ class Legend() : ComponentBase() {
wasStacked = false
}

width += Utils.calcTextWidth(labelpaint, label).toFloat()
width += labelpaint.calcTextWidth(label).toFloat()

maxHeight += labelLineHeight + yEntrySpace
} else {
Expand All @@ -425,8 +425,8 @@ class Legend() : ComponentBase() {
}

LegendOrientation.HORIZONTAL -> {
val labelLineHeight = Utils.getLineHeight(labelpaint)
val labelLineSpacing = Utils.getLineSpacing(labelpaint) + yEntrySpace
val labelLineHeight = labelpaint.getLineHeight()
val labelLineSpacing = labelpaint.getLineSpacing() + yEntrySpace
val contentWidth = viewPortHandler.contentWidth() * this.maxSizePercent

// Start calculating layout
Expand Down Expand Up @@ -462,7 +462,7 @@ class Legend() : ComponentBase() {

// grouped forms have null labels
if (label != null) {
calculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label))
calculatedLabelSizes.add(labelpaint.calcTextSize(label))
requiredWidth += if (drawingForm) formToTextSpace + formSize else 0f
requiredWidth += calculatedLabelSizes.get(i)!!.width
} else {
Expand Down
24 changes: 6 additions & 18 deletions chartLib/src/main/kotlin/info/appdev/charting/components/YAxis.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package info.appdev.charting.components

import android.graphics.Color
import android.graphics.Paint
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.calcTextHeight
import info.appdev.charting.utils.calcTextWidth
import info.appdev.charting.utils.convertDpToPixel
import kotlin.math.abs
import kotlin.math.max
Expand Down Expand Up @@ -52,30 +52,18 @@ class YAxis : AxisBase {
var isDrawZeroLineEnabled: Boolean = false
protected set

/**
* Returns true if autoscale restriction for axis min value is enabled
*/
/**
* Sets autoscale restriction for axis min value as enabled/disabled
*/
/**
* flag indicating that auto scale min restriction should be used
*/
@get:Deprecated("")
@set:Deprecated("")
@get:Deprecated("flag indicating that auto scale min restriction should be used")
@set:Deprecated("flag indicating that auto scale min restriction should be used")
var isUseAutoScaleMinRestriction: Boolean = false

/**
* Returns true if autoscale restriction for axis max value is enabled
*/
/**
* Sets autoscale restriction for axis max value as enabled/disabled
*/
/**
* flag indicating that auto scale max restriction should be used
*/
@get:Deprecated("")
@set:Deprecated("")
@get:Deprecated("flag indicating that auto scale max restriction should be used")
@set:Deprecated("flag indicating that auto scale max restriction should be used")
var isUseAutoScaleMaxRestriction: Boolean = false

/**
Expand Down Expand Up @@ -240,7 +228,7 @@ class YAxis : AxisBase {
p.textSize = mTextSize

val label = getLongestLabel(p)
var width = Utils.calcTextWidth(p, label).toFloat() + xOffset * 2f
var width = p.calcTextWidth(label).toFloat() + xOffset * 2f

var minWidth = this.minWidth
var maxWidth = this.maxWidth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import info.appdev.charting.utils.Transformer
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.calcTextHeight
import info.appdev.charting.utils.calcTextWidth
import info.appdev.charting.utils.convertDpToPixel
import kotlin.math.ceil
import kotlin.math.min
Expand Down Expand Up @@ -225,7 +226,7 @@ open class HorizontalBarChartRenderer(
val valueY = barEntry.y
val formattedValue = formatter.getFormattedValue(valueY, barEntry, i, viewPortHandler)
// calculate the correct offset depending on the draw position of the value
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
negOffset = ((if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)
- (buffer.buffer[j + 2] - buffer.buffer[j]))
Expand Down Expand Up @@ -302,7 +303,7 @@ open class HorizontalBarChartRenderer(
)

// calculate the correct offset depending on the draw position of the value
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)

Expand Down Expand Up @@ -380,7 +381,7 @@ open class HorizontalBarChartRenderer(
)

// calculate the correct offset depending on the draw position of the value
val valueTextWidth = Utils.calcTextWidth(paintValues, formattedValue).toFloat()
val valueTextWidth = paintValues.calcTextWidth(formattedValue).toFloat()
posOffset = (if (drawValueAboveBar) valueOffsetPlus else -(valueTextWidth + valueOffsetPlus))
negOffset = (if (drawValueAboveBar) -(valueTextWidth + valueOffsetPlus) else valueOffsetPlus)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import info.appdev.charting.interfaces.datasets.IBarDataSet
import info.appdev.charting.interfaces.datasets.ICandleDataSet
import info.appdev.charting.interfaces.datasets.IPieDataSet
import info.appdev.charting.utils.ColorTemplate
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.calcTextHeight
import info.appdev.charting.utils.calcTextWidth
import info.appdev.charting.utils.convertDpToPixel
import info.appdev.charting.utils.getLineHeight
import info.appdev.charting.utils.getLineSpacing
import java.util.Collections
import kotlin.math.min

Expand Down Expand Up @@ -207,8 +209,8 @@ open class LegendRenderer(
labelPaint.textSize = legend.textSize
labelPaint.color = legend.textColor

val labelLineHeight = Utils.getLineHeight(labelPaint, legendFontMetrics)
val labelLineSpacing = (Utils.getLineSpacing(labelPaint, legendFontMetrics)
val labelLineHeight = labelPaint.getLineHeight(legendFontMetrics)
val labelLineSpacing = (labelPaint.getLineSpacing(legendFontMetrics)
+ legend.yEntrySpace.convertDpToPixel())
val formYOffset = labelLineHeight - labelPaint.calcTextHeight("ABC") / 2f

Expand Down Expand Up @@ -391,7 +393,7 @@ open class LegendRenderer(
-formToTextSpace
else if (wasStacked) posX = originPosX

if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= Utils.calcTextWidth(labelPaint, entry.label).toFloat()
if (direction == LegendDirection.RIGHT_TO_LEFT) posX -= labelPaint.calcTextWidth(entry.label).toFloat()

if (!wasStacked) {
drawLabel(canvas, posX, posY + labelLineHeight, entry.label)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import info.appdev.charting.utils.Transformer
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.calcTextHeight
import info.appdev.charting.utils.calcTextSize
import info.appdev.charting.utils.calcTextWidth
import info.appdev.charting.utils.convertDpToPixel
import info.appdev.charting.utils.drawXAxisValue
import kotlin.math.roundToInt
Expand Down Expand Up @@ -71,7 +73,7 @@ open class XAxisRenderer(
paintAxisLabels.typeface = xAxis.typeface
paintAxisLabels.textSize = xAxis.textSize

val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
val labelSize = paintAxisLabels.calcTextSize(longest)

val labelWidth = labelSize.width
val labelHeight = paintAxisLabels.calcTextHeight("Q").toFloat()
Expand Down Expand Up @@ -208,15 +210,15 @@ open class XAxisRenderer(
// avoid clipping of the last

if (i / 2 == xAxis.entryCount - 1 && xAxis.entryCount > 1) {
val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat()
val width = paintAxisLabels.calcTextWidth(label).toFloat()

if (width > viewPortHandler.offsetRight() * 2
&& x + width > viewPortHandler.chartWidth
) x -= width / 2

// avoid clipping of the first
} else if (i == 0) {
val width = Utils.calcTextWidth(paintAxisLabels, label).toFloat()
val width = paintAxisLabels.calcTextWidth(label).toFloat()
x += width / 2
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import info.appdev.charting.utils.Transformer
import info.appdev.charting.utils.Utils
import info.appdev.charting.utils.ViewPortHandler
import info.appdev.charting.utils.calcTextHeight
import info.appdev.charting.utils.calcTextSize
import info.appdev.charting.utils.convertDpToPixel
import kotlin.math.roundToInt

Expand Down Expand Up @@ -58,7 +59,7 @@ open class XAxisRendererHorizontalBarChart(

val longest = xAxis.longestLabel

val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
val labelSize = paintAxisLabels.calcTextSize(longest)

val labelWidth = (labelSize.width + xAxis.xOffset * 3.5f).toInt().toFloat()
val labelHeight = labelSize.height
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ fun Canvas.drawXAxisValue(
paint.textAlign = originalTextAlign
}

/**
* calculates the approximate width of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*/
fun Paint.calcTextWidth(demoText: String?): Int {
return measureText(demoText).toInt()
}

/**
* Returns a recyclable FSize instance.
* Represents size of a rotated rectangle by degrees.
Expand Down
63 changes: 0 additions & 63 deletions chartLib/src/main/kotlin/info/appdev/charting/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package info.appdev.charting.utils

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.MotionEvent
Expand All @@ -13,7 +12,6 @@ import info.appdev.charting.formatter.IValueFormatter
import info.appdev.charting.utils.MPPointF.Companion.instance
import kotlin.Int
import kotlin.IntArray
import kotlin.String
import kotlin.Suppress
import kotlin.intArrayOf
import kotlin.math.abs
Expand Down Expand Up @@ -43,67 +41,6 @@ object Utils {
maximumFlingVelocity = viewConfiguration.scaledMaximumFlingVelocity
}

/**
* calculates the approximate width of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*/
fun calcTextWidth(paint: Paint, demoText: String?): Int {
return paint.measureText(demoText).toInt()
}

private val mFontMetrics = Paint.FontMetrics()

fun getLineHeight(paint: Paint): Float {
return getLineHeight(paint, mFontMetrics)
}

fun getLineHeight(paint: Paint, fontMetrics: Paint.FontMetrics): Float {
paint.getFontMetrics(fontMetrics)
return fontMetrics.descent - fontMetrics.ascent
}

fun getLineSpacing(paint: Paint): Float {
return getLineSpacing(paint, mFontMetrics)
}

fun getLineSpacing(paint: Paint, fontMetrics: Paint.FontMetrics): Float {
paint.getFontMetrics(fontMetrics)
return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom
}

/**
* Returns a recyclable FSize instance.
* calculates the approximate size of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*
* @param paint
* @param demoText
* @return A Recyclable FSize instance
*/
fun calcTextSize(paint: Paint, demoText: String): FSize {
val result = FSize.getInstance(0f, 0f)
calcTextSize(paint, demoText, result)
return result
}

private val mCalcTextSizeRect = Rect()

/**
* calculates the approximate size of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*
* @param paint
* @param demoText
* @param outputFSize An output variable, modified by the function.
*/
fun calcTextSize(paint: Paint, demoText: String, outputFSize: FSize) {
val r = mCalcTextSizeRect
r.set(0, 0, 0, 0)
paint.getTextBounds(demoText, 0, demoText.length, r)
outputFSize.width = r.width().toFloat()
outputFSize.height = r.height().toFloat()
}

/**
* Math.pow(...) is very expensive, so avoid calling it and create it
* yourself.
Expand Down
Loading
Loading