-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
173 additions
and
2 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
118 changes: 118 additions & 0 deletions
118
demo/plot/src/jvmBrowserMain/kotlin/demo/plot/browser/tools/PlotToolsBrowserDemoUtil.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,118 @@ | ||
/* | ||
* Copyright (c) 2019. JetBrains s.r.o. | ||
* Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
*/ | ||
|
||
package demo.plot.browser.tools | ||
|
||
import demo.common.util.demoUtils.browser.BrowserDemoUtil | ||
import kotlinx.html.* | ||
import kotlinx.html.stream.appendHTML | ||
import org.jetbrains.letsPlot.commons.geometry.DoubleVector | ||
import org.jetbrains.letsPlot.core.commons.jsObject.JsObjectSupportCommon.mapToJsObjectInitializer | ||
import org.jetbrains.letsPlot.core.spec.back.SpecTransformBackendUtil | ||
import java.io.StringWriter | ||
|
||
internal object PlotToolsBrowserDemoUtil { | ||
private const val DEMO_PROJECT = "demo/plot" | ||
private const val ROOT_ELEMENT_ID = "root" | ||
|
||
fun show( | ||
title: String, | ||
plotSpec: MutableMap<String, Any>, | ||
plotSize: DoubleVector = DoubleVector(1000.0, 600.0), | ||
applyBackendTransform: Boolean = true, | ||
backgroundColor: String = "lightgrey" | ||
) { | ||
BrowserDemoUtil.openInBrowser(DEMO_PROJECT) { | ||
getHtml( | ||
title, | ||
listOf(plotSpec), | ||
plotSize, | ||
applyBackendTransform, | ||
backgroundColor | ||
) | ||
} | ||
} | ||
|
||
private fun getPlotLibPath(): String { | ||
return BrowserDemoUtil.getPlotLibPath() | ||
} | ||
|
||
private fun getHtml( | ||
title: String, | ||
plotSpecList: List<MutableMap<String, Any>>, | ||
plotSize: DoubleVector, | ||
applyBackendTransform: Boolean, | ||
backgroundColor: String | ||
): String { | ||
|
||
val plotFun = if (applyBackendTransform) { // see: MonolithicJs | ||
"buildPlotFromProcessedSpecs" | ||
} else { | ||
"buildPlotFromRawSpecs" | ||
} | ||
|
||
val plotSpecListJs = StringBuilder("[\n") | ||
|
||
var first = true | ||
for (spec in plotSpecList) { | ||
@Suppress("NAME_SHADOWING") | ||
val spec = if (applyBackendTransform) { | ||
SpecTransformBackendUtil.processTransform(spec) | ||
} else { | ||
spec // raw: JS is going to apply transform on the client side | ||
} | ||
if (!first) plotSpecListJs.append(',') else first = false | ||
plotSpecListJs.append(mapToJsObjectInitializer(spec)) | ||
} | ||
plotSpecListJs.append("\n]") | ||
|
||
val writer = StringWriter().appendHTML().html { | ||
lang = "en" | ||
head { | ||
title(title) | ||
style { | ||
unsafe { | ||
+""" | ||
div.demo { | ||
border: 1px solid orange; | ||
margin: 20px; | ||
display: inline-block; | ||
} | ||
body { | ||
background-color:$backgroundColor | ||
} | ||
""".trimIndent() | ||
} | ||
} | ||
} | ||
body { | ||
script { | ||
type = "text/javascript" | ||
src = getPlotLibPath() | ||
} | ||
|
||
div("demo") { id = ROOT_ELEMENT_ID } | ||
|
||
script { | ||
type = "text/javascript" | ||
unsafe { | ||
+""" | ||
|var plotSpecList=$plotSpecListJs; | ||
|plotSpecList.forEach(function (spec, index) { | ||
| | ||
| var parentElement = document.createElement('div'); | ||
| document.getElementById("root").appendChild(parentElement); | ||
| LetsPlot.$plotFun(spec, ${plotSize.x}, ${plotSize.y}, parentElement); | ||
|}); | ||
""".trimMargin() | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
return writer.toString() | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
demo/plot/src/jvmBrowserMain/kotlin/demo/plot/browser/tools/ScatterToolsBrowser.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,55 @@ | ||
/* | ||
* Copyright (c) 2024. JetBrains s.r.o. | ||
* Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
*/ | ||
|
||
package demo.plot.browser.tools | ||
|
||
import demoAndTestShared.parsePlotSpec | ||
import kotlin.math.PI | ||
import kotlin.math.sin | ||
|
||
object ScatterToolsBrowser { | ||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
with(ScatterModel()) { | ||
(PlotToolsBrowserDemoUtil.show( | ||
"Scatter plot tools", | ||
plotSpec(), | ||
)) | ||
} | ||
} | ||
|
||
@Suppress("DuplicatedCode") | ||
private class ScatterModel { | ||
fun plotSpec(): MutableMap<String, Any> { | ||
val n = 50 | ||
val step = 4 * PI / n | ||
val x = List(n) { it * step } | ||
val y = List(n) { sin(it * step) } | ||
|
||
val spec = """ | ||
{ | ||
'kind': 'plot', | ||
'data': { | ||
'x': $x, | ||
'y': $y | ||
}, | ||
'mapping': { | ||
'x': 'x', | ||
'y': 'y', | ||
'color': $y | ||
}, | ||
'layers': [ | ||
{ | ||
'geom': 'point', | ||
'sampling': 'none' | ||
} | ||
] | ||
} | ||
""".trimIndent() | ||
|
||
return parsePlotSpec(spec) | ||
} | ||
} | ||
} |