Skip to content

Commit a6b376a

Browse files
authored
Merge pull request #758 from AppDevNext/MoreValueFormatter
More time value formatter
2 parents 737bc8c + 70e3d0b commit a6b376a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package info.appdev.chartexample.formatter
2+
3+
import info.appdev.charting.data.BarEntryDouble
4+
import info.appdev.charting.data.BarEntryFloat
5+
import info.appdev.charting.data.EntryFloat
6+
import info.appdev.charting.formatter.IValueFormatter
7+
import info.appdev.charting.utils.ViewPortHandler
8+
import timber.log.Timber
9+
import java.text.SimpleDateFormat
10+
import java.util.Locale
11+
12+
class TimeRangeValueFormatter(val format: String = "yyyy-MM-dd'T'HH:mm:ss'Z'") : IValueFormatter {
13+
14+
val simpleDateFormat = SimpleDateFormat(format, Locale.getDefault())
15+
16+
// For bar/line values
17+
override fun getFormattedValue(value: Float, entryFloat: EntryFloat?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String {
18+
return when (entryFloat) {
19+
is BarEntryDouble if entryFloat.yValsDouble != null -> {
20+
// High-precision double path
21+
val vals = entryFloat.yValsDouble!!
22+
val start = vals.first().toLong()
23+
val end = vals.last().toLong()
24+
"$start - $end"
25+
}
26+
27+
is BarEntryFloat if entryFloat.yVals != null -> {
28+
// Float path
29+
val vals = entryFloat.yVals!!
30+
val start = vals.first().toLong()
31+
val end = vals.last().toLong()
32+
"$start - $end"
33+
}
34+
35+
else -> simpleDateFormat.format(value.toLong())
36+
}
37+
}
38+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package info.appdev.chartexample.formatter
2+
3+
import info.appdev.charting.data.BarEntryDouble
4+
import info.appdev.charting.data.BarEntryFloat
5+
import info.appdev.charting.data.EntryFloat
6+
import info.appdev.charting.formatter.IValueFormatter
7+
import info.appdev.charting.utils.ViewPortHandler
8+
import timber.log.Timber
9+
import java.text.SimpleDateFormat
10+
import java.util.Locale
11+
12+
class UnixTimeValueFormatter(val format: String = "yyyy-MM-dd'T'HH:mm:ss'Z'") : IValueFormatter {
13+
14+
val simpleDateFormat = SimpleDateFormat(format, Locale.getDefault())
15+
16+
// For bar/line values
17+
override fun getFormattedValue(value: Float, entryFloat: EntryFloat?, dataSetIndex: Int, viewPortHandler: ViewPortHandler?): String {
18+
return when (entryFloat) {
19+
is BarEntryDouble if entryFloat.yValsDouble != null -> {
20+
// High-precision double path
21+
val vals = entryFloat.yValsDouble!!
22+
val start = simpleDateFormat.format(vals.first().toLong())
23+
val end = simpleDateFormat.format(vals.last().toLong())
24+
"$start - $end"
25+
}
26+
27+
is BarEntryFloat if entryFloat.yVals != null -> {
28+
// Float path
29+
val vals = entryFloat.yVals!!
30+
val start = simpleDateFormat.format(vals.first().toLong())
31+
val end = simpleDateFormat.format(vals.last().toLong())
32+
"$start - $end"
33+
}
34+
35+
else -> simpleDateFormat.format(value.toLong())
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)