diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt index c18906be9..260cd749d 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt @@ -207,12 +207,12 @@ class BarLineChartTouchListener( MotionEvent.ACTION_UP -> { val velocityTracker = mVelocityTracker val pointerId = event.getPointerId(0) - velocityTracker!!.computeCurrentVelocity(1000, Utils.getMaximumFlingVelocity().toFloat()) + velocityTracker!!.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat()) val velocityY = velocityTracker.getYVelocity(pointerId) val velocityX = velocityTracker.getXVelocity(pointerId) - if (abs(velocityX.toDouble()) > Utils.getMinimumFlingVelocity() || - abs(velocityY.toDouble()) > Utils.getMinimumFlingVelocity() + if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity || + abs(velocityY.toDouble()) > Utils.minimumFlingVelocity ) { if (mTouchMode == DRAG && mChart!!.isDragDecelerationEnabled) { stopDeceleration() @@ -225,8 +225,8 @@ class BarLineChartTouchListener( mDecelerationVelocity.x = velocityX mDecelerationVelocity.y = velocityY - Utils.postInvalidateOnAnimation(mChart) // This causes computeScroll to fire, recommended for this by - // Google + // This causes computeScroll to fire, recommended for this by Google + Utils.postInvalidateOnAnimation(mChart!!) } } @@ -249,7 +249,7 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_POINTER_UP -> { - Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, mVelocityTracker) + mVelocityTracker?.let { Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, it) } mTouchMode = POST_ZOOM } @@ -469,10 +469,9 @@ class BarLineChartTouchListener( val vph = mChart!!.viewPortHandler val xTrans = x - vph.offsetLeft() - var yTrans = 0f // check if axis is inverted - yTrans = if (inverted()) { + val yTrans: Float = if (inverted()) { -(y - vph.offsetTop()) } else { -(mChart!!.measuredHeight - y - vph.offsetBottom()) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt index fdafef061..e1ae235c4 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt @@ -12,6 +12,7 @@ import kotlin.math.abs import kotlin.math.ceil import kotlin.math.floor import kotlin.math.log10 +import kotlin.math.nextUp import kotlin.math.pow /** @@ -180,7 +181,10 @@ abstract class AxisRenderer( first -= interval } - val last = if (interval == 0.0) 0.0 else Utils.nextUp(floor(max / interval) * interval) + val last = if (interval == 0.0) + 0.0 + else + (floor(max / interval) * interval).nextUp() var f: Double diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt index 71216de13..d887fd0f1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt @@ -330,14 +330,14 @@ open class BarChartRenderer( px += iconsOffset.x py += iconsOffset.y - Utils.drawImage( - canvas, - icon, - px.toInt(), - py.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + px.toInt(), + py.toInt() + ) + } } j += 4 } @@ -390,14 +390,14 @@ open class BarChartRenderer( px += iconsOffset.x py += iconsOffset.y - Utils.drawImage( - canvas, - icon, - px.toInt(), - py.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + px.toInt(), + py.toInt() + ) + } } // draw stack values @@ -469,14 +469,14 @@ open class BarChartRenderer( if (entry.icon != null && dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - canvas, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt() + ) + } } k += 2 } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt index 733a2fb2d..226f36d45 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt @@ -155,14 +155,14 @@ open class BubbleChartRenderer( if (entry.icon != null && dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - canvas, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt() + ) + } } j += 2 } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt index 1f6730a27..4d05d66af 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt @@ -265,14 +265,14 @@ open class CandleStickChartRenderer( if (entry.icon != null && dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - canvas, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt() + ) + } } j += 2 } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt index 56b2578b7..997e5df94 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt @@ -252,14 +252,14 @@ open class HorizontalBarChartRenderer( px += iconsOffset.x py += iconsOffset.y - Utils.drawImage( - canvas, - icon, - px.toInt(), - py.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + px.toInt(), + py.toInt() + ) + } } j += 4 } @@ -328,14 +328,14 @@ open class HorizontalBarChartRenderer( px += iconsOffset.x py += iconsOffset.y - Utils.drawImage( - canvas, - icon, - px.toInt(), - py.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + px.toInt(), + py.toInt() + ) + } } } else { val transformed = FloatArray(vals.size * 2) @@ -416,14 +416,14 @@ open class HorizontalBarChartRenderer( if (entry.icon != null && dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - canvas, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt() + ) + } } k += 2 } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt index 41826b7d6..7ee1cf473 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt @@ -543,14 +543,14 @@ class LineChartRenderer( if (entry.icon != null && dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - canvas, - icon, - (x + iconsOffset.x).toInt(), - (y + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + (x + iconsOffset.x).toInt(), + (y + iconsOffset.y).toInt() + ) + } } } j += 2 diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt index 74abd0dc4..529fcf6b1 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt @@ -7,7 +7,6 @@ import android.graphics.Paint import android.graphics.Paint.Align import android.graphics.Path import android.graphics.RectF -import android.os.Build import android.text.Layout import android.text.StaticLayout import android.text.TextPaint @@ -31,6 +30,7 @@ import kotlin.math.sin import kotlin.math.sqrt import kotlin.math.tan import androidx.core.graphics.withSave +import androidx.core.graphics.createBitmap open class PieChartRenderer( protected var chart: PieChart, animator: ChartAnimator, @@ -95,7 +95,7 @@ open class PieChartRenderer( || (drawBitmap.height != height) ) { if (width > 0 && height > 0) { - drawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444) + drawBitmap = createBitmap(width, height, Bitmap.Config.ARGB_4444) mDrawBitmap = WeakReference(drawBitmap) bitmapCanvas = Canvas(drawBitmap) } else return @@ -106,7 +106,7 @@ open class PieChartRenderer( val pieData = chart.data for (set in pieData!!.dataSets) { - if (set.isVisible && set.entryCount > 0) drawDataSet(canvas, set) + if (set.isVisible && set.entryCount > 0) drawDataSet(set) } } @@ -171,7 +171,7 @@ open class PieChartRenderer( return sliceSpace } - protected fun drawDataSet(canvas: Canvas?, dataSet: IPieDataSet) { + protected fun drawDataSet(dataSet: IPieDataSet) { var angle = 0f val rotationAngle = chart.rotationAngle @@ -588,14 +588,14 @@ open class PieChartRenderer( var y = (labelRadius + iconsOffset.y) * sliceYBase + center.y y += iconsOffset.x - Utils.drawImage( - this, - icon, - x.toInt(), - y.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + this, + it, + x.toInt(), + y.toInt() + ) + } } xIndex++ @@ -725,12 +725,10 @@ open class PieChartRenderer( val layoutHeight = centerTextLayout!!.height.toFloat() canvas.save() - if (Build.VERSION.SDK_INT >= 18) { - val path = mDrawCenterTextPathBuffer - path.reset() - path.addOval(holeRect, Path.Direction.CW) - canvas.clipPath(path) - } + val path = mDrawCenterTextPathBuffer + path.reset() + path.addOval(holeRect, Path.Direction.CW) + canvas.clipPath(path) canvas.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f) centerTextLayout!!.draw(canvas) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt index 371df7164..7a471f3eb 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt @@ -180,14 +180,14 @@ open class RadarChartRenderer( pIcon.y += iconsOffset.x - Utils.drawImage( - canvas, - icon, - pIcon.x.toInt(), - pIcon.y.toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + pIcon.x.toInt(), + pIcon.y.toInt() + ) + } } } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt index daf916c40..2b0dadde3 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/ScatterChartRenderer.kt @@ -132,14 +132,14 @@ open class ScatterChartRenderer(@JvmField var chart: ScatterDataProvider, animat if (entry.icon != null && dataSet.isDrawIconsEnabled) { val icon = entry.icon - Utils.drawImage( - canvas, - icon, - (positions[j] + iconsOffset.x).toInt(), - (positions[j + 1] + iconsOffset.y).toInt(), - icon!!.intrinsicWidth, - icon.intrinsicHeight - ) + icon?.let { + Utils.drawImage( + canvas, + it, + (positions[j] + iconsOffset.x).toInt(), + (positions[j + 1] + iconsOffset.y).toInt() + ) + } } j += 2 } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt index 364adefae..8081430ad 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRenderer.kt @@ -80,7 +80,6 @@ open class XAxisRenderer( xAxis.labelRotationAngle ) - xAxis.mLabelWidth = labelRotatedSize.width.roundToInt() xAxis.mLabelHeight = labelRotatedSize.height.roundToInt() @@ -158,7 +157,7 @@ open class XAxisRenderer( * * @param pos */ - protected open fun drawLabels(canvas: Canvas?, pos: Float, anchor: MPPointF?) { + protected open fun drawLabels(canvas: Canvas, pos: Float, anchor: MPPointF) { val labelRotationAngleDegrees = xAxis.labelRotationAngle val centeringEnabled = xAxis.isCenterAxisLabelsEnabled @@ -220,7 +219,7 @@ open class XAxisRenderer( } } - protected fun drawLabel(canvas: Canvas?, formattedLabel: String?, x: Float, y: Float, anchor: MPPointF?, angleDegrees: Float) { + protected fun drawLabel(canvas: Canvas, formattedLabel: String?, x: Float, y: Float, anchor: MPPointF, angleDegrees: Float) { Utils.drawXAxisValue(canvas, formattedLabel, x, y, paintAxisLabels, anchor, angleDegrees) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt index 35d05907c..f6389de37 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/XAxisRendererHorizontalBarChart.kt @@ -5,6 +5,7 @@ import android.graphics.Paint import android.graphics.Paint.Align import android.graphics.Path import android.graphics.RectF +import androidx.core.graphics.withSave import com.github.mikephil.charting.components.LimitLine.LimitLabelPosition import com.github.mikephil.charting.components.XAxis import com.github.mikephil.charting.components.XAxis.XAxisPosition @@ -14,7 +15,7 @@ import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Transformer import com.github.mikephil.charting.utils.Utils import com.github.mikephil.charting.utils.ViewPortHandler -import androidx.core.graphics.withSave +import kotlin.math.roundToInt @Suppress("MemberVisibilityCanBePrivate") open class XAxisRendererHorizontalBarChart( @@ -50,7 +51,7 @@ open class XAxisRendererHorizontalBarChart( } override fun computeSize() { - paintAxisLabels.setTypeface(xAxis.typeface) + paintAxisLabels.typeface = xAxis.typeface paintAxisLabels.textSize = xAxis.textSize val longest = xAxis.longestLabel @@ -66,8 +67,8 @@ open class XAxisRendererHorizontalBarChart( xAxis.labelRotationAngle ) - xAxis.mLabelWidth = Math.round(labelRotatedSize.width) - xAxis.mLabelHeight = Math.round(labelRotatedSize.height) + xAxis.mLabelWidth = labelRotatedSize.width.roundToInt() + xAxis.mLabelHeight = labelRotatedSize.height.roundToInt() FSize.recycleInstance(labelRotatedSize) } @@ -77,7 +78,7 @@ open class XAxisRendererHorizontalBarChart( val xOffset = xAxis.xOffset - paintAxisLabels.setTypeface(xAxis.typeface) + paintAxisLabels.typeface = xAxis.typeface paintAxisLabels.textSize = xAxis.textSize paintAxisLabels.color = xAxis.textColor @@ -89,21 +90,25 @@ open class XAxisRendererHorizontalBarChart( pointF.y = 0.5f drawLabels(canvas, viewPortHandler.contentRight() + xOffset, pointF) } + XAxisPosition.TOP_INSIDE -> { pointF.x = 1.0f pointF.y = 0.5f drawLabels(canvas, viewPortHandler.contentRight() - xOffset, pointF) } + XAxisPosition.BOTTOM -> { pointF.x = 1.0f pointF.y = 0.5f drawLabels(canvas, viewPortHandler.contentLeft() - xOffset, pointF) } + XAxisPosition.BOTTOM_INSIDE -> { pointF.x = 1.0f pointF.y = 0.5f drawLabels(canvas, viewPortHandler.contentLeft() + xOffset, pointF) } + else -> { // BOTH SIDED pointF.x = 0.0f pointF.y = 0.5f @@ -117,7 +122,7 @@ open class XAxisRendererHorizontalBarChart( MPPointF.recycleInstance(pointF) } - override fun drawLabels(canvas: Canvas?, pos: Float, anchor: MPPointF?) { + override fun drawLabels(canvas: Canvas, pos: Float, anchor: MPPointF) { val labelRotationAngleDegrees = xAxis.labelRotationAngle val centeringEnabled = xAxis.isCenterAxisLabelsEnabled @@ -199,7 +204,7 @@ open class XAxisRendererHorizontalBarChart( override fun renderLimitLines(canvas: Canvas) { val limitLines = xAxis.limitLines - if (limitLines == null || limitLines.size <= 0) return + if (limitLines == null || limitLines.isEmpty()) return val pts = mRenderLimitLinesBuffer pts[0] = 0f @@ -221,7 +226,7 @@ open class XAxisRendererHorizontalBarChart( limitLinePaint.style = Paint.Style.STROKE limitLinePaint.color = l.lineColor limitLinePaint.strokeWidth = l.lineWidth - limitLinePaint.setPathEffect(l.dashPathEffect) + limitLinePaint.pathEffect = l.dashPathEffect pts[1] = l.limit @@ -230,7 +235,7 @@ open class XAxisRendererHorizontalBarChart( limitLinePath.moveTo(viewPortHandler.contentLeft(), pts[1]) limitLinePath.lineTo(viewPortHandler.contentRight(), pts[1]) - canvas.drawPath(limitLinePath, limitLinePaint!!) + canvas.drawPath(limitLinePath, limitLinePaint) limitLinePath.reset() // c.drawLines(pts, mLimitLinePaint); @@ -239,7 +244,7 @@ open class XAxisRendererHorizontalBarChart( // if drawing the limit-value label is enabled if (label != null && label != "") { limitLinePaint.style = l.textStyle - limitLinePaint.setPathEffect(null) + limitLinePaint.pathEffect = null limitLinePaint.color = l.textColor limitLinePaint.strokeWidth = 0.5f limitLinePaint.textSize = l.textSize @@ -259,6 +264,7 @@ open class XAxisRendererHorizontalBarChart( pts[1] - yOffset + labelLineHeight, limitLinePaint ) } + LimitLabelPosition.RIGHT_BOTTOM -> { limitLinePaint.textAlign = Align.RIGHT canvas.drawText( @@ -267,6 +273,7 @@ open class XAxisRendererHorizontalBarChart( pts[1] + yOffset, limitLinePaint ) } + LimitLabelPosition.LEFT_TOP -> { limitLinePaint.textAlign = Align.LEFT canvas.drawText( @@ -275,6 +282,7 @@ open class XAxisRendererHorizontalBarChart( pts[1] - yOffset + labelLineHeight, limitLinePaint ) } + else -> { limitLinePaint.textAlign = Align.LEFT canvas.drawText( diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt index 7dbed61b1..0f0bc0b25 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt @@ -11,6 +11,7 @@ import kotlin.math.abs import kotlin.math.ceil import kotlin.math.floor import kotlin.math.log10 +import kotlin.math.nextUp import kotlin.math.pow class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, private val chart: RadarChart) : YAxisRenderer(viewPortHandler, yAxis, null) { @@ -77,7 +78,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr first -= interval } - val last = if (interval == 0.0) 0.0 else Utils.nextUp(floor(max / interval) * interval) + val last = if (interval == 0.0) 0.0 else (floor(max / interval) * interval).nextUp() var f: Double @@ -137,9 +138,9 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr override fun renderAxisLabels(canvas: Canvas) { if (!yAxis.isEnabled || !yAxis.isDrawLabelsEnabled) return - paintAxisLabels!!.setTypeface(yAxis.typeface) - paintAxisLabels!!.textSize = yAxis.textSize - paintAxisLabels!!.color = yAxis.textColor + paintAxisLabels.typeface = yAxis.typeface + paintAxisLabels.textSize = yAxis.textSize + paintAxisLabels.color = yAxis.textColor val center = chart.centerOffsets val pOut = MPPointF.getInstance(0f, 0f) @@ -160,7 +161,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr val label = yAxis.getFormattedLabel(j) - canvas.drawText(label, pOut.x + xOffset, pOut.y, paintAxisLabels!!) + canvas.drawText(label, pOut.x + xOffset, pOut.y, paintAxisLabels) } MPPointF.recycleInstance(center) MPPointF.recycleInstance(pOut) @@ -182,9 +183,9 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr if (!l.isEnabled) continue - limitLinePaint!!.color = l.lineColor - limitLinePaint!!.setPathEffect(l.dashPathEffect) - limitLinePaint!!.strokeWidth = l.lineWidth + limitLinePaint.color = l.lineColor + limitLinePaint.pathEffect = l.dashPathEffect + limitLinePaint.strokeWidth = l.lineWidth val r = (l.limit - chart.yChartMin) * factor @@ -200,7 +201,7 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr } limitPath.close() - canvas.drawPath(limitPath, limitLinePaint!!) + canvas.drawPath(limitPath, limitLinePaint) } MPPointF.recycleInstance(center) MPPointF.recycleInstance(pOut) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java index 7a5c49f14..2db5ad322 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java @@ -3,7 +3,6 @@ import android.annotation.SuppressLint; import android.content.Context; -import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; @@ -35,8 +34,8 @@ public abstract class Utils { private static DisplayMetrics mMetrics; - private static int mMinimumFlingVelocity = 50; - private static int mMaximumFlingVelocity = 8000; + public static int minimumFlingVelocity = 50; + public static int maximumFlingVelocity = 8000; public final static double DEG2RAD = (Math.PI / 180.0); public final static float FDEG2RAD = ((float) Math.PI / 180.f); @@ -52,11 +51,10 @@ public abstract class Utils { @SuppressWarnings("deprecation") public static void init(@NonNull Context context) { ViewConfiguration viewConfiguration = ViewConfiguration.get(context); - mMinimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity(); - mMaximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity(); + minimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity(); + maximumFlingVelocity = viewConfiguration.getScaledMaximumFlingVelocity(); - Resources res = context.getResources(); - mMetrics = res.getDisplayMetrics(); + mMetrics = context.getResources().getDisplayMetrics(); } /** @@ -163,12 +161,11 @@ public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) } - /** * Math.pow(...) is very expensive, so avoid calling it and create it * yourself. */ - private static final int[] POW_10 = { + static final int[] POW_10 = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; @@ -183,107 +180,6 @@ public static IValueFormatter getDefaultValueFormatter() { return mDefaultValueFormatter; } - /** - * Formats the given number to the given number of decimals, and returns the - * number as a string, maximum 35 characters. If thousands are separated, the separating - * character is a dot ("."). - * - * @param number - * @param digitCount - * @param separateThousands set this to true to separate thousands values - * @return - */ - public static String formatNumber(float number, int digitCount, boolean separateThousands) { - return formatNumber(number, digitCount, separateThousands, '.'); - } - - /** - * Formats the given number to the given number of decimals, and returns the - * number as a string, maximum 35 characters. - * - * @param number - * @param digitCount - * @param separateThousands set this to true to separate thousands values - * @param separateChar a caracter to be paced between the "thousands" - * @return - */ - public static String formatNumber(float number, int digitCount, boolean separateThousands, - char separateChar) { - - char[] out = new char[35]; - - boolean neg = false; - if (number == 0) { - return "0"; - } - - boolean zero = number < 1 && number > -1; - - if (number < 0) { - neg = true; - number = -number; - } - - if (digitCount > POW_10.length) { - digitCount = POW_10.length - 1; - } - - number *= POW_10[digitCount]; - long lval = Math.round(number); - int ind = out.length - 1; - int charCount = 0; - boolean decimalPointAdded = false; - - while (lval != 0 || charCount < (digitCount + 1)) { - int digit = (int) (lval % 10); - lval = lval / 10; - out[ind--] = (char) (digit + '0'); - charCount++; - - // add decimal point - if (charCount == digitCount) { - out[ind--] = ','; - charCount++; - decimalPointAdded = true; - - // add thousand separators - } else if (separateThousands && lval != 0 && charCount > digitCount) { - - if (decimalPointAdded) { - - if ((charCount - digitCount) % 4 == 0) { - out[ind--] = separateChar; - charCount++; - } - - } else { - - if ((charCount - digitCount) % 4 == 3) { - out[ind--] = separateChar; - charCount++; - } - } - } - } - - // if number around zero (between 1 and -1) - if (zero) { - out[ind--] = '0'; - charCount += 1; - } - - // if the number is negative - if (neg) { - out[ind--] = '-'; - charCount += 1; - } - - int start = out.length - charCount; - - // use this instead of "new String(...)" because of issue < Android 4.0 - return String.valueOf(out, start, out.length - start); - } - /** * rounds the given number to the next significant number * @@ -312,7 +208,6 @@ public static float roundToNextSignificant(double number) { * @return */ public static int getDecimals(float number) { - float i = roundToNextSignificant(number); if (Float.isInfinite(i)) { @@ -322,22 +217,6 @@ public static int getDecimals(float number) { return (int) Math.ceil(-Math.log10(i)) + 2; } - /** - * Replacement for the Math.nextUp(...) method that is only available in - * HONEYCOMB and higher. - * - * @param d - * @return - */ - public static double nextUp(double d) { - if (d == Double.POSITIVE_INFINITY) { - return d; - } else { - d += 0.0d; - return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + ((d >= 0.0d) ? +1L : -1L)); - } - } - /** * Returns a recyclable MPPointF instance. * Calculates the position around a center point, depending on the distance @@ -349,7 +228,6 @@ public static double nextUp(double d) { * @return */ public static MPPointF getPosition(MPPointF center, float dist, float angle) { - MPPointF p = MPPointF.getInstance(0, 0); getPosition(center, dist, angle, p); return p; @@ -360,12 +238,11 @@ public static void getPosition(MPPointF center, float dist, float angle, MPPoint outputPoint.y = (float) (center.y + dist * Math.sin(Math.toRadians(angle))); } - public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, - VelocityTracker tracker) { + public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, VelocityTracker tracker) { // Check the dot product of current velocities. // If the pointer that left was opposing another velocity vector, clear. - tracker.computeCurrentVelocity(1000, mMaximumFlingVelocity); + tracker.computeCurrentVelocity(1000, maximumFlingVelocity); final int upIndex = ev.getActionIndex(); final int id1 = ev.getPointerId(upIndex); final float x1 = tracker.getXVelocity(id1); @@ -398,14 +275,6 @@ public static void postInvalidateOnAnimation(View view) { view.postInvalidateOnAnimation(); } - public static int getMinimumFlingVelocity() { - return mMinimumFlingVelocity; - } - - public static int getMaximumFlingVelocity() { - return mMaximumFlingVelocity; - } - /** * returns an angle between 0.f < 360.f (not less than zero, less than 360) */ @@ -421,8 +290,10 @@ public static float getNormalizedAngle(float angle) { public static void drawImage(Canvas canvas, Drawable drawable, - int x, int y, - int width, int height) { + int x, int y) { + + int width = drawable.getIntrinsicWidth(); + int height = drawable.getIntrinsicHeight(); MPPointF drawOffset = MPPointF.getInstance(); drawOffset.x = x - (width / 2); @@ -445,7 +316,7 @@ public static void drawImage(Canvas canvas, private static final Rect mDrawTextRectBuffer = new Rect(); private static final Paint.FontMetrics mFontMetricsBuffer = new Paint.FontMetrics(); - public static void drawXAxisValue(Canvas c, String text, float x, float y, + public static void drawXAxisValue(Canvas canvas, String text, float x, float y, Paint paint, MPPointF anchor, float angleDegrees) { @@ -488,13 +359,13 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, FSize.recycleInstance(rotatedSize); } - c.save(); - c.translate(translateX, translateY); - c.rotate(angleDegrees); + canvas.save(); + canvas.translate(translateX, translateY); + canvas.rotate(angleDegrees); - c.drawText(text, drawOffsetX, drawOffsetY, paint); + canvas.drawText(text, drawOffsetX, drawOffsetY, paint); - c.restore(); + canvas.restore(); } else { if (anchor.x != 0.f || anchor.y != 0.f) { @@ -505,13 +376,13 @@ public static void drawXAxisValue(Canvas c, String text, float x, float y, drawOffsetX += x; drawOffsetY += y; - c.drawText(text, drawOffsetX, drawOffsetY, paint); + canvas.drawText(text, drawOffsetX, drawOffsetY, paint); } paint.setTextAlign(originalTextAlign); } - public static void drawMultilineText(Canvas c, StaticLayout textLayout, + public static void drawMultilineText(Canvas canvas, StaticLayout textLayout, float x, float y, TextPaint paint, MPPointF anchor, float angleDegrees) { @@ -559,14 +430,14 @@ public static void drawMultilineText(Canvas c, StaticLayout textLayout, FSize.recycleInstance(rotatedSize); } - c.save(); - c.translate(translateX, translateY); - c.rotate(angleDegrees); + canvas.save(); + canvas.translate(translateX, translateY); + canvas.rotate(angleDegrees); - c.translate(drawOffsetX, drawOffsetY); - textLayout.draw(c); + canvas.translate(drawOffsetX, drawOffsetY); + textLayout.draw(canvas); - c.restore(); + canvas.restore(); } else { if (anchor.x != 0.f || anchor.y != 0.f) { @@ -577,12 +448,12 @@ public static void drawMultilineText(Canvas c, StaticLayout textLayout, drawOffsetX += x; drawOffsetY += y; - c.save(); + canvas.save(); - c.translate(drawOffsetX, drawOffsetY); - textLayout.draw(c); + canvas.translate(drawOffsetX, drawOffsetY); + textLayout.draw(canvas); - c.restore(); + canvas.restore(); } paint.setTextAlign(originalTextAlign); diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt index d7f022a2b..296c08238 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt @@ -1,3 +1,90 @@ -package com.github.mikephil.charting.utils; +package com.github.mikephil.charting.utils -fun getSDKInt() = android.os.Build.VERSION.SDK_INT +import android.content.Context +import android.os.Build +import android.util.Log.d +import java.lang.Double +import kotlin.Boolean +import kotlin.Char +import kotlin.CharArray +import kotlin.Float +import kotlin.Int +import kotlin.String +import kotlin.code + +fun getSDKInt() = Build.VERSION.SDK_INT + +fun Context.convertDpToPixel(dp: Float) = dp * this.resources.displayMetrics.density + +fun Float.formatNumber(digitCount: Int, separateThousands: Boolean, separateChar: Char = '.'): String { + var number = this + var digitCount = digitCount + val out = CharArray(35) + + var neg = false + if (number == 0f) { + return "0" + } + + val zero = number < 1 && number > -1 + + if (number < 0) { + neg = true + number = -number + } + + if (digitCount > Utils.POW_10.size) { + digitCount = Utils.POW_10.size - 1 + } + + number *= Utils.POW_10[digitCount].toFloat() + var lval = Math.round(number).toLong() + var ind = out.size - 1 + var charCount = 0 + var decimalPointAdded = false + + while (lval != 0L || charCount < (digitCount + 1)) { + val digit = (lval % 10).toInt() + lval = lval / 10 + out[ind--] = (digit + '0'.code).toChar() + charCount++ + + // add decimal point + if (charCount == digitCount) { + out[ind--] = ',' + charCount++ + decimalPointAdded = true + + // add thousand separators + } else if (separateThousands && lval != 0L && charCount > digitCount) { + if (decimalPointAdded) { + if ((charCount - digitCount) % 4 == 0) { + out[ind--] = separateChar + charCount++ + } + } else { + if ((charCount - digitCount) % 4 == 3) { + out[ind--] = separateChar + charCount++ + } + } + } + } + + // if number around zero (between 1 and -1) + if (zero) { + out[ind--] = '0' + charCount += 1 + } + + // if the number is negative + if (neg) { + out[ind--] = '-' + charCount += 1 + } + + val start = out.size - charCount + + // use this instead of "new String(...)" because of issue < Android 4.0 + return String(out, start, out.size - start) +} diff --git a/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt b/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt index ffae0e0c7..ee70b0da2 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt @@ -8,7 +8,7 @@ import com.github.mikephil.charting.data.CandleEntry import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.utils.MPPointF -import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.formatNumber import info.appdev.chartexample.R /** @@ -22,9 +22,9 @@ class MyMarkerView(context: Context?, layoutResource: Int) : MarkerView(context, // content (user-interface) override fun refreshContent(e: Entry, highlight: Highlight?) { if (e is CandleEntry) { - tvContent.text = Utils.formatNumber(e.high, 0, true) + tvContent.text = e.high.formatNumber(0, true) } else { - tvContent.text = Utils.formatNumber(e.y, 0, true) + tvContent.text = e.y.formatNumber(0, true) } super.refreshContent(e, highlight) diff --git a/app/src/main/kotlin/info/appdev/chartexample/custom/StackedBarsMarkerView.kt b/app/src/main/kotlin/info/appdev/chartexample/custom/StackedBarsMarkerView.kt index 74bebe2a3..18b23126d 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/custom/StackedBarsMarkerView.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/custom/StackedBarsMarkerView.kt @@ -9,6 +9,7 @@ import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.utils.MPPointF import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.formatNumber import info.appdev.chartexample.R /** @@ -26,12 +27,12 @@ class StackedBarsMarkerView(context: Context?, layoutResource: Int) : MarkerView if (entry.yVals != null) { // draw the stack value - tvContent.text = Utils.formatNumber(entry.yVals!![highlight.stackIndex], 0, true) + tvContent.text = entry.yVals!![highlight.stackIndex].formatNumber( 0, true) } else { - tvContent.text = Utils.formatNumber(entry.y, 0, true) + tvContent.text = entry.y.formatNumber( 0, true) } } else { - tvContent.text = Utils.formatNumber(entry.y, 0, true) + tvContent.text = entry.y.formatNumber( 0, true) } super.refreshContent(entry, highlight)