1+ import hep.dataforge.meta.invoke
12import kotlinx.coroutines.GlobalScope
23import kotlinx.coroutines.delay
34import kotlinx.coroutines.isActive
45import kotlinx.coroutines.launch
56import kotlinx.html.a
67import kotlinx.html.h1
78import kscience.plotly.Plotly
8- import kscience.plotly.layout
99import kscience.plotly.models.Trace
1010import kscience.plotly.models.invoke
1111import kscience.plotly.plot
@@ -14,25 +14,30 @@ import kscience.plotly.server.pushUpdates
1414import kscience.plotly.server.serve
1515import kscience.plotly.server.show
1616import kotlin.math.PI
17+ import kotlin.math.cos
1718import kotlin.math.sin
1819
1920
2021fun main () {
2122
22- val server = Plotly .serve(port = 3872 ) {
23+ val freq = 1.0 / 1000
24+ val oscillationFreq = 1.0 / 10000
2325
24- val x = (0 .. 100 ).map { it.toDouble() / 100.0 }
25- val y = x.map { sin(2.0 * PI * it) }
26+ val x = (0 .. 100 ).map { it.toDouble() / 100.0 }
27+ val sinY = x.map { sin(2.0 * PI * it) }
28+ val cosY = x.map { cos(2.0 * PI * it) }
2629
27- val trace = Trace .invoke(x, y) { name = " sin" }
30+ val sinTrace = Trace (x, sinY) { name = " sin" }
31+ val cosTrace = Trace (x, cosY) { name = " cos" }
2832
33+ val server = Plotly .serve(port = 3872 ) {
2934
3035 // root level plots go to default page
3136 page { plotly ->
3237 h1 { + " This is the plot page" }
3338 a(" /other" ) { + " The other page" }
3439 plot(renderer = plotly) {
35- traces(trace )
40+ traces(sinTrace, cosTrace )
3641 layout {
3742 title = " Other dynamic plot"
3843 xaxis.title = " x axis name"
@@ -45,7 +50,7 @@ fun main() {
4550 h1 { + " This is the other plot page" }
4651 a(" /" ) { + " Back to the main page" }
4752 plot(renderer = plotly) {
48- traces(trace )
53+ traces(sinTrace )
4954 layout {
5055 title = " Dynamic plot"
5156 xaxis.title = " x axis name"
@@ -54,20 +59,24 @@ fun main() {
5459 }
5560 }
5661
57- GlobalScope .launch {
58- var time: Long = 0
59- while (isActive) {
60- delay(10 )
61- time + = 10
62- val dynamicY = x.map { sin(2.0 * PI * (it + time.toDouble() / 1000.0 )) }
63- trace.y.set(dynamicY)
64- }
65- }
6662 pushUpdates(50 ) // start sending updates via websocket to the front-end
6763 }
6864
6965 server.show()
7066
67+ // Start pushing updates
68+ GlobalScope .launch {
69+ var time: Long = 0
70+
71+ while (isActive) {
72+ delay(10 )
73+ time + = 10
74+ sinTrace.y.numbers = x.map { sin(2.0 * PI * (it + time.toDouble() * freq)) }
75+ val cosAmp = cos(2.0 * PI * oscillationFreq * time)
76+ cosTrace.y.numbers = x.map { cos(2.0 * PI * (it + time.toDouble() * freq)) * cosAmp }
77+ }
78+ }
79+
7180 println (" Press Enter to close server" )
7281 readLine()
7382
0 commit comments