Parcourir la source

Fixed first break for x axis

Vadik Sirekanyan il y a 4 ans
Parent
commit
ff80b0d850
1 fichiers modifiés avec 5 ajouts et 4 suppressions
  1. 5 4
      src/main/kotlin/com/sirekanyan/andersrobot/LetsPlot.kt

+ 5 - 4
src/main/kotlin/com/sirekanyan/andersrobot/LetsPlot.kt

@@ -19,14 +19,15 @@ import java.time.format.DateTimeFormatter
 import java.util.*
 
 fun plotForecast(forecast: Forecast, locale: Locale): Plot {
+    val now = currentTimeMillis() / 1000
     val offset = ZoneOffset.ofTotalSeconds(forecast.city.timezone)
     val values = forecast.list.take(33)
     val xValues = values.map { it.dt }
     val yValues = values.map { it.main.temp }
-    val xBreaks = xBreaks(xValues, offset)
+    val xBreaks = xBreaks(now, xValues, offset)
     val yBreaks = yBreaks(yValues)
     val xLabels = xBreaks.map { it.toLocalDateTime(offset).formatDateTime("d MMM", locale) }
-    val current = (currentTimeMillis() / 1000).toLocalDateTime(offset).formatDateTime("d MMMM, HH:mm", locale)
+    val current = now.toLocalDateTime(offset).formatDateTime("d MMMM, HH:mm", locale)
     return letsPlot(mapOf("x" to xValues, "y" to yValues)) { x = "x"; y = "y" } +
             ggsize(300, 200) +
             ggtitle("${forecast.city.name}, $current") +
@@ -37,8 +38,8 @@ fun plotForecast(forecast: Forecast, locale: Locale): Plot {
             geomHLine(data = mapOf("y" to yBreaks), linetype = "dotted", color = Color.GRAY) { yintercept = "y" }
 }
 
-private fun xBreaks(times: List<Long>, offset: ZoneOffset): List<Long> {
-    val start = times.minOrNull()!!.toLocalDateTime(offset).toLocalDate()
+private fun xBreaks(now: Long, times: List<Long>, offset: ZoneOffset): List<Long> {
+    val start = now.toLocalDateTime(offset).toLocalDate()
     val end = times.maxOrNull()!!.toLocalDateTime(offset).toLocalDate()
     val breaks = mutableListOf<Long>()
     var b = start.plusDays(1)