commonMain.jetbrains.datalore.plot.DemoAndTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lets-plot-common Show documentation
Show all versions of lets-plot-common Show documentation
Lets-Plot JVM package without rendering part
/*
* 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 jetbrains.datalore.plot
import jetbrains.datalore.base.geometry.DoubleVector
import jetbrains.datalore.plot.builder.Plot
import jetbrains.datalore.plot.config.PlotConfig
import jetbrains.datalore.plot.config.PlotConfigClientSide
import jetbrains.datalore.plot.config.PlotConfigClientSideUtil
import jetbrains.datalore.plot.server.config.PlotConfigClientSideJvmJs
import jetbrains.datalore.plot.server.config.PlotConfigServerSide
object DemoAndTest {
fun createPlot(plotSpec: MutableMap, andBuildComponent: Boolean = true): Plot {
val plot = createPlot(plotSpec) {
for (s in it) {
println("PLOT MESSAGE: $s")
}
}
if (andBuildComponent) {
plot.ensureBuilt()
}
return plot
}
private fun createPlot(
plotSpec: MutableMap,
computationMessagesHandler: ((List) -> Unit)?
): Plot {
PlotConfig.assertPlotSpecOrErrorMessage(plotSpec)
@Suppress("NAME_SHADOWING")
val plotSpec = transformPlotSpec(plotSpec)
if (PlotConfig.isFailure(plotSpec)) {
val errorMessage = PlotConfig.getErrorMessage(plotSpec)
throw IllegalArgumentException(errorMessage)
}
// if (computationMessagesHandler != null) {
// val computationMessages = PlotConfigUtil.findComputationMessages(plotSpec)
// if (!computationMessages.isEmpty()) {
// computationMessagesHandler(computationMessages)
// }
// }
//
val config = PlotConfigClientSide.create(plotSpec) { messages ->
if (computationMessagesHandler != null && messages.isNotEmpty()) {
computationMessagesHandler(messages)
}
}
val assembler = PlotConfigClientSideUtil.createPlotAssembler(config)
return assembler.createPlot()
}
private fun transformPlotSpec(plotSpec: MutableMap): MutableMap {
@Suppress("NAME_SHADOWING")
var plotSpec = plotSpec
plotSpec = PlotConfigServerSide.processTransform(plotSpec)
return PlotConfigClientSideJvmJs.processTransform(plotSpec)
}
fun contourDemoData(): Map> {
val countX = 20
val countY = 20
val mean = DoubleVector(5.0, 5.0)
val height = 1.0
val radius = 10.0
val slop = height / radius
val x = ArrayList()
val y = ArrayList()
val z = ArrayList()
for (row in 0 until countY) {
for (col in 0 until countX) {
val dist = DoubleVector(col.toDouble(), row.toDouble()).subtract(mean).length()
val v = if (dist >= radius)
0.0
else
height - dist * slop
x.add(col.toDouble())
y.add(row.toDouble())
z.add(v)
}
}
val map = HashMap>()
map["x"] = x
map["y"] = y
map["z"] = z
return map
}
fun getMap(opts: Map, key: String): Map {
@Suppress("UNCHECKED_CAST")
val map = opts[key] as? Map
return map ?: emptyMap()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy