Skip to content

Commit fc76f35

Browse files
committed
Kotlin formatter
1 parent 39290ef commit fc76f35

14 files changed

+330
-469
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.mikephil.charting.formatter
2+
3+
import com.github.mikephil.charting.components.AxisBase
4+
import java.text.DecimalFormat
5+
6+
open class DefaultAxisValueFormatter(digits: Int) : IAxisValueFormatter {
7+
/**
8+
* decimal format for formatting
9+
*/
10+
protected var decimalFormat: DecimalFormat
11+
/**
12+
* Returns the number of decimal digits this formatter uses or -1, if unspecified.
13+
*/
14+
/**
15+
* the number of decimal digits this formatter uses
16+
*/
17+
var decimalDigits = 0
18+
protected set
19+
20+
/**
21+
* Constructor that specifies to how many digits the value should be
22+
* formatted.
23+
*/
24+
init {
25+
decimalDigits = digits
26+
val b = StringBuffer()
27+
for (i in 0 until digits) {
28+
if (i == 0) b.append(".")
29+
b.append("0")
30+
}
31+
decimalFormat = DecimalFormat("###,###,###,##0$b")
32+
}
33+
34+
override fun getFormattedValue(value: Float, axis: AxisBase?): String? {
35+
// avoid memory allocations here (for performance)
36+
return decimalFormat.format(value.toDouble())
37+
}
38+
}

MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultFillFormatter.java

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.github.mikephil.charting.formatter
2+
3+
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider
4+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
5+
6+
/**
7+
* Default formatter that calculates the position of the filled line.
8+
*/
9+
open class DefaultFillFormatter : IFillFormatter {
10+
11+
override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float {
12+
val fillMin: Float
13+
val chartMaxY = dataProvider!!.yChartMax
14+
val chartMinY = dataProvider.yChartMin
15+
val data = dataProvider.lineData
16+
fillMin = if (dataSet!!.yMax > 0 && dataSet.yMin < 0) {
17+
0f
18+
} else {
19+
val max: Float = if (data.yMax > 0) 0f else chartMaxY
20+
val min: Float = if (data.yMin < 0) 0f else chartMinY
21+
if (dataSet.yMin >= 0)
22+
min
23+
else
24+
max
25+
}
26+
return fillMin
27+
}
28+
}

MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultValueFormatter.java

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.github.mikephil.charting.formatter
2+
3+
import com.github.mikephil.charting.data.Entry
4+
import com.github.mikephil.charting.utils.ViewPortHandler
5+
import java.text.DecimalFormat
6+
7+
/**
8+
* Default formatter used for formatting values inside the chart. Uses a DecimalFormat with
9+
* pre-calculated number of digits (depending on max and min value).
10+
*/
11+
open class DefaultValueFormatter(digits: Int) : IValueFormatter {
12+
/**
13+
* DecimalFormat for formatting
14+
*/
15+
protected var decimalFormat: DecimalFormat? = null
16+
17+
/**
18+
* Returns the number of decimal digits this formatter uses.
19+
*
20+
* @return
21+
*/
22+
var decimalDigits = 0
23+
protected set
24+
25+
/**
26+
* Constructor that specifies to how many digits the value should be formatted.
27+
*/
28+
init {
29+
setup(digits)
30+
}
31+
32+
/**
33+
* Sets up the formatter with a given number of decimal digits.
34+
*
35+
* @param digits
36+
*/
37+
fun setup(digits: Int) {
38+
decimalDigits = digits
39+
val b = StringBuffer()
40+
for (i in 0 until digits) {
41+
if (i == 0) b.append(".")
42+
b.append("0")
43+
}
44+
decimalFormat = DecimalFormat("###,###,###,##0$b")
45+
}
46+
47+
override fun getFormattedValue(value: Float, entry: Entry?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String? {
48+
49+
// put more logic here ...
50+
// avoid memory allocations here (for performance reasons)
51+
return decimalFormat!!.format(value.toDouble())
52+
}
53+
}

MPChartLib/src/main/java/com/github/mikephil/charting/formatter/IndexAxisValueFormatter.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)