Simplifying Plotly Functionality with Kotlin-Multiplatform

Blake Bradford Avatar

·

Are you a software engineer or solution architect looking for a simplified way to access Plotly functionality? Look no further than Plotly.kt, a Kotlin-Multiplatform library that provides a seamless integration with the popular data visualization library, Plotly. In this article, we will delve into the key features of Plotly.kt, explore the different rendering modes it offers, and highlight its experimental capabilities.

Plotly.kt: A Brief Overview

Plotly.kt is designed to provide easy access to Plotly functionality from Kotlin-Multiplatform. With Plotly.kt, you can create plotly configurations and render them as plotly charts. The library supports three drawable plot objects: Plot, PlotFragment, and PlotlyPage.

  • Plot: A stand-alone plot frame that requires external infrastructure to load appropriate JavaScript libraries.
  • PlotFragment: An HTML fragment that may include several plots. HTML API is provided by the kotlinx-html library.
  • PlotlyPage: A complete page that includes body fragments and page headers required to load the JavaScript part of Plotly.

Rendering Modes: From Static HTML Export to Dynamic Updates

Plotly.kt supports various rendering modes that cater to different use cases. Let’s explore them one by one:

  1. HTML Page Export: Using this mode, you can export a plot or page as a standalone HTML file. It supports both CDN distribution and local JS file options.
  2. JVM and Native: Export plots or pages in standalone HTML files using CDN distribution or local JS files (JVM only). Note that this mode does not support updates.

  3. Ktor-based Server with Dynamic Updates: This mode, available only for JVM, provides a Ktor CIO server with full multi-page and update capabilities. It allows you to serve dynamic plots and provides a rich interactive experience.

  4. Kotlin-JS: While Plotly is a JavaScript library, Plotly.kt makes it convenient to use with Kotlin-JS. The library has its own object structure, can generate stand-alone models, and offers internal optimizations.

  5. JavaFX Browser: Plotly.kt can be integrated with a JavaFX browser, allowing you to run Plotly.kt in a JavaFX environment. An example project is provided in the fx-demo module.

  6. Kotlin Jupyter Kernel: Plotly.kt offers beta-version support for integration with the Kotlin Jupyter kernel. You can render static plots in Jupyter Lab and dynamic plots using the plotly-server module. Note that for dynamic pages, you need to pass the renderer parameter explicitly to the plot.

  7. Direct Image Render via Orca (Experimental): Plotly.kt provides experimental support for direct rendering of plots to raster or vector images using the Plotly Orca application. You can use the Plot.export extension to call Orca. Note that Orca needs to be installed on the system and available on the system path.

  8. Kotlin-Scripting (Experimental): Plotly.kt allows you to separate script logic into a stand-alone plotly.kts script file and generate an HTML file from the command line. Refer to the plotlykt-script module for details.

Build and Usage

To start using Plotly.kt, you need to define the following configuration in your gradle.kts file:

“`kotlin
plugins {
kotlin(“jvm”)
}

repositories {
maven(“https://repo.kotlin.link”)
}

dependencies {
implementation(“space.kscience:plotlykt-server:$version”)
}
“`

If you don’t need the server, you can use plotlykt-core instead.

Planned Features and Contributions

The Plotly.kt project has an exciting roadmap with several planned features, including table widgets, server-side plot events, an online plot editor, dynamic data support, and Mathjax and LaTeX support. The library is an open-source project that welcomes contributions from the community. If you encounter a missing feature, you have three options: contribute to the project, create a model or extension in your project, or add missing features directly into the configuration.

The Plotly.kt project acknowledges and appreciates the contributions made by its creators and contributors. Special thanks go to Vasily Chernov, Alexander Nozik, Mikhail Zeleniy, and Ekaterina Samorodova. The project has also received partial funding from JetBrains Research grant.

In conclusion, Plotly.kt is a powerful Kotlin-Multiplatform library that simplifies Plotly functionality, enabling software engineers and solution architects to create interactive and visually appealing data visualizations. Whether you are working with JVM, Kotlin-JS, JavaFX, or Kotlin Jupyter kernel, Plotly.kt has got you covered. Stay tuned for new updates and features as the project continues to evolve. If you have any questions, feel free to ask!

References

Category: Software Engineering

Tags: Kotlin, Plotly, Kotlin-Multiplatform, Data Visualization, Kotlin-JS, Kotlin Jupyter, JavaFX, Orca, Kotlin scripting

Leave a Reply

Your email address will not be published. Required fields are marked *