You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package outwatchapp.components
  2. import scala.scalajs.js.|
  3. import scala.util.Random
  4. import colibri.ext.monix._
  5. import com.softwaremill.tagging._
  6. import monix.bio.Task
  7. import monix.eval.Coeval
  8. import monix.execution.Cancelable
  9. import outwatch.HtmlVNode
  10. import outwatch.ManagedSubscriptions.managedElement
  11. import outwatch.dsl._
  12. import typings.chartJs.mod._
  13. import typings.chartJs.mod.{^ => Chart}
  14. import typings.moment.mod.Moment
  15. import typings.std.Date
  16. import typings.std.HTMLCanvasElement
  17. import scalajs.js
  18. sealed trait ChartjsDemo
  19. object ChartjsDemo {
  20. val random = new Random()
  21. def apply(): Task[HtmlVNode @@ ChartjsDemo] = Coeval(
  22. div(
  23. canvas(
  24. managedElement { el =>
  25. val chart = new Chart(
  26. el.asInstanceOf[HTMLCanvasElement],
  27. chartConfig(ChartType.bar, randomData(100, random.nextInt()))
  28. )
  29. Cancelable(() => chart.destroy())
  30. }
  31. )
  32. ).taggedWith[ChartjsDemo]
  33. ).to[Task]
  34. def randomData(
  35. max: Int,
  36. seed: Int
  37. ): js.Array[js.UndefOr[ChartPoint | Double | scala.Null]] = {
  38. val random = new Random(seed)
  39. js.Array[js.UndefOr[ChartPoint | Double | Null]](
  40. random.nextInt(max),
  41. random.nextInt(max),
  42. random.nextInt(max),
  43. random.nextInt(max),
  44. random.nextInt(max),
  45. random.nextInt(max)
  46. )
  47. }
  48. def chartConfig(
  49. tpe: ChartType,
  50. Data: js.Array[js.UndefOr[ChartPoint | Double | Null]]
  51. ): ChartConfiguration =
  52. ChartConfiguration()
  53. .setType(tpe)
  54. .setData(
  55. ChartData()
  56. .setLabels(Labels)
  57. .setDatasets(
  58. js.Array(
  59. ChartDataSets()
  60. .setLabel("Dataset 1")
  61. .setData(Data)
  62. .setBorderWidth(1)
  63. .setBackgroundColor(BackgroundColor)
  64. .setBorderColor(BorderColor)
  65. )
  66. )
  67. )
  68. .setOptions(ChartOptions().setResponsive(true))
  69. val Labels: js.Array[
  70. String | js.Array[Date | Double | Moment | String] | Double | Date | Moment
  71. ] =
  72. js.Array("Red", "Blue", "Yellow", "Green", "Purple", "Orange")
  73. val BackgroundColor: ChartColor =
  74. js.Array(
  75. color(54, 162, 235, 0.2),
  76. color(255, 206, 86, 0.2),
  77. color(75, 192, 192, 0.2),
  78. color(153, 102, 255, 0.2),
  79. color(255, 159, 64, 0.2)
  80. )
  81. val BorderColor: ChartColor =
  82. js.Array(
  83. color(255, 99, 132, 1),
  84. color(54, 162, 235, 1),
  85. color(255, 206, 86, 1),
  86. color(75, 192, 192, 1),
  87. color(153, 102, 255, 1),
  88. color(255, 159, 64, 1)
  89. )
  90. def color(r: Int, g: Int, b: Int, a: Double): String =
  91. s"rgba($r, $g, $b, $a)"
  92. }