Skip to content

Latest commit

 

History

History
175 lines (135 loc) · 5.57 KB

File metadata and controls

175 lines (135 loc) · 5.57 KB

Using Lets-Plot Kotlin API in JVM and Kotlin/JS Applications

Library Artifacts

All artifacts are available at Maven Central.

Lets-Plot Kotlin API lets-plot-kotlin-jvm
lets-plot-kotlin-js
lets-plot-kotlin-geotools
Published by this project.
Lets-Plot (JVM) lets-plot-swing
lets-plot-batik (obsolete)
Published by the Lets-Plot project.

Using as Dependency

The following is how you configure a Gradle project with Kotlin DSL.

JVM/Swing

plugins {
    kotlin("jvm")
}

dependencies {
    // Lets-Plot Kotlin API
    implementation("org.jetbrains.lets-plot:lets-plot-kotlin-jvm:4.13.0")
    // Lets-Plot (JVM)
    implementation("org.jetbrains.lets-plot:lets-plot-swing:4.9.0")
}

Kotlin/JS

plugins {
    kotlin("multiplatform")
}

kotlin {
    ...
    sourceSets {
        named("jsMain") {
            dependencies {
                // Lets-Plot Kotlin API
                implementation("org.jetbrains.lets-plot:lets-plot-kotlin-js:4.13.0")
            }
        }
    }
}

Examples

See Lets-Plot Kotlin Mini Apps (Demos) GitHub repository:

Showing Plots (JVM/Swing)

  1. Create a figure object using the Lets-Plot Kotlin API:
val figure = letsPlot(data) + geomDensity(
                                    color = "dark-green",
                                    fill = "green",
                                    alpha = .3,
                                    size = 2.0) { x = "x" }
  1. Convert the figure object to a map of plot specification parameters:
val rawSpec = figure.toSpec()
val processedSpec = MonolithicCommon.processRawSpecs(rawSpec, frontendOnly = false)
  1. Create a plot Swing panel: SwingPlotPanel:
val panel: JPanel = SwingPlotPanel(
    processedSpec = processedSpec,
    preserveAspectRatio = preserveAspectRatio,
    preferredSizeFromPlot = false,
    repaintDelay = 10,
) { messages ->
    for (message in messages) {
      println("[Example App] $message")
    }
}

Showing Plots (Kotlin/JS)

  1. Create a figure object using the Lets-Plot Kotlin API:
val figure = letsPlot(data) + geomDensity(
                                    color = "dark-green",
                                    fill = "green",
                                    alpha = .3,
                                    size = 2.0) { x = "x" }
  1. Convert the figure object to a map of plot specification parameters:
val rawSpec = figure.toSpec()
  1. Generate HTML code:

    a) A code which you can embed in your HTML document, providing the Lets-Plot JS library is already loaded statically via a <script> tag in the document's <head> section.

    val html: String = PlotHtmlHelper.getStaticDisplayHtmlForRawSpec(rawSpec)

    b) An iframe code which includes everything needed to render the plot.

    val html: String = PlotHtmlExport.buildHtmlFromRawSpecs(
                                        plotSpec = rawSpec,
                                        scriptUrl = PlotHtmlHelper.scriptUrl(version="4.9.0"),
                                        iFrame = true
                                    )
  2. Embed the generated HTML code in your document:

    This is the tricky part because you have to make sure that the JavaScript included in the generated HTML code is not just added to DOM but also is executed by the browser.

    For example, see: JsFrontendUtil.createPlotDiv(figure).

Note: In case you are using Android WebView to display the plot:

  • enable JavaScript: webView.settings.javaScriptEnabled = true
  • grant the internet access permission (AndroidManifest.xml): <uses-permission android:name="android.permission.INTERNET" />

License

Code and documentation released under the MIT license. Copyright © 2019-2026, JetBrains s.r.o.