Skip to content

Commit

Permalink
Start/time chart fine-tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
v6ak committed Oct 14, 2023
1 parent 99c9ffa commit 66585f0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions client/src/main/scala/com/example/Moment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import scala.scalajs.js.Date
@js.native
trait Moment extends js.Any {
def add(time: Int, units: String): Moment = js.native
def subtract(time: Int, units: String): Moment = js.native
//def plus(time: Int, units: String): Moment = js.native


Expand Down
3 changes: 2 additions & 1 deletion client/src/main/scala/com/v6ak/zbdb/ChartJsUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ object ChartJsUtils {

def zeroMoment = moment("2000-01-01") // We don't want to mutate it

def timeAxis(label: String) = literal(
def timeAxis(label: String, min: js.Any = js.undefined) = literal(
`type` = "time",
min = min,
time = literal(
unit = "minute",
displayFormats = literal(
Expand Down
19 changes: 18 additions & 1 deletion client/src/main/scala/com/v6ak/zbdb/Gender.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.v6ak.zbdb

import scala.scalajs.js
import scala.language.implicitConversions

abstract sealed class Gender(){
def inflect[T](feminine:T, masculine: T): T
}
Expand All @@ -11,4 +14,18 @@ object Gender{
case object Female extends Gender {
override def inflect[T](feminine: T, masculine: T): T = feminine
}
}
}

final class RichGenderSeq(val seq: Seq[Gender]) extends AnyVal {
def inflectCzech[T](feminine: T, masculine: T): T = if(seq.contains(Gender.Male)) masculine else feminine
def inflectCzech[T](feminineSingular: T, masculineSingular: T, femininePlural: T, masculinePlural: T): T = seq match {
case Seq(one) => one.inflect(feminine = feminineSingular, masculine = masculineSingular)
case _ => inflectCzech(feminine = femininePlural, masculine = masculinePlural)
}
}

object RichGenderSeq {
@inline implicit def toRichGenderSeq(seq: Seq[Gender]): RichGenderSeq = new RichGenderSeq(seq)
@inline implicit def toRichGenderSeq(seq: js.Array[Gender]): RichGenderSeq = new RichGenderSeq(seq.toSeq)

}
27 changes: 23 additions & 4 deletions client/src/main/scala/com/v6ak/zbdb/PlotRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.example.moment._
import com.example.RichMoment._
import com.v6ak.zbdb.CollectionUtils.RichMap
import com.v6ak.zbdb.TextUtils.{formatLength, formatSpeed}
import com.v6ak.zbdb.RichGenderSeq._


final class PlotRenderer(participantTable: ParticipantTable) {
Expand Down Expand Up @@ -62,14 +63,29 @@ final class PlotRenderer(participantTable: ParticipantTable) {
callbacks = literal(
label = (context: js.Dynamic) => {
val participants = context.raw.participants.asInstanceOf[js.Array[Participant]]
participants.map(_.fullName).mkString(", ") + " (" + participants.size + ")"
val names = participants.map(_.fullName).mkString(", ")
val started = participants.map(_.gender).inflectCzech(
feminineSingular = "Vyrazila",
femininePlural = "Vyrazily",
masculineSingular = "Vyrazil",
masculinePlural = "Vyrazili",
)
val finished = participants.map(_.gender).inflectCzech(
feminineSingular = "ušla",
femininePlural = "ušly",
masculineSingular = "ušel",
masculinePlural = "ušli",
)
val start = context.raw.x.asInstanceOf[Moment].hoursAndMinutes
val total = context.raw.y.asInstanceOf[Moment].hoursAndMinutes
s"$names (${participants.size}) – $started $start, celou trasu $finished za $total"
},
),
)
}

private def startTimeToTotalDurationPlot = showChartInModal(
title = "Porovnání startu a času (pouze finalisti)"
title = "Porovnání startu a celkového času (pouze finalisti)"
) { rows =>
val finishers = rows.filter(p => p.hasFinished).groupBy(p =>
(p.startTime.toString, p.partTimes.last.endTimeOption.get - p.startTime)
Expand All @@ -93,9 +109,12 @@ final class PlotRenderer(participantTable: ParticipantTable) {
),
options = literal(
scales = literal(
x = timeAxis("Čas startu"),
x = timeAxis(
label = "čas startu",
min = participantTable.startTime.subtract(5, "minutes").unix().toLong * 1000
),
y = durationAxis(
label="Celková doba",
label="celková doba",
min=zeroMoment.add(
finishers.values.flatten.map(p =>
(p.partTimes.last.endTimeOption.get - p.startTime) / 3600 / 1000
Expand Down

0 comments on commit 66585f0

Please sign in to comment.