Skip to content

Commit 6054ebd

Browse files
committed
Kotlin getNormalizedAngle
1 parent 73d467c commit 6054ebd

File tree

10 files changed

+62
-96
lines changed

10 files changed

+62
-96
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.github.mikephil.charting.interfaces.datasets.IPieDataSet;
1818
import com.github.mikephil.charting.renderer.PieChartRenderer;
1919
import com.github.mikephil.charting.utils.MPPointF;
20-
import com.github.mikephil.charting.utils.Utils;
2120
import com.github.mikephil.charting.utils.UtilsKtKt;
2221

2322
import java.util.List;
@@ -344,7 +343,7 @@ public XAxis getXAxis() {
344343
public int getIndexForAngle(float angle) {
345344

346345
// take the current angle of the chart into consideration
347-
float a = Utils.getNormalizedAngle(angle - getRotationAngle());
346+
float a = UtilsKtKt.getNormalizedAngle(angle - getRotationAngle());
348347

349348
for (int i = 0; i < mAbsoluteAngles.length; i++) {
350349
if (mAbsoluteAngles[i] > a)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
2020
import com.github.mikephil.charting.listener.PieRadarChartTouchListener;
2121
import com.github.mikephil.charting.utils.MPPointF;
22-
import com.github.mikephil.charting.utils.Utils;
2322
import com.github.mikephil.charting.utils.UtilsKtKt;
2423

2524
/**
@@ -341,7 +340,7 @@ public float distanceToCenter(float x, float y) {
341340
*/
342341
public void setRotationAngle(float angle) {
343342
mRawRotationAngle = angle;
344-
mRotationAngle = Utils.getNormalizedAngle(mRawRotationAngle);
343+
mRotationAngle = UtilsKtKt.getNormalizedAngle(mRawRotationAngle);
345344
}
346345

347346
/**

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.github.mikephil.charting.renderer.RadarChartRenderer;
1515
import com.github.mikephil.charting.renderer.XAxisRendererRadarChart;
1616
import com.github.mikephil.charting.renderer.YAxisRendererRadarChart;
17-
import com.github.mikephil.charting.utils.Utils;
1817
import com.github.mikephil.charting.utils.UtilsKtKt;
1918

2019
import java.util.List;
@@ -207,7 +206,7 @@ public List<Integer> getLayerColorList() {
207206
public int getIndexForAngle(float angle) {
208207

209208
// take the current angle of the chart into consideration
210-
float a = Utils.getNormalizedAngle(angle - getRotationAngle());
209+
float a = UtilsKtKt.getNormalizedAngle(angle - getRotationAngle());
211210

212211
float sliceangle = getSliceAngle();
213212

MPChartLib/src/main/java/com/github/mikephil/charting/components/Legend.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ else if (wasStacked) {
694694
// grouped forms have null labels
695695
if (label != null) {
696696

697-
mCalculatedLabelSizes.add(Utils.calcTextSize(labelpaint, label));
697+
mCalculatedLabelSizes.add(CanvasUtilsKt.calcTextSize(labelpaint, label));
698698
requiredWidth += drawingForm ? formToTextSpace + formSize : 0.f;
699699
requiredWidth += mCalculatedLabelSizes.get(i).width;
700700
} else {

MPChartLib/src/main/java/com/github/mikephil/charting/data/BaseDataSet.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import com.github.mikephil.charting.formatter.IValueFormatter
1111
import com.github.mikephil.charting.interfaces.datasets.IDataSet
1212
import com.github.mikephil.charting.utils.ColorTemplate
1313
import com.github.mikephil.charting.utils.MPPointF
14-
import com.github.mikephil.charting.utils.Utils
1514
import com.github.mikephil.charting.utils.convertDpToPixel
15+
import com.github.mikephil.charting.utils.getDefaultValueFormatter
1616

1717
/**
1818
* This is the base dataset of all DataSets. It's purpose is to implement critical methods
@@ -237,13 +237,17 @@ abstract class BaseDataSet<T : Entry>() : IDataSet<T> {
237237
}
238238

239239
override fun setValueFormatter(f: IValueFormatter?) {
240-
if (f == null) return
241-
else mValueFormatter = f
240+
if (f == null)
241+
return
242+
else
243+
mValueFormatter = f
242244
}
243245

244246
override fun getValueFormatter(): IValueFormatter {
245-
if (needsFormatter()) return Utils.getDefaultValueFormatter()
246-
return mValueFormatter!!
247+
return if (needsFormatter())
248+
getDefaultValueFormatter()
249+
else
250+
mValueFormatter!!
247251
}
248252

249253
override fun needsFormatter(): Boolean {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.github.mikephil.charting.utils.Transformer
1919
import com.github.mikephil.charting.utils.Utils
2020
import com.github.mikephil.charting.utils.ViewPortHandler
2121
import com.github.mikephil.charting.utils.calcTextHeight
22+
import com.github.mikephil.charting.utils.calcTextSize
2223
import com.github.mikephil.charting.utils.calcTextWidth
2324
import com.github.mikephil.charting.utils.convertDpToPixel
2425
import com.github.mikephil.charting.utils.drawXAxisValue
@@ -73,7 +74,7 @@ open class XAxisRenderer(
7374
paintAxisLabels.typeface = xAxis.typeface
7475
paintAxisLabels.textSize = xAxis.textSize
7576

76-
val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
77+
val labelSize = paintAxisLabels.calcTextSize(longest)
7778

7879
val labelWidth = labelSize.width
7980
val labelHeight = paintAxisLabels.calcTextHeight("Q").toFloat()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.github.mikephil.charting.utils.Transformer
1616
import com.github.mikephil.charting.utils.Utils
1717
import com.github.mikephil.charting.utils.ViewPortHandler
1818
import com.github.mikephil.charting.utils.calcTextHeight
19+
import com.github.mikephil.charting.utils.calcTextSize
1920
import com.github.mikephil.charting.utils.convertDpToPixel
2021
import kotlin.math.roundToInt
2122

@@ -58,7 +59,7 @@ open class XAxisRendererHorizontalBarChart(
5859

5960
val longest = xAxis.longestLabel
6061

61-
val labelSize = Utils.calcTextSize(paintAxisLabels, longest)
62+
val labelSize = paintAxisLabels.calcTextSize(longest)
6263

6364
val labelWidth = (labelSize.width + xAxis.xOffset * 3.5f).toInt().toFloat()
6465
val labelHeight = labelSize.height

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ val FDEG2RAD: Float = (Math.PI.toFloat() / 180f)
2121
*/
2222
fun Paint.calcTextWidth(demoText: String?) = this.measureText(demoText).toInt()
2323

24+
private val calcTextSizeRect = Rect()
25+
26+
/**
27+
* calculates the approximate size of a text, depending on a demo text
28+
* avoid repeated calls (e.g. inside drawing methods)
29+
*
30+
* @param paint
31+
* @param demoText
32+
* @param outputFSize An output variable, modified by the function.
33+
*/
34+
fun Paint.calcTextSize(demoText: String, outputFSize: FSize) {
35+
val r = calcTextSizeRect
36+
r.set(0, 0, 0, 0)
37+
this.getTextBounds(demoText, 0, demoText.length, r)
38+
outputFSize.width = r.width().toFloat()
39+
outputFSize.height = r.height().toFloat()
40+
}
41+
42+
/**
43+
* Returns a recyclable FSize instance.
44+
* calculates the approximate size of a text, depending on a demo text
45+
* avoid repeated calls (e.g. inside drawing methods)
46+
* @return A Recyclable FSize instance
47+
*/
48+
fun Paint.calcTextSize(demoText: String): FSize {
49+
val result = FSize.getInstance(0f, 0f)
50+
this.calcTextSize(demoText, result)
51+
return result
52+
}
53+
2454
/**
2555
* calculates the approximate height of a text, depending on a demo text
2656
* avoid repeated calls (e.g. inside drawing methods)

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

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -67,42 +67,6 @@ public static float getLineSpacing(Paint paint, Paint.FontMetrics fontMetrics) {
6767
return fontMetrics.ascent - fontMetrics.top + fontMetrics.bottom;
6868
}
6969

70-
/**
71-
* Returns a recyclable FSize instance.
72-
* calculates the approximate size of a text, depending on a demo text
73-
* avoid repeated calls (e.g. inside drawing methods)
74-
*
75-
* @param paint
76-
* @param demoText
77-
* @return A Recyclable FSize instance
78-
*/
79-
public static FSize calcTextSize(Paint paint, String demoText) {
80-
81-
FSize result = FSize.getInstance(0, 0);
82-
calcTextSize(paint, demoText, result);
83-
return result;
84-
}
85-
86-
private static final Rect mCalcTextSizeRect = new Rect();
87-
88-
/**
89-
* calculates the approximate size of a text, depending on a demo text
90-
* avoid repeated calls (e.g. inside drawing methods)
91-
*
92-
* @param paint
93-
* @param demoText
94-
* @param outputFSize An output variable, modified by the function.
95-
*/
96-
public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) {
97-
98-
Rect r = mCalcTextSizeRect;
99-
r.set(0, 0, 0, 0);
100-
paint.getTextBounds(demoText, 0, demoText.length(), r);
101-
outputFSize.width = r.width();
102-
outputFSize.height = r.height();
103-
104-
}
105-
10670
/**
10771
* Math.pow(...) is very expensive, so avoid calling it and create it
10872
* yourself.
@@ -111,38 +75,6 @@ public static void calcTextSize(Paint paint, String demoText, FSize outputFSize)
11175
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000
11276
};
11377

114-
private static final IValueFormatter mDefaultValueFormatter = generateDefaultValueFormatter();
115-
116-
private static IValueFormatter generateDefaultValueFormatter() {
117-
return new DefaultValueFormatter(1);
118-
}
119-
120-
/// - returns: The default value formatter used for all chart components that needs a default
121-
public static IValueFormatter getDefaultValueFormatter() {
122-
return mDefaultValueFormatter;
123-
}
124-
125-
/**
126-
* Returns a recyclable MPPointF instance.
127-
* Calculates the position around a center point, depending on the distance
128-
* from the center, and the angle of the position around the center.
129-
*
130-
* @param center
131-
* @param dist
132-
* @param angle in degrees, converted to radians internally
133-
* @return
134-
*/
135-
public static MPPointF getPosition(MPPointF center, float dist, float angle) {
136-
MPPointF p = MPPointF.getInstance(0, 0);
137-
getPosition(center, dist, angle, p);
138-
return p;
139-
}
140-
141-
public static void getPosition(MPPointF center, float dist, float angle, MPPointF outputPoint) {
142-
outputPoint.x = (float) (center.x + dist * Math.cos(Math.toRadians(angle)));
143-
outputPoint.y = (float) (center.y + dist * Math.sin(Math.toRadians(angle)));
144-
}
145-
14678
public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, VelocityTracker tracker) {
14779

14880
// Check the dot product of current velocities.
@@ -169,17 +101,6 @@ public static void velocityTrackerPointerUpCleanUpIfNecessary(MotionEvent ev, Ve
169101
}
170102
}
171103

172-
/**
173-
* returns an angle between 0.f < 360.f (not less than zero, less than 360)
174-
*/
175-
public static float getNormalizedAngle(float angle) {
176-
while (angle < 0.f) {
177-
angle += 360.f;
178-
}
179-
180-
return angle % 360.f;
181-
}
182-
183104
private static final Rect mDrawableBoundsCache = new Rect();
184105

185106
public static void drawImage(Canvas canvas, Drawable drawable, int x, int y) {

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.github.mikephil.charting.utils
22

33
import android.content.Context
4-
import android.graphics.Paint
54
import android.os.Build
65
import android.util.DisplayMetrics
76
import android.util.Log
8-
import android.util.Log.i
97
import android.view.ViewConfiguration
8+
import com.github.mikephil.charting.formatter.DefaultValueFormatter
109
import java.lang.Double
1110
import kotlin.Boolean
1211
import kotlin.Char
1312
import kotlin.CharArray
1413
import kotlin.Float
1514
import kotlin.Int
1615
import kotlin.String
16+
import kotlin.apply
1717
import kotlin.code
1818
import kotlin.math.ceil
1919
import kotlin.math.cos
2020
import kotlin.math.log10
2121
import kotlin.math.pow
2222
import kotlin.math.roundToInt
2323
import kotlin.math.sin
24-
import kotlin.time.times
2524

2625
var metrics: DisplayMetrics? = null
2726
var minimumFlingVelocity = 0
@@ -89,13 +88,26 @@ fun getSDKInt() = Build.VERSION.SDK_INT
8988

9089
fun Context.convertDpToPixel(dp: Float) = dp * this.resources.displayMetrics.density
9190

91+
fun getDefaultValueFormatter() = DefaultValueFormatter(1)
92+
9293
fun MPPointF.getPosition(dist: Float, angle: Float) :MPPointF {
9394
return MPPointF().apply {
9495
x = (this.x + dist * cos(Math.toRadians(angle.toDouble()))).toFloat()
9596
y = (this.y + dist * sin(Math.toRadians(angle.toDouble()))).toFloat()
9697
}
9798
}
9899

100+
/**
101+
* returns an angle between 0.f < 360.f (not less than zero, less than 360)
102+
*/
103+
fun Float.getNormalizedAngle(): Float {
104+
var angle = this
105+
while (angle < 0f) {
106+
angle += 360f
107+
}
108+
return angle % 360f
109+
}
110+
99111
fun Float.formatNumber(digitCount: Int, separateThousands: Boolean, separateChar: Char = '.'): String {
100112
var number = this
101113
var digitCount = digitCount

0 commit comments

Comments
 (0)