|
|
|
@@ -12,7 +12,6 @@ import android.widget.Toast
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import androidx.core.graphics.toColorInt
|
|
|
|
import androidx.core.graphics.toColorInt
|
|
|
|
import com.github.mikephil.charting.charts.BarChart
|
|
|
|
import com.github.mikephil.charting.charts.BarChart
|
|
|
|
import com.github.mikephil.charting.components.YAxis
|
|
|
|
|
|
|
|
import com.github.mikephil.charting.data.BarData
|
|
|
|
import com.github.mikephil.charting.data.BarData
|
|
|
|
import com.github.mikephil.charting.data.BarDataSet
|
|
|
|
import com.github.mikephil.charting.data.BarDataSet
|
|
|
|
import com.github.mikephil.charting.data.BarEntry
|
|
|
|
import com.github.mikephil.charting.data.BarEntry
|
|
|
|
@@ -83,12 +82,10 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
barChart.axisRight.setDrawGridLines(false)
|
|
|
|
barChart.axisRight.setDrawGridLines(false)
|
|
|
|
barChart.axisRight.setDrawLabels(false)
|
|
|
|
barChart.axisRight.setDrawLabels(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.xAxis.setDrawGridLines(true)
|
|
|
|
barChart.xAxis.setDrawLabels(true)
|
|
|
|
barChart.xAxis.setDrawLabels(true)
|
|
|
|
barChart.xAxis.setDrawAxisLine(false)
|
|
|
|
barChart.xAxis.setDrawAxisLine(false)
|
|
|
|
|
|
|
|
|
|
|
|
barChart.isScaleXEnabled = false
|
|
|
|
|
|
|
|
barChart.isScaleYEnabled = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
graphTypeSpinner = findViewById(R.id.graph_type_selection)
|
|
|
|
graphTypeSpinner = findViewById(R.id.graph_type_selection)
|
|
|
|
timeRangeSpinner = findViewById(R.id.time_range_selection)
|
|
|
|
timeRangeSpinner = findViewById(R.id.time_range_selection)
|
|
|
|
|
|
|
|
|
|
|
|
@@ -139,54 +136,20 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun resetBarChart() {
|
|
|
|
|
|
|
|
barChart.fitScreen()
|
|
|
|
|
|
|
|
barChart.data?.clearValues()
|
|
|
|
|
|
|
|
barChart.xAxis.valueFormatter = null
|
|
|
|
|
|
|
|
barChart.notifyDataSetChanged()
|
|
|
|
|
|
|
|
barChart.clear()
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
barChart.setBackgroundColor(Color.WHITE)
|
|
|
|
|
|
|
|
//barChart.description.text = logbookName
|
|
|
|
|
|
|
|
barChart.setDrawValueAboveBar(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.axisLeft.setAxisMinimum(0F)
|
|
|
|
|
|
|
|
barChart.axisLeft.setDrawGridLines(false)
|
|
|
|
|
|
|
|
barChart.axisLeft.setDrawLabels(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.axisRight.setDrawGridLines(false)
|
|
|
|
|
|
|
|
barChart.axisRight.setDrawLabels(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.xAxis.setDrawGridLines(true)
|
|
|
|
|
|
|
|
barChart.xAxis.setDrawLabels(true)
|
|
|
|
|
|
|
|
barChart.xAxis.setDrawAxisLine(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(true)
|
|
|
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
|
|
|
|
//barChart.isScaleXEnabled = false
|
|
|
|
|
|
|
|
//barChart.isScaleYEnabled = true
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// for debugging
|
|
|
|
|
|
|
|
Log.d(TAG, "resetBarChart; barChart.xAxis.labelCount: ${barChart.xAxis.labelCount}, barChart.visibleXRange: ${barChart.visibleXRange}, barChart.xAxis.isCenterAxisLabelsEnabled: ${barChart.xAxis.isCenterAxisLabelsEnabled}, barChart.isAutoScaleMinMaxEnabled: ${barChart.isAutoScaleMinMaxEnabled}, barChart.isScaleXEnabled: ${barChart.isScaleXEnabled}, barChart.isScaleYEnabled: ${barChart.isScaleYEnabled}")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun showMedicineBarGraph(state: GraphState) {
|
|
|
|
fun showMedicineBarGraph(state: GraphState) {
|
|
|
|
val values = HashMap<String, ArrayList<BarEntry>>()
|
|
|
|
val values = HashMap<String, ArrayList<BarEntry>>()
|
|
|
|
val days = state.endSpan - state.startSpan + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (event in state.events) {
|
|
|
|
for (event in state.events) {
|
|
|
|
val index = unixToSpan(event.time) - state.startSpan
|
|
|
|
val index = unixToSpan(event.time) - state.startSpan
|
|
|
|
val key = event.notes.trim().lowercase()
|
|
|
|
val key = event.notes.trim().lowercase()
|
|
|
|
val array = values.getOrPut(key) {
|
|
|
|
val array = values.getOrPut(key) {
|
|
|
|
// create initial array with 0
|
|
|
|
ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), 0F) })
|
|
|
|
ArrayList(List(days) { BarEntry(it.toFloat(), 0F) })
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
array[index].y += 1F
|
|
|
|
array[index].y += 1F
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "values.size: ${values.size}, days: $days")
|
|
|
|
Log.d(TAG, "values.size: ${values.size}")
|
|
|
|
for ((key, value) in values) {
|
|
|
|
for ((key, value) in values) {
|
|
|
|
Log.d(TAG, "key: $key, value.size: ${value.size} ,value: ${value.joinToString { it.y.toLong().toString() }}")
|
|
|
|
Log.d(TAG, "key: $key, value.size: ${value.size} ,value: ${value.joinToString { it.y.toLong().toString() }}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -205,18 +168,19 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val sets = arrayListOf<IBarDataSet>()
|
|
|
|
val sets = arrayListOf<IBarDataSet>()
|
|
|
|
for ((key, array) in values.entries) {
|
|
|
|
for ((key, value) in values.entries) {
|
|
|
|
|
|
|
|
if (key.startsWith("v")) {
|
|
|
|
val description = shorten(key)
|
|
|
|
val description = shorten(key)
|
|
|
|
Log.d(TAG, "key: $key")
|
|
|
|
val barDataSet = BarDataSet(value, description)
|
|
|
|
val barDataSet = BarDataSet(array, description)
|
|
|
|
|
|
|
|
barDataSet.color = chooseColor(key)
|
|
|
|
barDataSet.color = chooseColor(key)
|
|
|
|
sets.add(barDataSet)
|
|
|
|
sets.add(barDataSet)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val data = BarData(sets)
|
|
|
|
val data = BarData(sets)
|
|
|
|
//data.groupBars(0F, 0.2F, 0.1F);
|
|
|
|
//data.groupBars(0F, 0.2F, 0.1F);
|
|
|
|
//data.setValueTextSize(12f)
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
//data.barWidth = 1F
|
|
|
|
data.barWidth = 1F
|
|
|
|
//data.groupBars(0F, 1F, 1F)
|
|
|
|
//data.groupBars(0F, 1F, 1F)
|
|
|
|
|
|
|
|
|
|
|
|
data.setValueFormatter(object : ValueFormatter() {
|
|
|
|
data.setValueFormatter(object : ValueFormatter() {
|
|
|
|
@@ -229,31 +193,22 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
|
|
|
|
barChart.setScaleEnabled(true)
|
|
|
|
override fun onValueSelected(e: Entry?, h: Highlight?) {
|
|
|
|
|
|
|
|
Log.d(TAG, "onValueSelected ${e == null} ${h == null}")
|
|
|
|
|
|
|
|
if (e == null || h == null) {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val index = e.x.toInt()
|
|
|
|
|
|
|
|
if (index !in 0..values.size) {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "index: $index")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onNothingSelected() {}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
|
|
|
|
barChart.setData(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.legend.isEnabled = true
|
|
|
|
barChart.legend.isEnabled = true
|
|
|
|
val valueCount = min(days, 24)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
//barChart.xAxis.setLabelCount(min(values.size, 24), false);
|
|
|
|
barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
//val maxCount = min(maxIndex, 30) // values.size
|
|
|
|
barChart.xAxis.setCenterAxisLabels(false)
|
|
|
|
//Log.d(TAG, "maxCount: $maxCount")
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(20F) //maxCount.toFloat()) // show max 24 entries
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(30, true)
|
|
|
|
|
|
|
|
//barChart.xAxis.isEnabled = false
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(true)
|
|
|
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.axisLeft.isSLEEP_PATTERN_GRANULARITYEnabled = true
|
|
|
|
|
|
|
|
//barChart.axisLeft.setSLEEP_PATTERN_GRANULARITY(0.8F)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.setData(data)
|
|
|
|
barChart.invalidate()
|
|
|
|
barChart.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -318,7 +273,7 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
assert(sleepBegin <= sleepEnd)
|
|
|
|
assert(sleepBegin <= sleepEnd)
|
|
|
|
val iBegin = (sleepBegin - dayBegin) / SLEEP_PATTERN_GRANULARITY
|
|
|
|
val iBegin = (sleepBegin - dayBegin) / SLEEP_PATTERN_GRANULARITY
|
|
|
|
val iEnd = iBegin + (sleepEnd - sleepBegin) / SLEEP_PATTERN_GRANULARITY
|
|
|
|
val iEnd = iBegin + (sleepEnd - sleepBegin) / SLEEP_PATTERN_GRANULARITY
|
|
|
|
//Log.d(TAG, "index: $index, iBegin: $iBegin, iEnd: $iEnd, dayBegin: ${Date(dayBegin * 1000)}, dayEnd: ${Date(dayEnd * 1000)}, sleepBegin: ${Date(sleepBegin * 1000)}, sleepEnd: ${Date(sleepEnd * 1000)}")
|
|
|
|
Log.d(TAG, "index: $index, iBegin: $iBegin, iEnd: $iEnd, dayBegin: ${Date(dayBegin * 1000)}, dayEnd: ${Date(dayEnd * 1000)}, sleepBegin: ${Date(sleepBegin * 1000)}, sleepEnd: ${Date(sleepEnd * 1000)}")
|
|
|
|
for (j in iBegin..iEnd) {
|
|
|
|
for (j in iBegin..iEnd) {
|
|
|
|
stack[index][j.toInt()] += 1
|
|
|
|
stack[index][j.toInt()] += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -356,11 +311,13 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun mapColor(occurrences: Int, maxOccurrences: Int): Int {
|
|
|
|
fun mapColor(occurrences: Int, maxOccurrences: Int): Int {
|
|
|
|
// occurrences: number of reported sleeps in a specific time slot
|
|
|
|
//Log.d(TAG, "$occurrences <= $maxOccurrences")
|
|
|
|
|
|
|
|
// occurrences: number of reported sleeps in a specific time slice
|
|
|
|
// maxOccurrences: maximum number of days with data that can contribute to maxOccurrences
|
|
|
|
// maxOccurrences: maximum number of days with data that can contribute to maxOccurrences
|
|
|
|
assert(maxOccurrences > 0)
|
|
|
|
assert(maxOccurrences > 0)
|
|
|
|
assert(occurrences <= maxOccurrences)
|
|
|
|
assert(occurrences <= maxOccurrences)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// map to color
|
|
|
|
// map to color
|
|
|
|
val q = occurrences.toFloat() / maxOccurrences.toFloat()
|
|
|
|
val q = occurrences.toFloat() / maxOccurrences.toFloat()
|
|
|
|
val i = q * (SLEEP_PATTERN_COLORS.size - 1).toFloat()
|
|
|
|
val i = q * (SLEEP_PATTERN_COLORS.size - 1).toFloat()
|
|
|
|
@@ -376,8 +333,8 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
//Log.d(TAG, "index: $index: daysWithData: $daysWithData, dayArray: ${dayArray.joinToString { it.toString() }}")
|
|
|
|
//Log.d(TAG, "index: $index: daysWithData: $daysWithData, dayArray: ${dayArray.joinToString { it.toString() }}")
|
|
|
|
val vals = ArrayList<Float>()
|
|
|
|
val vals = ArrayList<Float>()
|
|
|
|
|
|
|
|
|
|
|
|
var prevIndex = -1 // time slot index
|
|
|
|
var prevIndex = -1 // time slice index
|
|
|
|
var prevValue = -1 // number of entries we have found for time slot
|
|
|
|
var prevValue = -1 // number of entries we have found for time slice
|
|
|
|
for ((i, v) in dayArray.withIndex()) {
|
|
|
|
for ((i, v) in dayArray.withIndex()) {
|
|
|
|
if (i == 0) {
|
|
|
|
if (i == 0) {
|
|
|
|
prevIndex = i
|
|
|
|
prevIndex = i
|
|
|
|
@@ -397,7 +354,6 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
|
|
|
|
|
|
|
|
//Log.d(TAG, "Range $index, vals: ${vals.joinToString { it.toInt().toString() }}") //, allColors: ${allColors.joinToString { it.toString() }}")
|
|
|
|
//Log.d(TAG, "Range $index, vals: ${vals.joinToString { it.toInt().toString() }}") //, allColors: ${allColors.joinToString { it.toString() }}")
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "index: ${index.toFloat()}")
|
|
|
|
|
|
|
|
values.add(BarEntry(index.toFloat(), vals.toFloatArray()))
|
|
|
|
values.add(BarEntry(index.toFloat(), vals.toFloatArray()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -478,46 +434,17 @@ class StatisticsActivity : AppCompatActivity() {
|
|
|
|
set1.isHighlightEnabled = true
|
|
|
|
set1.isHighlightEnabled = true
|
|
|
|
|
|
|
|
|
|
|
|
set1.setDrawIcons(false)
|
|
|
|
set1.setDrawIcons(false)
|
|
|
|
|
|
|
|
barChart.legend.isEnabled = false
|
|
|
|
//barChart.legend.isEnabled = false
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
/*
|
|
|
|
barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
|
|
val valueCount = min(values.size, 24)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barChart.xAxis.labelCount}, barChart.visibleXRange: ${barChart.visibleXRange}, barChart.xAxis.isCenterAxisLabelsEnabled: ${barChart.xAxis.isCenterAxisLabelsEnabled}, barChart.isAutoScaleMinMaxEnabled: ${barChart.isAutoScaleMinMaxEnabled}, barChart.isScaleXEnabled: ${barChart.isScaleXEnabled}, barChart.isScaleYEnabled: ${barChart.isScaleYEnabled}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.minimumWidth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.isAutoScaleMinMaxEnabled = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.setScaleEnabled(true)
|
|
|
|
|
|
|
|
//barChart.fitScreen()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//debugBarValues(values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
|
|
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
barChart.setData(data)
|
|
|
|
barChart.setData(data)
|
|
|
|
Log.d(TAG, "xChartMax: ${barChart.xChartMax}")
|
|
|
|
|
|
|
|
barChart.centerViewTo(barChart.xChartMax, 0F, YAxis.AxisDependency.RIGHT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// does not work quite right yet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.legend.isEnabled = false
|
|
|
|
|
|
|
|
val valueCount = min(values.size, 24)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(false)
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
barChart.invalidate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.moveViewToX(77F) //values.lastOrNull()!!.x)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Sleep pattern bars that do not use time slots.
|
|
|
|
// Sleep pattern bars that do not use time slots.
|
|
|
|
// This is useful/nicer for bars that only represent data of a singular days.
|
|
|
|
// This is useful/nicer for bars that only represent data of a singlur days.
|
|
|
|
fun showSleepPatternBarGraphDaily(state: GraphState) {
|
|
|
|
fun showSleepPatternBarGraphDaily(state: GraphState) {
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
val ranges = toSleepRanges(state.events)
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), FloatArray(0)) })
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), FloatArray(0)) })
|
|
|
|
@@ -622,25 +549,13 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
override fun onNothingSelected() {}
|
|
|
|
override fun onNothingSelected() {}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "showSleepPatternBarGraphDaily: values.size: ${values.size}, barChart.xAxis.labelCount: ${barChart.xAxis.labelCount}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set1.setDrawIcons(false)
|
|
|
|
set1.setDrawIcons(false)
|
|
|
|
//barChart.legend.isEnabled = false
|
|
|
|
barChart.legend.isEnabled = false
|
|
|
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
//val valueCount = min(values.size, 24)
|
|
|
|
barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
|
|
//barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
|
|
|
|
//barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
barChart.setData(data)
|
|
|
|
barChart.setData(data)
|
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "showSleepPatternBarGraphDaily: new barChart.xAxis.labelCount: ${barChart.xAxis.labelCount}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.legend.isEnabled = false
|
|
|
|
|
|
|
|
val valueCount = min(values.size, 24)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(false)
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
barChart.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -686,6 +601,7 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (graphTypeSelection == GraphType.SLEEP_SUM) {
|
|
|
|
for (index in values.indices) {
|
|
|
|
for (index in values.indices) {
|
|
|
|
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
|
|
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
|
|
if (daysWithData == 0) {
|
|
|
|
if (daysWithData == 0) {
|
|
|
|
@@ -694,6 +610,7 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
values[index].y /= daysWithData
|
|
|
|
values[index].y /= daysWithData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val set1 = BarDataSet(values, "")
|
|
|
|
val set1 = BarDataSet(values, "")
|
|
|
|
val data = BarData(set1)
|
|
|
|
val data = BarData(set1)
|
|
|
|
@@ -702,44 +619,44 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
|
|
|
|
|
|
|
|
data.setValueFormatter(object : ValueFormatter() {
|
|
|
|
data.setValueFormatter(object : ValueFormatter() {
|
|
|
|
override fun getFormattedValue(value: Float): String {
|
|
|
|
override fun getFormattedValue(value: Float): String {
|
|
|
|
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
|
|
|
|
|
|
return when (graphTypeSelection) {
|
|
|
|
return when (graphTypeSelection) {
|
|
|
|
GraphType.SLEEP_EVENTS -> {
|
|
|
|
GraphType.SLEEP_EVENTS -> value.toInt().toString()
|
|
|
|
prefix + value.toInt().toString()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphType.SLEEP_SUM -> {
|
|
|
|
GraphType.SLEEP_SUM -> {
|
|
|
|
prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.SLEEP, value.toInt())
|
|
|
|
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
|
|
|
|
|
|
return prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.SLEEP, value.toInt())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
else -> {
|
|
|
|
Log.e(TAG, "unhandled graphTypeSelection $graphTypeSelection")
|
|
|
|
Log.e(TAG, "unhandled graphTypeSelection $graphTypeSelection")
|
|
|
|
prefix + value.toInt().toString()
|
|
|
|
value.toInt().toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
set1.setDrawIcons(false)
|
|
|
|
set1.setDrawIcons(false)
|
|
|
|
|
|
|
|
barChart.legend.isEnabled = false
|
|
|
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(min(values.size, 24))
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
barChart.setData(data)
|
|
|
|
barChart.setData(data)
|
|
|
|
|
|
|
|
|
|
|
|
barChart.legend.isEnabled = false
|
|
|
|
|
|
|
|
val valueCount = min(values.size, 24)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(false)
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
barChart.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun showBottleBarGraph(state: GraphState) {
|
|
|
|
fun showBottleBarGraph(state: GraphState) {
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), 0F) })
|
|
|
|
val values = ArrayList(List(state.endSpan - state.startSpan + 1) { BarEntry(it.toFloat(), 0F) })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// needed?
|
|
|
|
|
|
|
|
for (i in values.indices) {
|
|
|
|
|
|
|
|
values[i].x = i.toFloat()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (event in state.events) {
|
|
|
|
for (event in state.events) {
|
|
|
|
val index = unixToSpan(event.time) - state.startSpan
|
|
|
|
val index = unixToSpan(event.time) - state.startSpan
|
|
|
|
|
|
|
|
|
|
|
|
state.dayCounter.setDaysWithData(event.time, event.time)
|
|
|
|
state.dayCounter.setDaysWithData(event.time, event.time)
|
|
|
|
|
|
|
|
|
|
|
|
values[index].x += values.size.toFloat()
|
|
|
|
// setDaysWithData(sleepBegin, sleepEnd)
|
|
|
|
if (graphTypeSelection == GraphType.BOTTLE_EVENTS) {
|
|
|
|
if (graphTypeSelection == GraphType.BOTTLE_EVENTS) {
|
|
|
|
values[index].y += 1F
|
|
|
|
values[index].y += 1F
|
|
|
|
} else if (graphTypeSelection == GraphType.BOTTLE_SUM) {
|
|
|
|
} else if (graphTypeSelection == GraphType.BOTTLE_SUM) {
|
|
|
|
@@ -750,6 +667,7 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (graphTypeSelection == GraphType.BOTTLE_SUM) {
|
|
|
|
for (index in values.indices) {
|
|
|
|
for (index in values.indices) {
|
|
|
|
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
|
|
val daysWithData = state.dayCounter.countDaysWithData(spanToUnix(state.startSpan + index), spanToUnix(state.startSpan + index + 1))
|
|
|
|
//Log.d(TAG, "index: $index, daysWithData: $daysWithData")
|
|
|
|
//Log.d(TAG, "index: $index, daysWithData: $daysWithData")
|
|
|
|
@@ -759,6 +677,7 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
values[index].y /= daysWithData
|
|
|
|
values[index].y /= daysWithData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val set1 = BarDataSet(values, "")
|
|
|
|
val set1 = BarDataSet(values, "")
|
|
|
|
|
|
|
|
|
|
|
|
@@ -774,34 +693,37 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
|
|
|
|
|
|
|
|
data.setValueFormatter(object : ValueFormatter() {
|
|
|
|
data.setValueFormatter(object : ValueFormatter() {
|
|
|
|
override fun getFormattedValue(value: Float): String {
|
|
|
|
override fun getFormattedValue(value: Float): String {
|
|
|
|
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
|
|
|
|
|
|
//Log.d(TAG, "getFormattedValue ${dataTypeSelectionValue} ${eventTypeSelectionValue}")
|
|
|
|
//Log.d(TAG, "getFormattedValue ${dataTypeSelectionValue} ${eventTypeSelectionValue}")
|
|
|
|
return when (graphTypeSelection) {
|
|
|
|
return when (graphTypeSelection) {
|
|
|
|
GraphType.BOTTLE_EVENTS -> {
|
|
|
|
GraphType.BOTTLE_EVENTS -> value.toInt().toString()
|
|
|
|
prefix + value.toInt().toString()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GraphType.BOTTLE_SUM -> {
|
|
|
|
GraphType.BOTTLE_SUM -> {
|
|
|
|
prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.BABY_BOTTLE, value.toInt())
|
|
|
|
val prefix = if (timeRangeSelection == TimeRange.DAY) { "" } else { "⌀ " }
|
|
|
|
|
|
|
|
return prefix + NumericUtils(applicationContext).formatEventQuantity(LunaEvent.Type.BABY_BOTTLE, value.toInt())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
else -> {
|
|
|
|
Log.e(TAG, "unhandled graphTypeSelection")
|
|
|
|
Log.e(TAG, "unhandled graphTypeSelection")
|
|
|
|
prefix + value.toInt()
|
|
|
|
value.toInt().toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// hm, does not work yet
|
|
|
|
|
|
|
|
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
data.setValueTextSize(12f)
|
|
|
|
barChart.setData(data)
|
|
|
|
barChart.setData(data)
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.legend.isEnabled = false
|
|
|
|
barChart.moveViewToX(values.lastOrNull()!!.x)
|
|
|
|
val valueCount = min(values.size, 24)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(valueCount.toFloat())
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(valueCount)
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(false)
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//barChart.moveViewToX(values.lastOrNull()!!.x)
|
|
|
|
val maximumRange = 16F
|
|
|
|
|
|
|
|
//val count = values.size.coerceIn(5, 20)
|
|
|
|
|
|
|
|
barChart.setVisibleXRangeMaximum(maximumRange) // show max 24 entries
|
|
|
|
|
|
|
|
barChart.xAxis.setLabelCount(maximumRange.toInt(), true)
|
|
|
|
|
|
|
|
//barChart.xAxis.isEnabled = false
|
|
|
|
|
|
|
|
barChart.xAxis.setCenterAxisLabels(true)
|
|
|
|
|
|
|
|
barChart.setScaleEnabled(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
barChart.invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DayCounter(val startDays: Int, val stopDays: Int) {
|
|
|
|
class DayCounter(val startDays: Int, val stopDays: Int) {
|
|
|
|
@@ -834,7 +756,7 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
|
|
|
|
|
|
|
|
data class GraphState(val events: List<LunaEvent>, val dayCounter: DayCounter, val startUnix: Long, val endUnix: Long, val startSpan: Int, val endSpan: Int)
|
|
|
|
data class GraphState(val events: List<LunaEvent>, val dayCounter: DayCounter, val startUnix: Long, val endUnix: Long, val startSpan: Int, val endSpan: Int)
|
|
|
|
|
|
|
|
|
|
|
|
// wrapper for common graph setup
|
|
|
|
// wrapper for comon graph setup
|
|
|
|
fun prepareGraph(type: LunaEvent.Type, callback: (GraphState) -> Unit) {
|
|
|
|
fun prepareGraph(type: LunaEvent.Type, callback: (GraphState) -> Unit) {
|
|
|
|
val events = MainActivity.allEvents.filter { it.type == type }.sortedBy { it.time }
|
|
|
|
val events = MainActivity.allEvents.filter { it.type == type }.sortedBy { it.time }
|
|
|
|
|
|
|
|
|
|
|
|
@@ -857,7 +779,7 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
|
|
|
|
|
|
|
|
// days when the a day/week/month starts/ends
|
|
|
|
// days when the a day/week/month starts/ends
|
|
|
|
val startDays = unixToDays(spanToUnix(startSpan))
|
|
|
|
val startDays = unixToDays(spanToUnix(startSpan))
|
|
|
|
val endDays = unixToDays(spanToUnix(endSpan + 1)) // until end of next span
|
|
|
|
val endDays = unixToDays(spanToUnix(endSpan + 1)) // until end of next week
|
|
|
|
val dayCounter = DayCounter(startDays, endDays)
|
|
|
|
val dayCounter = DayCounter(startDays, endDays)
|
|
|
|
|
|
|
|
|
|
|
|
// print dates
|
|
|
|
// print dates
|
|
|
|
@@ -872,22 +794,10 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
val month = dateTime.get(Calendar.MONTH) + 1 // month starts at 0
|
|
|
|
val month = dateTime.get(Calendar.MONTH) + 1 // month starts at 0
|
|
|
|
val week = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
|
|
val week = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
|
|
val day = dateTime.get(Calendar.DAY_OF_MONTH)
|
|
|
|
val day = dateTime.get(Calendar.DAY_OF_MONTH)
|
|
|
|
|
|
|
|
|
|
|
|
// Adjust years if the first week of a year starts in the previous year.
|
|
|
|
|
|
|
|
val years = if (month == 12 && week == 1) {
|
|
|
|
|
|
|
|
year + 1
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
year
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val days = "%02d".format(day)
|
|
|
|
|
|
|
|
val weeks = "%02d".format(week)
|
|
|
|
|
|
|
|
val months = "%02d".format(month)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return when (timeRangeSelection) {
|
|
|
|
return when (timeRangeSelection) {
|
|
|
|
TimeRange.DAY -> "$days/$months/$years"
|
|
|
|
TimeRange.DAY -> "$day/$month/$year"
|
|
|
|
TimeRange.WEEK -> "$weeks/$years"
|
|
|
|
TimeRange.WEEK -> "$week/$year"
|
|
|
|
TimeRange.MONTH -> "$months/$years"
|
|
|
|
TimeRange.MONTH -> "$month/$year"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -900,23 +810,19 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
fun showGraph() {
|
|
|
|
fun showGraph() {
|
|
|
|
//Log.d(TAG, "showGraph: graphTypeSelection: $graphTypeSelection, timeRangeSelection: $timeRangeSelection")
|
|
|
|
//Log.d(TAG, "showGraph: graphTypeSelection: $graphTypeSelection, timeRangeSelection: $timeRangeSelection")
|
|
|
|
|
|
|
|
|
|
|
|
// test
|
|
|
|
|
|
|
|
resetBarChart()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when (graphTypeSelection) {
|
|
|
|
when (graphTypeSelection) {
|
|
|
|
GraphType.BOTTLE_EVENTS,
|
|
|
|
GraphType.BOTTLE_EVENTS,
|
|
|
|
GraphType.BOTTLE_SUM -> prepareGraph(LunaEvent.Type.BABY_BOTTLE) { state -> showBottleBarGraph(state) }
|
|
|
|
GraphType.BOTTLE_SUM -> prepareGraph(LunaEvent.Type.BABY_BOTTLE) { state -> showBottleBarGraph(state) }
|
|
|
|
GraphType.SLEEP_EVENTS,
|
|
|
|
GraphType.SLEEP_EVENTS,
|
|
|
|
GraphType.SLEEP_SUM -> prepareGraph(LunaEvent.Type.SLEEP) { state -> showSleepBarGraph(state) }
|
|
|
|
GraphType.SLEEP_SUM -> prepareGraph(LunaEvent.Type.SLEEP) { state -> showSleepBarGraph(state) }
|
|
|
|
GraphType.SLEEP_PATTERN -> prepareGraph(LunaEvent.Type.SLEEP) { state -> showSleepPatternBarGraphSlotted(state) }
|
|
|
|
GraphType.SLEEP_PATTERN -> prepareGraph(LunaEvent.Type.SLEEP) { state ->
|
|
|
|
/* if (timeRangeSelection == TimeRange.DAY) {
|
|
|
|
if (timeRangeSelection == TimeRange.DAY) {
|
|
|
|
// specialized pattern bar for daily view (optional)
|
|
|
|
// specialized pattern bar for daily view
|
|
|
|
showSleepPatternBarGraphDaily(state)
|
|
|
|
showSleepPatternBarGraphDaily(state)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
showSleepPatternBarGraphSlotted(state)
|
|
|
|
showSleepPatternBarGraphSlotted(state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
GraphType.MEDICINE_EVENTS -> prepareGraph(LunaEvent.Type.MEDICINE) { state -> showMedicineBarGraph(state) }
|
|
|
|
GraphType.MEDICINE_EVENTS -> prepareGraph(LunaEvent.Type.MEDICINE) { state -> showMedicineBarGraph(state) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -1008,10 +914,9 @@ Log.d(TAG, "showSleepPatternBarGraphSlotted; barChart.xAxis.labelCount: ${barCha
|
|
|
|
dateTime.time = Date(seconds * 1000)
|
|
|
|
dateTime.time = Date(seconds * 1000)
|
|
|
|
val years = dateTime.get(Calendar.YEAR) - 1970
|
|
|
|
val years = dateTime.get(Calendar.YEAR) - 1970
|
|
|
|
val weeks = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
|
|
val weeks = dateTime.get(Calendar.WEEK_OF_YEAR)
|
|
|
|
val month = dateTime.get(Calendar.MONTH) + 1 // month starts at 0
|
|
|
|
val month = dateTime.get(Calendar.MONTH)
|
|
|
|
|
|
|
|
// dirty hack to get monotone number of weeks
|
|
|
|
if (month == 12 && weeks == 1) {
|
|
|
|
if (month == 11 && weeks == 1) {
|
|
|
|
// The first week if the year might start in the previous year.
|
|
|
|
|
|
|
|
return 52 * (years + 1) + weeks
|
|
|
|
return 52 * (years + 1) + weeks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|