-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from SciProgCentre/dev
0.7.0-dev-1
- Loading branch information
Showing
37 changed files
with
385 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
kotlin("multiplatform") | ||
id("org.jetbrains.compose") version "1.5.12" | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
maven("https://repo.kotlin.link") | ||
maven("https://jogamp.org/deployment/maven") | ||
} | ||
|
||
kotlin{ | ||
jvm() | ||
jvmToolchain(17) | ||
sourceSets{ | ||
jvmMain{ | ||
dependencies { | ||
implementation(compose.runtime) | ||
implementation(compose.foundation) | ||
implementation(compose.material) | ||
implementation(compose.desktop.currentOs) | ||
implementation("io.github.kevinnzou:compose-webview-multiplatform:1.8.6") | ||
implementation(projects.plotlyktServer) | ||
implementation(spclibs.logback.classic) | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions.freeCompilerArgs += "-Xopt-in=kotlin.RequiresOptIn" | ||
} | ||
|
||
compose { | ||
desktop { | ||
application { | ||
mainClass = "space.kscience.plotly.compose.AppKt" | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
examples/compose-demo/src/jvmMain/kotlin/space/kscience/plotly/compose/App.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package space.kscience.plotly.compose | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.window.Window | ||
import androidx.compose.ui.window.application | ||
import com.multiplatform.webview.web.LoadingState | ||
import com.multiplatform.webview.web.WebView | ||
import com.multiplatform.webview.web.rememberWebViewNavigator | ||
import com.multiplatform.webview.web.rememberWebViewStateWithHTMLData | ||
import dev.datlag.kcef.KCEF | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
private val allowedPages = listOf( | ||
"Static", | ||
"Dynamic" | ||
) | ||
|
||
@Composable | ||
fun App() { | ||
val scaleFlow = remember { MutableStateFlow(1f) } | ||
val scale by scaleFlow.collectAsState() | ||
val scope = rememberCoroutineScope() | ||
val server = remember { | ||
scope.servePlots(scaleFlow) | ||
} | ||
|
||
val state = rememberWebViewStateWithHTMLData(staticPlot()) | ||
|
||
val navigator = rememberWebViewNavigator() | ||
|
||
val loadingState = state.loadingState | ||
if (loadingState is LoadingState.Loading) { | ||
LinearProgressIndicator( | ||
progress = loadingState.progress, | ||
modifier = Modifier.fillMaxWidth() | ||
) | ||
} | ||
|
||
Row(Modifier.fillMaxSize()) { | ||
Column(Modifier.width(300.dp)) { | ||
Button({ navigator.loadHtml(staticPlot()) }, modifier = Modifier.fillMaxWidth()) { | ||
Text("Static") | ||
} | ||
Button({ navigator.loadUrl("http://localhost:7778/Dynamic") }, modifier = Modifier.fillMaxWidth()) { | ||
Text("Dynamic") | ||
} | ||
|
||
Slider( | ||
scale, | ||
{ scaleFlow.value = it }, | ||
valueRange = 0.1f..10f, | ||
modifier = Modifier.fillMaxWidth() | ||
) | ||
} | ||
Column(Modifier.fillMaxSize()) { | ||
|
||
WebView( | ||
state = state, | ||
navigator = navigator, | ||
modifier = Modifier.fillMaxSize() | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun main() = application { | ||
KCEF.initBlocking( | ||
builder = { | ||
progress { | ||
onDownloading { | ||
println("Download progress: $it%") | ||
} | ||
} | ||
release(true) | ||
} | ||
) | ||
Window(onCloseRequest = ::exitApplication) { | ||
MaterialTheme { | ||
App() | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
examples/compose-demo/src/jvmMain/kotlin/space/kscience/plotly/compose/server.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package space.kscience.plotly.compose | ||
|
||
import io.ktor.server.engine.ApplicationEngine | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.isActive | ||
import kotlinx.coroutines.launch | ||
import space.kscience.plotly.* | ||
import space.kscience.plotly.models.Scatter | ||
import space.kscience.plotly.models.invoke | ||
import space.kscience.plotly.server.pushUpdates | ||
import space.kscience.plotly.server.serve | ||
import kotlin.math.PI | ||
import kotlin.math.cos | ||
import kotlin.math.sin | ||
|
||
fun staticPlot(): String = Plotly.page { | ||
val x = (0..100).map { it.toDouble() / 100.0 }.toDoubleArray() | ||
val y1 = x.map { sin(2.0 * PI * it) }.toDoubleArray() | ||
val y2 = x.map { cos(2.0 * PI * it) }.toDoubleArray() | ||
val trace1 = Scatter(x, y1) { | ||
name = "sin" | ||
} | ||
val trace2 = Scatter(x, y2) { | ||
name = "cos" | ||
} | ||
plot(config = PlotlyConfig { responsive = true }) {//static plot | ||
traces(trace1, trace2) | ||
layout { | ||
title = "First graph, row: 1, size: 8/12" | ||
xaxis.title = "x axis name" | ||
yaxis { title = "y axis name" } | ||
} | ||
} | ||
}.render() | ||
|
||
fun CoroutineScope.servePlots(scale: StateFlow<Number>): ApplicationEngine = Plotly.serve(this, port = 7778) { | ||
page("Static") { container -> | ||
val x = (0..100).map { it.toDouble() / 100.0 }.toDoubleArray() | ||
val y1 = x.map { sin(2.0 * PI * it) }.toDoubleArray() | ||
val y2 = x.map { cos(2.0 * PI * it) }.toDoubleArray() | ||
val trace1 = Scatter(x, y1) { | ||
name = "sin" | ||
} | ||
val trace2 = Scatter(x, y2) { | ||
name = "cos" | ||
} | ||
plot(renderer = container) {//static plot | ||
traces(trace1, trace2) | ||
layout { | ||
title = "First graph, row: 1, size: 8/12" | ||
xaxis.title = "x axis name" | ||
yaxis { title = "y axis name" } | ||
} | ||
} | ||
} | ||
|
||
page("Dynamic") { container -> | ||
val x = (0..100).map { it.toDouble() / 100.0 } | ||
val y = x.map { sin(2.0 * PI * it) } | ||
|
||
val trace = Scatter(x, y) { name = "sin" } | ||
|
||
val plot = plot("dynamic", config = PlotlyConfig { responsive = true }, renderer = container) { | ||
traces(trace) | ||
layout { | ||
title = "Dynamic plot" | ||
xaxis.title = "x axis name" | ||
yaxis.title = "y axis name" | ||
} | ||
} | ||
|
||
launch { | ||
var time: Long = 0 | ||
while (isActive) { | ||
delay(10) | ||
time += 10 | ||
val frequency = scale.value.toDouble() | ||
val dynamicY = x.map { sin(2.0 * PI * frequency * (it + time.toDouble() / 1000.0)) } | ||
//trace.y.numbers = dynamicY | ||
plot.data[0].y.numbers = dynamicY | ||
plot.layout { | ||
xaxis.title = "x axis name (t = $time)" | ||
} | ||
} | ||
} | ||
} | ||
pushUpdates(100) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.