Skip to content

Commit

Permalink
Implementing Date Format and Decimal Format
Browse files Browse the repository at this point in the history
  • Loading branch information
rodiguif committed Jan 31, 2020
1 parent f90e1e4 commit 8c80b85
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class MainExample : AppCompatActivity() {
forecastedLine.color, forecastedLine.forecasted)
endDateBar(endDateBar.x, endDateBar.y)
unit("")
dateFormat("MM/dd/yyyy")
decimalFormat(SetupChartExample.getDecimalFormat())
detailsEnable(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand Down Expand Up @@ -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
}
}
4 changes: 1 addition & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ android {
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
Expand All @@ -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'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -145,6 +149,8 @@ class ForecastChartComponent(context: Context) : LinearLayout(context), Anvil.Re
forecastedData,
endDateData,
unit,
dateFormat,
decimalFormat,
zoomEnabled
)
}
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -43,6 +44,6 @@ object ForecastChart{
combinedData.setData(barData)

combinedChart.data = combinedData
configChart(context, combinedChart, unit, zoomEnabled)
configChart(context, combinedChart, unit, dateFormat, decimalFormat, zoomEnabled)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8c80b85

Please sign in to comment.