-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Chart.js for gender structure and start/duration plots (#42)
- Loading branch information
Showing
4 changed files
with
196 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.v6ak.zbdb | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation._ | ||
import org.scalajs.dom._ | ||
import scala.scalajs.js.Dynamic.literal | ||
import scala.scalajs.js.JSConverters._ | ||
import scalatags.JsDom.all._ | ||
import Bootstrap.DialogUtils | ||
import com.example.moment.{Moment, moment} | ||
|
||
@JSGlobal @js.native class Chart(el: Element, data: js.Any) extends js.Object { | ||
def resize(): Unit = js.native | ||
def update(): Unit = js.native | ||
def destroy(): Unit = js.native | ||
} | ||
|
||
object ChartJsUtils { | ||
// Chart.register(…) | ||
|
||
def zeroMoment = moment("2000-01-01") // We don't want to mutate it | ||
|
||
def timeAxis(label: String) = literal( | ||
`type` = "time", | ||
time = literal( | ||
unit = "minute", | ||
displayFormats = literal( | ||
minute = "HH:mm", | ||
), | ||
), | ||
ticks = literal( | ||
stepSize = 5, | ||
), | ||
title = literal( | ||
display = true, | ||
text = label, | ||
), | ||
) | ||
|
||
def durationAxis(label: String, min: js.Any) = literal( | ||
`type` = "time", | ||
min = min, | ||
ticks = literal( | ||
callback = (value: Moment, _index: js.Dynamic, _ticks: js.Dynamic) => { | ||
val diff = value - zeroMoment | ||
val hours = diff / 1000 / 60 / 60 | ||
val minutes = diff / 1000 / 60 - hours * 60 | ||
f"$hours%02d:$minutes%02d" | ||
}, | ||
stepSize = 30, | ||
), | ||
title = literal( | ||
display = true, | ||
text = label, | ||
), | ||
) | ||
|
||
def dataFromPairs(seq: Seq[(Any, Any)]) = literal( | ||
labels = seq.map(_._1).toJSArray, | ||
datasets = js.Array( | ||
literal( | ||
data = seq.map(_._2).toJSArray, | ||
) | ||
) | ||
) | ||
|
||
def dataFromTriples(seq: Seq[(Any, Any, String)]) = literal( | ||
labels = seq.map(_._1).toJSArray, | ||
datasets = js.Array( | ||
literal( | ||
backgroundColor = seq.map(_._3).toJSArray, | ||
data = seq.map(_._2).toJSArray, | ||
) | ||
) | ||
) | ||
|
||
def showChartInModal(title: String = null)(f: Seq[Participant] => js.Any): (Option[String], (String, => Seq[Participant], ParticipantTable) => Unit) = | ||
Option(title) -> ((modalBodyId: String, rowsLoader, participantTable: Any) => { | ||
val el = window.document.getElementById(modalBodyId).asInstanceOf[HTMLElement] | ||
el.style.maxHeight = "90vh" | ||
el.style.maxWidth = "90vw" | ||
|
||
val can = canvas().render | ||
el.appendChild(can) | ||
|
||
val plotParams = f(rowsLoader) | ||
console.log("plotParams", plotParams) | ||
val chart = new Chart(can, plotParams) | ||
val resizeHandler: Event => Unit = _ => { | ||
//chart.update() | ||
chart.resize() | ||
} | ||
window.addEventListener("resize", resizeHandler) | ||
|
||
def destroy(): Unit = { | ||
window.removeEventListener("resize", resizeHandler) | ||
chart.destroy() | ||
} | ||
|
||
val modalElement = el.parentNode.parentNode.parentNode.asInstanceOf[Element] | ||
modalElement.onBsModalHidden(() => destroy()) | ||
} | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters