Skip to content

Commit 8c48095

Browse files
committed
More Utils to Kotlin
1 parent 49c5d0b commit 8c48095

File tree

7 files changed

+62
-45
lines changed

7 files changed

+62
-45
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/charts/Chart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ protected void setupDefaultFormatter(float min, float max) {
366366
reference = Math.abs(max - min);
367367
}
368368

369-
int digits = Utils.getDecimals(reference);
369+
int digits = UtilsKtKt.getDecimals(reference);
370370

371371
// setup the formatter with a new number of digits
372372
mDefaultValueFormatter.setup(digits);

MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class BarLineChartTouchListener(
222222
decelerationVelocity.y = velocityY
223223

224224
// This causes computeScroll to fire, recommended for this by Google
225-
Utils.postInvalidateOnAnimation(chart!!)
225+
chart?.postInvalidateOnAnimation()
226226
}
227227
}
228228

@@ -598,13 +598,14 @@ class BarLineChartTouchListener(
598598

599599
decelerationLastTime = currentTime
600600

601-
if (abs(decelerationVelocity.x.toDouble()) >= 0.01 || abs(decelerationVelocity.y.toDouble()) >= 0.01) Utils.postInvalidateOnAnimation(chart) // This causes computeScroll to fire, recommended for this by Google
601+
if (abs(decelerationVelocity.x.toDouble()) >= 0.01 || abs(decelerationVelocity.y.toDouble()) >= 0.01)
602+
chart?.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google
602603
else {
603604
// Range might have changed, which means that Y-axis labels
604605
// could have changed in size, affecting Y-axis size.
605606
// So we need to recalculate offsets.
606-
chart!!.calculateOffsets()
607-
chart!!.postInvalidate()
607+
chart?.calculateOffsets()
608+
chart?.postInvalidate()
608609

609610
stopDeceleration()
610611
}

MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe
8383
if (decelerationAngularVelocity != 0f) {
8484
decelerationLastTime = AnimationUtils.currentAnimationTimeMillis()
8585

86-
Utils.postInvalidateOnAnimation(chart) // This causes computeScroll to fire, recommended for this by Google
86+
chart?.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google
8787
}
8888
}
8989

@@ -242,7 +242,7 @@ class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListe
242242
decelerationLastTime = currentTime
243243

244244
if (abs(decelerationAngularVelocity) >= 0.001) {
245-
Utils.postInvalidateOnAnimation(chart) // This causes computeScroll to fire, recommended for this by Google
245+
chart?.postInvalidateOnAnimation() // This causes computeScroll to fire, recommended for this by Google
246246
} else {
247247
stopDeceleration()
248248
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.github.mikephil.charting.utils.MPPointD
88
import com.github.mikephil.charting.utils.Transformer
99
import com.github.mikephil.charting.utils.Utils
1010
import com.github.mikephil.charting.utils.ViewPortHandler
11+
import com.github.mikephil.charting.utils.roundToNextSignificant
1112
import kotlin.math.abs
1213
import kotlin.math.ceil
1314
import kotlin.math.floor
@@ -134,14 +135,14 @@ abstract class AxisRenderer(
134135

135136
// Find out how much spacing (in y value space) between axis values
136137
val rawInterval = range / labelCount
137-
var interval = Utils.roundToNextSignificant(rawInterval).toDouble()
138+
var interval = rawInterval.roundToNextSignificant().toDouble()
138139

139140
// If granularity is enabled, then do not allow the interval to go below specified granularity.
140141
// This is used to avoid repeated values when rounding values for display.
141142
if (axis.isGranularityEnabled) interval = if (interval < axis.granularity) axis.granularity.toDouble() else interval
142143

143144
// Normalize interval
144-
val intervalMagnitude = Utils.roundToNextSignificant(10.0.pow(log10(interval).toInt().toDouble())).toDouble()
145+
val intervalMagnitude = 10.0.pow(log10(interval).toInt().toDouble()).roundToNextSignificant().toDouble()
145146
val intervalSigDigit = (interval / intervalMagnitude).toInt()
146147
if (intervalSigDigit > 5) {
147148
// Use one order of magnitude higher, to avoid intervals like 0.9 or 90

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/YAxisRendererRadarChart.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.github.mikephil.charting.components.YAxis
77
import com.github.mikephil.charting.utils.MPPointF
88
import com.github.mikephil.charting.utils.Utils
99
import com.github.mikephil.charting.utils.ViewPortHandler
10+
import com.github.mikephil.charting.utils.roundToNextSignificant
1011
import kotlin.math.abs
1112
import kotlin.math.ceil
1213
import kotlin.math.floor
@@ -32,14 +33,14 @@ class YAxisRendererRadarChart(viewPortHandler: ViewPortHandler, yAxis: YAxis, pr
3233

3334
// Find out how much spacing (in y value space) between axis values
3435
val rawInterval = range / labelCount
35-
var interval = Utils.roundToNextSignificant(rawInterval).toDouble()
36+
var interval = rawInterval.roundToNextSignificant().toDouble()
3637

3738
// If granularity is enabled, then do not allow the interval to go below specified granularity.
3839
// This is used to avoid repeated values when rounding values for display.
3940
if (axis.isGranularityEnabled) interval = if (interval < axis.granularity) axis.granularity.toDouble() else interval
4041

4142
// Normalize interval
42-
val intervalMagnitude = Utils.roundToNextSignificant(10.0.pow(log10(interval).toInt().toDouble())).toDouble()
43+
val intervalMagnitude = 10.0.pow(log10(interval).toInt().toDouble()).roundToNextSignificant().toDouble()
4344
val intervalSigDigit = (interval / intervalMagnitude).toInt()
4445
if (intervalSigDigit > 5) {
4546
// Use one order of magnitude higher, to avoid intervals like 0.9 or 90

MPChartLib/src/main/java/com/github/mikephil/charting/utils/Utils.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
package com.github.mikephil.charting.utils;
33

4-
import android.annotation.SuppressLint;
54
import android.content.Context;
65
import android.graphics.Canvas;
76
import android.graphics.Paint;
@@ -11,10 +10,8 @@
1110
import android.text.StaticLayout;
1211
import android.text.TextPaint;
1312
import android.util.DisplayMetrics;
14-
import android.util.Log;
1513
import android.view.MotionEvent;
1614
import android.view.VelocityTracker;
17-
import android.view.View;
1815
import android.view.ViewConfiguration;
1916

2017
import com.github.mikephil.charting.formatter.DefaultValueFormatter;
@@ -182,23 +179,6 @@ public static float roundToNextSignificant(double number) {
182179
return shifted / magnitude;
183180
}
184181

185-
/**
186-
* Returns the appropriate number of decimals to be used for the provided
187-
* number.
188-
*
189-
* @param number
190-
* @return
191-
*/
192-
public static int getDecimals(float number) {
193-
float i = roundToNextSignificant(number);
194-
195-
if (Float.isInfinite(i)) {
196-
return 0;
197-
}
198-
199-
return (int) Math.ceil(-Math.log10(i)) + 2;
200-
}
201-
202182
/**
203183
* Returns a recyclable MPPointF instance.
204184
* Calculates the position around a center point, depending on the distance
@@ -246,17 +226,6 @@ public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, Ve
246226
}
247227
}
248228

249-
/**
250-
* Original method view.postInvalidateOnAnimation() only supportd in API >=
251-
* 16, This is a replica of the code from ViewCompat.
252-
*
253-
* @param view
254-
*/
255-
@SuppressLint("NewApi")
256-
public static void postInvalidateOnAnimation(View view) {
257-
view.postInvalidateOnAnimation();
258-
}
259-
260229
/**
261230
* returns an angle between 0.f < 360.f (not less than zero, less than 360)
262231
*/
@@ -270,9 +239,7 @@ public static float getNormalizedAngle(float angle) {
270239

271240
private static final Rect mDrawableBoundsCache = new Rect();
272241

273-
public static void drawImage(Canvas canvas,
274-
Drawable drawable,
275-
int x, int y) {
242+
public static void drawImage(Canvas canvas, Drawable drawable, int x, int y) {
276243

277244
int width = drawable.getIntrinsicWidth();
278245
int height = drawable.getIntrinsicHeight();

MPChartLib/src/main/java/com/github/mikephil/charting/utils/UtilsKt.kt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ import android.content.Context
44
import android.os.Build
55
import android.util.DisplayMetrics
66
import android.util.Log
7+
import android.util.Log.i
78
import android.view.ViewConfiguration
9+
import java.lang.Double
10+
import kotlin.Boolean
11+
import kotlin.Char
12+
import kotlin.CharArray
13+
import kotlin.Float
14+
import kotlin.Int
15+
import kotlin.String
16+
import kotlin.code
17+
import kotlin.math.ceil
18+
import kotlin.math.log10
19+
import kotlin.math.pow
20+
import kotlin.math.roundToInt
21+
import kotlin.time.times
822

923
var metrics: DisplayMetrics? = null
1024
var minimumFlingVelocity = 0
@@ -18,6 +32,39 @@ fun Context.initUtils() {
1832
metrics = this.resources.displayMetrics
1933
}
2034

35+
/**
36+
* Returns the appropriate number of decimals to be used for the provided number.
37+
*/
38+
fun Float.getDecimals(): Int {
39+
val i = this.toDouble().roundToNextSignificant()
40+
41+
if (java.lang.Float.isInfinite(i)) {
42+
return 0
43+
}
44+
45+
return ceil(-log10(i.toDouble())).toInt() + 2
46+
}
47+
48+
/**
49+
* rounds the given number to the next significant number
50+
*
51+
* @param number
52+
* @return
53+
*/
54+
fun kotlin.Double.roundToNextSignificant(): Float {
55+
if (Double.isInfinite(this) ||
56+
Double.isNaN(this) || this == 0.0
57+
) {
58+
return 0f
59+
}
60+
61+
val d = ceil(log10(if (this < 0) -this else this).toFloat().toDouble()).toFloat()
62+
val pw = 1 - d.toInt()
63+
val magnitude = 10.0.pow(pw.toDouble()).toFloat()
64+
val shifted = (this * magnitude).roundToInt()
65+
return shifted / magnitude
66+
}
67+
2168
/**
2269
* This method converts dp unit to equivalent pixels, depending on device
2370
* density. NEEDS UTILS TO BE INITIALIZED BEFORE USAGE.

0 commit comments

Comments
 (0)