All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.jetbrains.datalore.plot.DemoAndTest.kt Maven / Gradle / Ivy

There is a newer version: 4.5.3-alpha1
Show newest version
/*
 * Copyright (c) 2021. 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.PlotSvgComponent
import jetbrains.datalore.plot.config.PlotConfig
import jetbrains.datalore.plot.config.PlotConfigClientSide
import jetbrains.datalore.plot.config.PlotConfigClientSideUtil
import jetbrains.datalore.plot.server.config.PlotConfigServerSide

object DemoAndTest {

    fun createPlot(plotSpec: MutableMap, andBuildComponent: Boolean = true): PlotSvgComponent {
        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)?
    ): PlotSvgComponent {

        PlotConfig.assertPlotSpecOrErrorMessage(plotSpec)

        @Suppress("NAME_SHADOWING")
        val plotSpec = transformPlotSpec(plotSpec)
        if (PlotConfig.isFailure(plotSpec)) {
            val errorMessage = PlotConfig.getErrorMessage(plotSpec)
            throw IllegalArgumentException(errorMessage)
        }

        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 PlotConfigClientSide.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