diff --git a/app/build.gradle b/app/build.gradle index 26bfbb1..ed8fde5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -31,7 +31,7 @@ dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' - implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1' + implementation 'com.jakewharton.threetenabp:threetenabp:1.2.2' implementation project(path: ':core') } diff --git a/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt b/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt index c5fead4..e6ae098 100644 --- a/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt +++ b/app/src/main/java/com/oxeanbits/forecastchart/ui/MainExample.kt @@ -54,6 +54,8 @@ class MainExample : AppCompatActivity() { forecastedLine.color, forecastedLine.forecasted) endDateBar(endDateBar.x, endDateBar.y) unit("m³") + dateFormat("MM/dd/yyyy") + decimalFormat(SetupChartExample.getDecimalFormat()) detailsEnable(true) } } diff --git a/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt b/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt index f030e02..3116753 100644 --- a/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt +++ b/app/src/main/java/com/oxeanbits/forecastchart/util/SetupChartExample.kt @@ -6,6 +6,7 @@ import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.utils.ColorTemplate import com.oxeanbits.forecastchart.core.model.Line import com.oxeanbits.forecastchart.core.util.DateFormatter +import java.text.DecimalFormat object SetupChartExample{ @@ -52,4 +53,12 @@ object SetupChartExample{ fun getEndDateObj(): BarEntry{ return BarEntry(timestamp5, 100f) } + + fun getDecimalFormat(): DecimalFormat{ + val decimalFormat = DecimalFormat() + decimalFormat.minimumFractionDigits = 2 + decimalFormat.maximumFractionDigits = 2 + + return decimalFormat + } } \ No newline at end of file diff --git a/core/build.gradle b/core/build.gradle index fa287f0..c44ba6a 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -15,14 +15,12 @@ android { versionCode 1 versionName "1.0" } - buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } - } dependencies { @@ -34,5 +32,5 @@ dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' - implementation 'com.jakewharton.threetenabp:threetenabp:1.2.1' + implementation 'com.jakewharton.threetenabp:threetenabp:1.2.2' } \ No newline at end of file diff --git a/core/src/main/java/com/oxeanbits/forecastchart/core/ui/component/ForecastChartComponent.kt b/core/src/main/java/com/oxeanbits/forecastchart/core/ui/component/ForecastChartComponent.kt index c0beaec..45d55a0 100644 --- a/core/src/main/java/com/oxeanbits/forecastchart/core/ui/component/ForecastChartComponent.kt +++ b/core/src/main/java/com/oxeanbits/forecastchart/core/ui/component/ForecastChartComponent.kt @@ -8,6 +8,7 @@ import com.github.mikephil.charting.data.BarEntry import com.github.mikephil.charting.data.Entry import com.oxeanbits.forecastchart.core.model.Line import com.oxeanbits.forecastchart.core.util.Colors +import com.oxeanbits.forecastchart.core.util.DateFormatter.DEFAULT_DATE_FORMAT import com.oxeanbits.forecastchart.core.util.ForecastChart import trikita.anvil.Anvil import trikita.anvil.BaseDSL.MATCH @@ -23,6 +24,7 @@ import trikita.anvil.DSL.orientation import trikita.anvil.DSL.text import trikita.anvil.DSL.textColor import trikita.anvil.DSL.textView +import java.text.DecimalFormat inline fun forecastChartComponent(crossinline func: ForecastChartComponent.() -> Unit) { highOrderComponent(func) @@ -35,6 +37,8 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re private var forecastedData: Line? = null private var endDateData: BarEntry = emptyBar() private var unit: String = "" + private var dateFormat: String = DEFAULT_DATE_FORMAT + private var decimalFormat: DecimalFormat = DecimalFormat() private var zoomEnabled: Boolean = false private var detailsEnable: Boolean = false @@ -108,7 +112,7 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re } textView { size(WRAP, WRAP) - text(" ${unit}") + text(" $unit") textColor(Colors.TEXT_DEFAULT_GRAY) textSize(15f) } @@ -145,6 +149,8 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re forecastedData, endDateData, unit, + dateFormat, + decimalFormat, zoomEnabled ) } @@ -183,6 +189,14 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re this.unit = unit } + fun dateFormat(dateFormat: String){ + this.dateFormat = dateFormat + } + + fun decimalFormat(decimalFormat: DecimalFormat){ + this.decimalFormat = decimalFormat + } + fun zoomEnabled(zoomEnabled: Boolean){ this.zoomEnabled = zoomEnabled } diff --git a/core/src/main/java/com/oxeanbits/forecastchart/core/ui/marker/ForecastMarkerView.kt b/core/src/main/java/com/oxeanbits/forecastchart/core/ui/marker/ForecastMarkerView.kt index 11faadc..6b2fa69 100644 --- a/core/src/main/java/com/oxeanbits/forecastchart/core/ui/marker/ForecastMarkerView.kt +++ b/core/src/main/java/com/oxeanbits/forecastchart/core/ui/marker/ForecastMarkerView.kt @@ -9,9 +9,11 @@ import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.utils.MPPointF import com.oxeanbits.forecastchart.core.R import com.oxeanbits.forecastchart.core.util.DateFormatter +import java.text.DecimalFormat @SuppressLint("ViewConstructor") -class ForecastMarkerView(context: Context?, layoutResource: Int, private val unit: String) : +class ForecastMarkerView(context: Context?, layoutResource: Int, private val unit: String, + private val dateFormat: String, private val decimalFormat: DecimalFormat) : MarkerView(context, layoutResource) { @SuppressLint("SetTextI18n") @@ -22,10 +24,10 @@ class ForecastMarkerView(context: Context?, layoutResource: Int, private val uni val dateContent: TextView = findViewById(R.id.date_content) val date = e?.x?.toLong()?.let { - DateFormatter.timestampToDate(it) + DateFormatter.timestampToDate(it, dateFormat) } - prodContent.text = e?.y.toString() + unit + prodContent.text = decimalFormat.format(e?.y) + unit dateContent.text = date super.refreshContent(e, highlight) } diff --git a/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt b/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt index bb78cdd..e440f85 100644 --- a/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt +++ b/core/src/main/java/com/oxeanbits/forecastchart/core/util/DateFormatter.kt @@ -9,14 +9,16 @@ import org.threeten.bp.format.DateTimeFormatter object DateFormatter{ + const val DEFAULT_DATE_FORMAT: String = "dd/MM/yyyy" + fun stringToTimestamp(date: String): Float{ return LocalDate.parse(date).atStartOfDay() .toInstant(ZoneOffset.UTC).epochSecond.toFloat() } - fun timestampToDate(timestamp: Long): String{ + fun timestampToDate(timestamp: Long, dateFormat: String): String{ val mDate = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneOffset.UTC) - val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") + val formatter = DateTimeFormatter.ofPattern(dateFormat) return mDate.format(formatter) } } \ No newline at end of file diff --git a/core/src/main/java/com/oxeanbits/forecastchart/core/util/ForecastChart.kt b/core/src/main/java/com/oxeanbits/forecastchart/core/util/ForecastChart.kt index a76297a..b99f539 100644 --- a/core/src/main/java/com/oxeanbits/forecastchart/core/util/ForecastChart.kt +++ b/core/src/main/java/com/oxeanbits/forecastchart/core/util/ForecastChart.kt @@ -12,14 +12,15 @@ import com.oxeanbits.forecastchart.core.model.Line import com.oxeanbits.forecastchart.core.util.SetupChart.configChart import com.oxeanbits.forecastchart.core.util.SetupChart.setupEndBarDataSet import com.oxeanbits.forecastchart.core.util.SetupChart.setupLineDataSet +import java.text.DecimalFormat object ForecastChart{ - const val END_DATE_LABEL = "End Date" - const val BAR_WIDTH = 1000f + private const val END_DATE_LABEL = "End Date" + private const val BAR_WIDTH = 1000f fun createForecastChart(context: Context, combinedChart: CombinedChart, expectedData: Line, actualData: Line, forecastedData: Line, endDateData: BarEntry, - unit: String, zoomEnabled: Boolean) { + unit: String, dateFormat: String, decimalFormat: DecimalFormat, zoomEnabled: Boolean) { val production = setupLineDataSet(expectedData) val actual = setupLineDataSet(actualData) @@ -43,6 +44,6 @@ object ForecastChart{ combinedData.setData(barData) combinedChart.data = combinedData - configChart(context, combinedChart, unit, zoomEnabled) + configChart(context, combinedChart, unit, dateFormat, decimalFormat, zoomEnabled) } } \ No newline at end of file diff --git a/core/src/main/java/com/oxeanbits/forecastchart/core/util/SetupChart.kt b/core/src/main/java/com/oxeanbits/forecastchart/core/util/SetupChart.kt index 1144405..cb54029 100644 --- a/core/src/main/java/com/oxeanbits/forecastchart/core/util/SetupChart.kt +++ b/core/src/main/java/com/oxeanbits/forecastchart/core/util/SetupChart.kt @@ -9,6 +9,7 @@ import com.oxeanbits.forecastchart.core.R import com.oxeanbits.forecastchart.core.model.Bar import com.oxeanbits.forecastchart.core.model.Line import com.oxeanbits.forecastchart.core.ui.marker.ForecastMarkerView +import java.text.DecimalFormat object SetupChart{ const val LINE_WIDTH = 2f @@ -44,7 +45,7 @@ object SetupChart{ } fun configChart(context: Context, combinedChart: CombinedChart, unit: String, - zoomEnabled: Boolean){ + dateFormat: String, decimalFormat: DecimalFormat, zoomEnabled: Boolean){ combinedChart.legend.form = Legend.LegendForm.LINE combinedChart.legend.xEntrySpace = LEGEND_SIZE combinedChart.legend.textSize = LEGEND_SIZE @@ -68,7 +69,9 @@ object SetupChart{ val marker = ForecastMarkerView( context, R.layout.marker_layout, - unit + unit, + dateFormat, + decimalFormat ) combinedChart.marker = marker combinedChart.extraLeftOffset = SIDE_OFFSET