-
Notifications
You must be signed in to change notification settings - Fork 0
/
nivo-line-chart.sc
executable file
·69 lines (61 loc) · 2.06 KB
/
nivo-line-chart.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env -S scala-cli project.scala
// ------------------------------------------------------------------------------
// Nivo line chart demo, animated !
// Run with ./nivo-line-chart.sc
// ------------------------------------------------------------------------------
import org.terminal21.client.*
import org.terminal21.client.fiberExecutor
import org.terminal21.client.components.*
import org.terminal21.client.components.std.*
import org.terminal21.client.components.nivo.*
import scala.util.Random
import NivoLineChart.*
import org.terminal21.model.ClientEvent
Sessions
.withNewSession("nivo-line-chart", "Nivo Line Chart")
.andLibraries(NivoLib /* note we need to register the NivoLib in order to use it */ )
.connect: session =>
given ConnectedSession = session
def components(events: Events): Seq[UiElement] =
val chart = ResponsiveLine(
data = createRandomData,
yScale = Scale(stacked = Some(true)),
axisBottom = Some(Axis(legend = "transportation", legendOffset = 36)),
axisLeft = Some(Axis(legend = "count", legendOffset = -40)),
legends = Seq(Legend())
)
Seq(
Paragraph(text = "Means of transportation for various countries", style = Map("margin" -> 20)),
chart
)
// we'll send new data to our controller every 2 seconds via a custom event
case object Ticker extends ClientEvent
fiberExecutor.submit:
while !session.isClosed do
Thread.sleep(2000)
session.fireEvent(Ticker)
Controller
.noModel(components)
.render()
.run()
object NivoLineChart:
def createRandomData: Seq[Serie] =
Seq(
dataFor("Japan"),
dataFor("France"),
dataFor("Greece"),
dataFor("UK"),
dataFor("Germany")
)
def dataFor(country: String): Serie =
Serie(
country,
data = Seq(
Datum("plane", rnd), // rnd = random int, see below
Datum("helicopter", rnd),
Datum("boat", rnd),
Datum("car", rnd),
Datum("submarine", rnd)
)
)
def rnd = Random.nextInt(500) + 50