Skip to content

Commit

Permalink
Seed plot tools browser demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alshan committed Apr 17, 2024
1 parent bd5a251 commit 357ebb3
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ object AreaBrowser {
@JvmStatic
fun main(args: Array<String>) {
with(Area()) {
@Suppress("UNCHECKED_CAST")
(PlotConfigBrowserDemoUtil.show(
"Area plot",
plotSpecList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ object PlotConfigBrowserDemoUtil {

val plotSpecListJs = StringBuilder("[\n")

@Suppress("UNCHECKED_CAST")
var first = true
for (spec in plotSpecList) {
@Suppress("NAME_SHADOWING")
Expand Down
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()
}
}
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)
}
}
}

0 comments on commit 357ebb3

Please sign in to comment.