Skip to content

Commit c0e3816

Browse files
authored
Merge pull request #224 from AppDevNext/Kotlinformatter
More Kotlin formatter
2 parents 8b3bacc + cbcc3d1 commit c0e3816

11 files changed

Lines changed: 150 additions & 277 deletions

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/custom/DayAxisValueFormatter.java

Lines changed: 0 additions & 139 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.xxmassdeveloper.mpchartexample.custom
2+
3+
import com.github.mikephil.charting.charts.BarLineChartBase
4+
import com.github.mikephil.charting.components.AxisBase
5+
import com.github.mikephil.charting.formatter.IAxisValueFormatter
6+
import kotlin.math.max
7+
8+
class DayAxisValueFormatter(private val chart: BarLineChartBase<*>) : IAxisValueFormatter {
9+
private val months = arrayOf(
10+
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
11+
)
12+
13+
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
14+
val days = value.toInt()
15+
16+
val year = determineYear(days)
17+
18+
val month = determineMonth(days)
19+
val monthName = months[month % months.size]
20+
val yearName = year.toString()
21+
22+
if (chart.visibleXRange > 30 * 6) {
23+
return "$monthName $yearName"
24+
} else {
25+
val dayOfMonth = determineDayOfMonth(days, month + 12 * (year - 2016))
26+
27+
var appendix = "th"
28+
29+
when (dayOfMonth) {
30+
1 -> appendix = "st"
31+
2 -> appendix = "nd"
32+
3 -> appendix = "rd"
33+
21 -> appendix = "st"
34+
22 -> appendix = "nd"
35+
23 -> appendix = "rd"
36+
31 -> appendix = "st"
37+
}
38+
return if (dayOfMonth == 0) "" else "$dayOfMonth$appendix $monthName"
39+
}
40+
}
41+
42+
private fun getDaysForMonth(month: Int, year: Int): Int {
43+
// month is 0-based
44+
45+
if (month == 1) {
46+
var is29Feb = false
47+
48+
if (year < 1582) is29Feb = (if (year < 1) year + 1 else year) % 4 == 0
49+
else if (year > 1582) is29Feb = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
50+
51+
return if (is29Feb) 29 else 28
52+
}
53+
54+
return if (month == 3 || month == 5 || month == 8 || month == 10) 30
55+
else 31
56+
}
57+
58+
private fun determineMonth(dayOfYear: Int): Int {
59+
var month = -1
60+
var days = 0
61+
62+
while (days < dayOfYear) {
63+
month += 1
64+
65+
if (month >= 12) month = 0
66+
67+
val year = determineYear(days)
68+
days += getDaysForMonth(month, year)
69+
}
70+
71+
return max(month.toDouble(), 0.0).toInt()
72+
}
73+
74+
private fun determineDayOfMonth(days: Int, month: Int): Int {
75+
var count = 0
76+
var daysForMonths = 0
77+
78+
while (count < month) {
79+
val year = determineYear(daysForMonths)
80+
daysForMonths += getDaysForMonth(count % 12, year)
81+
count++
82+
}
83+
84+
return days - daysForMonths
85+
}
86+
87+
private fun determineYear(days: Int): Int {
88+
return if (days <= 366) 2016
89+
else if (days <= 730) 2017
90+
else if (days <= 1094) 2018
91+
else if (days <= 1458) 2019
92+
else 2020
93+
}
94+
}

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/custom/MyAxisValueFormatter.java

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.xxmassdeveloper.mpchartexample.custom
2+
3+
import com.github.mikephil.charting.components.AxisBase
4+
import com.github.mikephil.charting.formatter.IAxisValueFormatter
5+
import java.text.DecimalFormat
6+
7+
class MyAxisValueFormatter : IAxisValueFormatter {
8+
private val mFormat = DecimalFormat("###,###,###,##0.0")
9+
10+
override fun getFormattedValue(value: Float, axis: AxisBase?): String {
11+
return mFormat.format(value.toDouble()) + " $"
12+
}
13+
}

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/custom/MyCustomXAxisValueFormatter.java

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

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/custom/MyFillFormatter.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.xxmassdeveloper.mpchartexample.custom
2+
3+
import com.github.mikephil.charting.formatter.IFillFormatter
4+
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider
5+
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet
6+
7+
@Suppress("unused")
8+
class MyFillFormatter(private val fillPos: Float) : IFillFormatter {
9+
override fun getFillLinePosition(dataSet: ILineDataSet?, dataProvider: LineDataProvider?): Float {
10+
// your logic could be here
11+
return fillPos
12+
}
13+
}

MPChartExample/src/main/java/com/xxmassdeveloper/mpchartexample/custom/MyValueFormatter.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.xxmassdeveloper.mpchartexample.custom
2+
3+
import com.github.mikephil.charting.data.Entry
4+
import com.github.mikephil.charting.formatter.IValueFormatter
5+
import com.github.mikephil.charting.utils.ViewPortHandler
6+
import java.text.DecimalFormat
7+
8+
class MyValueFormatter : IValueFormatter {
9+
private val decimalFormat = DecimalFormat("###,###,###,##0.0")
10+
11+
override fun getFormattedValue(value: Float, entry: Entry?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String? {
12+
return decimalFormat.format(value.toDouble()) + " $"
13+
}
14+
}

0 commit comments

Comments
 (0)