visionforge/plotly/examples/notebooks/plotlykt-server-demo.ipynb

4.7 KiB

In [ ]:
import space.kscience.plotly.server.PlotlyServerIntegration
import space.kscience.plotly.server.jupyter

USE(PlotlyServerIntegration())
In [ ]:
import kotlin.math.*
In [ ]:
val x1 = (0..100).map { it.toDouble() / 100.0 }
val y1 = x1.map { sin(2.0 * PI * it) }
val y2 = x1.map { cos(2.0 * PI * it) }

val trace1 = Trace(x1, y1) { name = "sin" }
val trace2 = Trace(x1, y2) { name = "cos" }

val fragment = Plotly.fragment { renderer ->
    val plotConfig = PlotlyConfig {
        responsive = true
    }

    plot("above", config = plotConfig, renderer = renderer) {
        traces(trace1, trace2)
        layout {
            title = "The plot above"
            xaxis.title = "x axis name"
            yaxis.title = "y axis name"
        }
    }
    hr()
    h1 { +"A custom separator" }
    hr()
    div {
        plot("below", renderer = renderer) {
            traces(trace1, trace2)
            layout {
                title = "The plot below"
                xaxis.title = "x axis name"
                yaxis.title = "y axis name"
            }
        }
    }
}
fragment
In [ ]:
Plotly.plot {
    traces(trace1, trace2)
    layout {
        title = "The plot below"
        xaxis.title = "x axis name"
        yaxis.title = "y axis name"
    }
}
In [ ]:
val x = (0..100).map { it.toDouble() / 100.0 }
val y = x.map { sin(2.0 * PI * it) }

val trace = Trace(x, y) { name = "sin" }


val dynamicPlot = Plotly.plot {
    traces(trace)
    layout {
        title = "Dynamic plot"
        xaxis.title = "x axis name"
        yaxis.title = "y axis name"
    }
}

dynamicPlot
In [ ]:
import kotlinx.coroutines.*

val job = GlobalScope.launch {
    var time: Long = 0
    while (isActive) {
        delay(10)
        time += 10
        val dynamicY = x.map { sin(2.0 * PI * (it + time.toDouble() / 1000.0)) }
        trace.y.set(dynamicY)
    }
}
In [ ]:
job.cancel()
In [ ]:
dynamicPlot.layout.xaxis.title = "крокозябра"
In [ ]:
Plotly.jupyter.port = 8884
In [ ]:
Plotly.jupyter.port
In [ ]: