“Weekly” -> { // Weekly: Showing 4 weeks in a month…
- User
“Weekly” -> { // Weekly: Showing 4 weeks in a month startCalendar.set(Calendar.DAY_OF_MONTH, 1) startCalendar.set(Calendar.HOUR_OF_DAY, 0) startCalendar.set(Calendar.MINUTE, 0) startCalendar.set(Calendar.SECOND, 0) startCalendar.set(Calendar.MILLISECOND, 0)
while (startCalendar.get(Calendar.DAY_OF_WEEK) != startCalendar.firstDayOfWeek) { startCalendar.add(Calendar.DATE, +1) }
endCalendar.time = startCalendar.time endCalendar.add(Calendar.DATE, 3 * 7) endCalendar.set(Calendar.HOUR_OF_DAY, 23) endCalendar.set(Calendar.MINUTE, 59) endCalendar.set(Calendar.SECOND, 59) endCalendar.set(Calendar.MILLISECOND, 999) dateFormat = SimpleDateFormat(“W_MMM_yyyy”, Locale.getDefault())
val c = Calendar.getInstance() c.time = startCalendar.time val newLabels = mutableListOf()
while (c.timeInMillis <= endCalendar.timeInMillis) { newLabels.add(dateFormat.format(c.time)) c.add(Calendar.DATE, 1) } xAxisLabels.value = newLabels
// xAxisLabels.value = (1…4).map { “Week $it” } bottomAxisValueFormatter = AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }
val startDocId = dateFormat.format(startCalendar.time) val endDocId = dateFormat.format(endCalendar.time) query.orderBy(FieldPath.documentId()).startAt(startDocId).endAt(endDocId) }, it successfully shows 4 weeks in April but the bottomAxis shows 2_Apr_2023 for all 4 weeks, how to fix?
- Assistant
To fix the bottom axis labels showing the same date, you can format each week label to show the starting and ending dates of the week, like this:
javadateFormat = SimpleDateFormat("W_MMM_yyyy", Locale.getDefault()) val weekDateFormat = SimpleDateFormat("d_MMM_yyyy", Locale.getDefault()) val c = Calendar.getInstance() c.time = startCalendar.time val newLabels = mutableListOf<String>() while (c.timeInMillis <= endCalendar.timeInMillis) { val startWeekDate = weekDateFormat.format(c.time) c.add(Calendar.DATE, 6) // add 6 days to get the end date of the week val endWeekDate = weekDateFormat.format(c.time) newLabels.add("$startWeekDate - $endWeekDate") c.add(Calendar.DATE, 1) // advance to the next week } xAxisLabels.value = newLabels bottomAxisValueFormatter = AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] } - User
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public inline operator fun BigDecimal.minus(other: BigDecimal): BigDecimal defined in kotlin public inline operator fun BigInteger.minus(other: BigInteger): BigInteger defined in kotlin public operator fun Iterable<TypeVariable(T)>.minus(element: TypeVariable(T)): List<TypeVariable(T)> defined in kotlin.collections public operator fun Iterable<TypeVariable(T)>.minus(elements: Array<out TypeVariable(T)>): List<TypeVariable(T)> defined in kotlin.collections public operator fun Iterable<TypeVariable(T)>.minus(elements: Iterable<TypeVariable(T)>): List<TypeVariable(T)> defined in kotlin.collections public operator fun Iterable<TypeVariable(T)>.minus(elements: Sequence<TypeVariable(T)>): List<TypeVariable(T)> defined in kotlin.collections public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(key: TypeVariable(K)): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(keys: Array<out TypeVariable(K)>): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(keys: Iterable<TypeVariable(K)>): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections public operator fun <K, V> Map<out TypeVariable(K), TypeVariable(V)>.minus(keys: Sequence<TypeVariable(K)>): Map<TypeVariable(K), TypeVariable(V)> defined in kotlin.collections public operator fun Set<TypeVariable(T)>.minus(element: TypeVariable(T)): Set<TypeVariable(T)> defined in kotlin.collections public operator fun Set<TypeVariable(T)>.minus(elements: Array<out TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections public operator fun Set<TypeVariable(T)>.minus(elements: Iterable<TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections public operator fun Set<TypeVariable(T)>.minus(elements: Sequence<TypeVariable(T)>): Set<TypeVariable(T)> defined in kotlin.collections public operator fun Sequence<TypeVariable(T)>.minus(element: TypeVariable(T)): Sequence<TypeVariable(T)> defined in kotlin.sequences public operator fun Sequence<TypeVariable(T)>.minus(elements: Array<out TypeVariable(T)>): Sequence<TypeVariable(T)> defined in kotlin.sequences public operator fun Sequence<TypeVariable(T)>.minus(elements: Iterable<TypeVariable(T)>): Sequence<TypeVariable(T)> defined in kotlin.sequences public operator fun Sequence<TypeVariable(T)>.minus(elements: Sequence<TypeVariable(T)>): Sequence<TypeVariable(T)> defined in kotlin.sequences
- Assistant
I apologize for the confusion. The error is due to using the
-operator instead of concatenating the string. Here's the corrected code:kotlinval startEndDateFormat = SimpleDateFormat("d_MMM_yyyy", Locale.getDefault()) val c = Calendar.getInstance() c.time = startCalendar.time val newLabels = mutableListOf<String>() while (c.timeInMillis <= endCalendar.timeInMillis) { val startWeekDate = startEndDateFormat.format(c.time) c.add(Calendar.DATE, 6) // add 6 days to get the end date of the week val endWeekDate = startEndDateFormat.format(c.time) newLabels.add("$startWeekDate - $endWeekDate") c.add(Calendar.DATE, 1) // advance to the next week } xAxisLabels.value = newLabels bottomAxisValueFormatter = AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }Now the xAxis labels will show the correct week ranges for the given month.
- User
then it will just show "startWeekDate - endWeekDate" for the bottomAxis value
- Assistant
Apologies for the confusion earlier. Below is the corrected code:
kotlindateFormat = SimpleDateFormat("W_MMM_yyyy", Locale.getDefault()) val weekDateFormat = SimpleDateFormat("d_MMM_yyyy", Locale.getDefault()) val c = Calendar.getInstance() c.time = startCalendar.time val newLabels = mutableListOf<String>() while (c.timeInMillis <= endCalendar.timeInMillis) { val startWeekDate = weekDateFormat.format(c.time) c.add(Calendar.DATE, 6) // add 6 days to get the end date of the week val endWeekDate = weekDateFormat.format(c.time) newLabels.add("$startWeekDate - $endWeekDate") c.add(Calendar.DATE, 1) // advance to the next week } xAxisLabels.value = newLabels bottomAxisValueFormatter = AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] } - User
I only want it to show Week 1, Week 2, Week 3 and Week 4
- Assistant
I apologize for the confusion. Here's the corrected code to display Week 1, Week 2, Week 3, and Week 4:
kotlinval c = Calendar.getInstance() c.time = startCalendar.time val newLabels = mutableListOf<String>() var weekCounter = 1 while (c.timeInMillis <= endCalendar.timeInMillis) { newLabels.add("Week $weekCounter") weekCounter += 1 c.add(Calendar.DATE, 7) // advance to the next week } xAxisLabels.value = newLabels bottomAxisValueFormatter = AxisValueFormatter { x, _ -> xAxisLabels.value[x.toInt() % xAxisLabels.value.size] }Now the bottom axis will show Week 1, Week 2, Week 3, and Week 4 as the xAxis labels.