androidkotlinjetbrains-idelets-plot

Which android view should be used to display a jetbrains.letsPlot() + geomHistogram() -object?


I am having some trouble including a Plot-object (produced by jetbrains.letsPlot() + geomHistogram()) within the activity.xml for a Kotlin Android app. I am familiar with basic activity elements but I don't know which one to use in this case and I can not figure it out using https://github.com/JetBrains/lets-plot.

Any help is very appreciated!


Solution

  • You can use Android' WebView to display a plot.

    jetbrains.letsPlot() + geomHistogram() produces a Figure object.

    First you will have to convert it to a simple Map object using toSpec() method:

            val spec = when (plot) {
                is Plot -> plot.toSpec()
                else -> (plot as GGBunch).toSpec()
            }
    
    

    Next, you will generate a string (plot HTML representation) using PlotHtmlHelper:

    val html = PlotHtmlHelper.getStaticDisplayHtmlForRawSpec(spec)
    

    This way you can generate a web page and load it to WebView. The page should also contain a script tag for loading the Lets-Plot JS library.

    The code could look similar to this (but with a newer library version - 2.2.1 currently): https://github.com/JetBrains/lets-plot-kotlin/blob/master/demo/browser/src/main/kotlin/frontendContextDemo/BrowserDemoFrontendContext.kt

    If you are using Kotlin/JS you may want to take a look at this example as well: https://github.com/alshan/lets-plot-mini-apps/blob/main/js-ir-frontend-app/src/main/kotlin/Main.kt


    Update (oct 17'23)

    Check out new Lets-Plot Skia Frontend. You can now target Android in a Compose Multiplatform project.