Skip to content

Commit

Permalink
dynamicBars example (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
altavir committed Nov 20, 2020
1 parent 67e638d commit 9e676f0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
1 change: 0 additions & 1 deletion examples/src/main/kotlin/customPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin

@UnstablePlotlyAPI
fun main() {

val x1 = (0..100).map { it.toDouble() / 100.0 }
Expand Down
71 changes: 71 additions & 0 deletions examples/src/main/kotlin/misc/dynamicBars.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import hep.dataforge.meta.invoke
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kscience.plotly.Plotly
import kscience.plotly.models.Bar
import kscience.plotly.plot
import kscience.plotly.server.close
import kscience.plotly.server.pushUpdates
import kscience.plotly.server.serve
import kscience.plotly.server.show
import kotlin.random.Random


fun main() {
val initialValue = (1..10).toList()

val traces = (0..2).associate { i ->
val name = "Series $i"

name to Bar {
x.strings = initialValue.map { "Column: $it" }
y.numbers = initialValue
this.name = name
}
}

val server = Plotly.serve(port = 3872) {

//root level plots go to default page
page { plotly ->
plot(renderer = plotly) {
traces(traces.values)
layout {
title = "Other dynamic plot"
xaxis.title = "x axis name"
yaxis.title = "y axis name"
}
}
}

pushUpdates(100) // start sending updates via websocket to the front-end
}

server.show()

//Start pushing updates
GlobalScope.launch {
delay(1000)
while (isActive) {
repeat(10) { columnIndex ->
repeat(3) { seriesIndex ->
delay(100)
traces["Series $seriesIndex"]?.let {bar->
println("Updating ${bar.name}, Column $columnIndex")
//TODO replace with dynamic data API
val yValues = bar.y.doubles
yValues[columnIndex] = Random.nextInt(0,100).toDouble()
bar.y.doubles = yValues
}
}
}
}
}

println("Press Enter to close server")
readLine()

server.close()
}

0 comments on commit 9e676f0

Please sign in to comment.