From cb5f66e58c73b7b122838526c4d433aea9130820 Mon Sep 17 00:00:00 2001 From: Hannes Achleitner Date: Sun, 4 Jan 2026 17:34:28 +0100 Subject: [PATCH] getFormattedValue non null --- .../info/appdev/chartexample/BarChartPositiveNegative.kt | 6 +++--- .../info/appdev/chartexample/LineChartTimeActivity.kt | 6 +++--- .../chartexample/custom/CustomScatterShapeRenderer.kt | 8 ++++++-- .../info/appdev/chartexample/custom/MyMarkerView.kt | 3 +-- .../chartexample/formatter/DayAxisValueFormatter.kt | 7 ++++--- .../appdev/chartexample/formatter/MyAxisValueFormatter.kt | 4 ++-- .../chartexample/formatter/UnixTimeAxisValueFormatter.kt | 4 ++-- .../charting/formatter/DefaultAxisValueFormatter.kt | 2 +- .../info/appdev/charting/formatter/IAxisValueFormatter.kt | 2 +- .../info/appdev/charting/formatter/PercentFormatter.kt | 2 +- 10 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt index 7e7cce347..9d706931b 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartPositiveNegative.kt @@ -70,7 +70,7 @@ class BarChartPositiveNegative : DemoBase() { binding.chart1.legend.isEnabled = false // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT - val data: MutableList = ArrayList() + val data: MutableList = mutableListOf() data.add(Data(0f, -224.1f, "12-29")) data.add(Data(1f, 238.5f, "12-30")) data.add(Data(2f, 1280.1f, "12-31")) @@ -78,8 +78,8 @@ class BarChartPositiveNegative : DemoBase() { data.add(Data(4f, -2280.1f, "01-02")) xAxis.valueFormatter = object : IAxisValueFormatter { - override fun getFormattedValue(value: Float, axis: AxisBase?): String? { - return data[min(max(value.toInt(), 0), data.size - 1)].xAxisValue + override fun getFormattedValue(value: Float, axis: AxisBase?): String { + return data[min(max(value.toInt(), 0), data.size - 1)].xAxisValue.toString() } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt index c38b31af5..cbda5d3cc 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartTimeActivity.kt @@ -77,11 +77,11 @@ class LineChartTimeActivity : DemoBase(), OnSeekBarChangeListener { xAxis.setCenterAxisLabels(true) xAxis.granularity = 1f // one hour xAxis.valueFormatter = object : IAxisValueFormatter { - private val mFormat = SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH) + private val simpleDateFormat = SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH) - override fun getFormattedValue(value: Float, axis: AxisBase?): String? { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { val millis = TimeUnit.HOURS.toMillis(value.toLong()) - return mFormat.format(Date(millis)) + return simpleDateFormat.format(Date(millis)) } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt b/app/src/main/kotlin/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt index ca0fd5c91..298636ee6 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt @@ -12,8 +12,12 @@ import info.appdev.charting.utils.convertDpToPixel */ class CustomScatterShapeRenderer : IShapeRenderer { override fun renderShape( - canvas: Canvas, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler?, - posX: Float, posY: Float, renderPaint: Paint + canvas: Canvas, + dataSet: IScatterDataSet, + viewPortHandler: ViewPortHandler?, + posX: Float, + posY: Float, + renderPaint: Paint ) { val shapeHalf = dataSet.scatterShapeSize.convertDpToPixel() / 2f diff --git a/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt b/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt index 3de94bcea..850e2fb44 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/custom/MyMarkerView.kt @@ -18,8 +18,7 @@ import info.appdev.charting.utils.formatNumber class MyMarkerView(context: Context?, layoutResource: Int) : MarkerView(context, layoutResource) { private val tvContent: TextView = findViewById(R.id.tvContent) - // runs every time the MarkerView is redrawn, can be used to update the - // content (user-interface) + // runs every time the MarkerView is redrawn, can be used to update the content (user-interface) override fun refreshContent(entry: Entry, highlight: Highlight) { if (entry is CandleEntry) { diff --git a/app/src/main/kotlin/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt b/app/src/main/kotlin/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt index 55fa37c27..3bd0a5299 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt @@ -23,7 +23,6 @@ class DayAxisValueFormatter(private val chart: BarLineChartBase<*>) : IAxisValue return "$monthName $yearName" } else { val dayOfMonth = determineDayOfMonth(days, month + 12 * (year - 2016)) - var appendix = "th" when (dayOfMonth) { @@ -51,8 +50,10 @@ class DayAxisValueFormatter(private val chart: BarLineChartBase<*>) : IAxisValue return if (is29Feb) 29 else 28 } - return if (month == 3 || month == 5 || month == 8 || month == 10) 30 - else 31 + return if (month == 3 || month == 5 || month == 8 || month == 10) + 30 + else + 31 } private fun determineMonth(dayOfYear: Int): Int { diff --git a/app/src/main/kotlin/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt b/app/src/main/kotlin/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt index 1fc6ab234..863d0f3eb 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt @@ -5,9 +5,9 @@ import info.appdev.charting.formatter.IAxisValueFormatter import java.text.DecimalFormat class MyAxisValueFormatter : IAxisValueFormatter { - private val mFormat = DecimalFormat("###,###,###,##0.0") + private val decimalFormat = DecimalFormat("###,###,###,##0.0") override fun getFormattedValue(value: Float, axis: AxisBase?): String { - return mFormat.format(value.toDouble()) + " $" + return decimalFormat.format(value.toDouble()) + " $" } } diff --git a/app/src/main/kotlin/info/appdev/chartexample/formatter/UnixTimeAxisValueFormatter.kt b/app/src/main/kotlin/info/appdev/chartexample/formatter/UnixTimeAxisValueFormatter.kt index 3efeb6ae3..d123887dd 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/formatter/UnixTimeAxisValueFormatter.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/formatter/UnixTimeAxisValueFormatter.kt @@ -7,10 +7,10 @@ import java.util.Locale class UnixTimeAxisValueFormatter(val format: String = "yyyy-MM-dd'T'HH:mm:ss'Z'") : IAxisValueFormatter { - val sdf = SimpleDateFormat(format, Locale.getDefault()) + val simpleDateFormat = SimpleDateFormat(format, Locale.getDefault()) override fun getFormattedValue(value: Float, axis: AxisBase?): String { - return sdf.format(value * 1000L) + return simpleDateFormat.format(value * 1000L) } } diff --git a/chartLib/src/main/kotlin/info/appdev/charting/formatter/DefaultAxisValueFormatter.kt b/chartLib/src/main/kotlin/info/appdev/charting/formatter/DefaultAxisValueFormatter.kt index d8a6419c0..06b3837df 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/formatter/DefaultAxisValueFormatter.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/formatter/DefaultAxisValueFormatter.kt @@ -25,7 +25,7 @@ open class DefaultAxisValueFormatter(digits: Int) : IAxisValueFormatter { decimalFormat = DecimalFormat("###,###,###,##0$stringBuffer") } - override fun getFormattedValue(value: Float, axis: AxisBase?): String? { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { // avoid memory allocations here (for performance) return decimalFormat.format(value.toDouble()) } diff --git a/chartLib/src/main/kotlin/info/appdev/charting/formatter/IAxisValueFormatter.kt b/chartLib/src/main/kotlin/info/appdev/charting/formatter/IAxisValueFormatter.kt index 760b6a41d..b253014f8 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/formatter/IAxisValueFormatter.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/formatter/IAxisValueFormatter.kt @@ -14,5 +14,5 @@ interface IAxisValueFormatter { * @param value the value to be formatted * @param axis the axis the value belongs to */ - fun getFormattedValue(value: Float, axis: AxisBase?): String? + fun getFormattedValue(value: Float, axis: AxisBase?): String } diff --git a/chartLib/src/main/kotlin/info/appdev/charting/formatter/PercentFormatter.kt b/chartLib/src/main/kotlin/info/appdev/charting/formatter/PercentFormatter.kt index c085290ae..271fab7bf 100644 --- a/chartLib/src/main/kotlin/info/appdev/charting/formatter/PercentFormatter.kt +++ b/chartLib/src/main/kotlin/info/appdev/charting/formatter/PercentFormatter.kt @@ -28,7 +28,7 @@ open class PercentFormatter : IValueFormatter, IAxisValueFormatter { } // IAxisValueFormatter - override fun getFormattedValue(value: Float, axis: AxisBase?): String? { + override fun getFormattedValue(value: Float, axis: AxisBase?): String { return decimalFormat.format(value.toDouble()) + " %" }