## Using Lets-Plot Kotlin API in JVM and Kotlin/JS Applications
### Library Artifacts
All artifacts are available at [Maven Central](https://search.maven.org/search?q=lets-plot).
| 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
```Kotlin
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
```Kotlin
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)](https://github.com/alshan/lets-plot-mini-apps) GitHub repository:
- [JVM/Swing app examples](https://github.com/alshan/lets-plot-mini-apps/blob/main/jvm-swing-app)
- [Kotlin/JS app example](https://github.com/alshan/lets-plot-mini-apps/tree/main/js-frontend-app)
- [JVM plot export examples](https://github.com/alshan/lets-plot-mini-apps/tree/main/jvm-plot-export/src/main/kotlin):
Runnable examples that show how to export plot to an SVG, HTML or PNG image using
`PlotSvgExport`, `PlotHtmlExport` or `PlotImageExport` utilities.
### Showing Plots (JVM/Swing)
1. Create a figure object using the Lets-Plot Kotlin API:
```kotlin
val figure = letsPlot(data) + geomDensity(
color = "dark-green",
fill = "green",
alpha = .3,
size = 2.0) { x = "x" }
```
2. Convert the figure object to a map of plot specification parameters:
```kotlin
val rawSpec = figure.toSpec()
val processedSpec = MonolithicCommon.processRawSpecs(rawSpec, frontendOnly = false)
```
3. Create a plot Swing panel: [SwingPlotPanel](https://github.com/JetBrains/lets-plot/blob/master/platf-awt/src/main/kotlin/org/jetbrains/letsPlot/awt/plot/swing/SwingPlotPanel.kt):
```kotlin
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:
```kotlin
val figure = letsPlot(data) + geomDensity(
color = "dark-green",
fill = "green",
alpha = .3,
size = 2.0) { x = "x" }
```
2. Convert the figure object to a map of plot specification parameters:
```kotlin
val rawSpec = figure.toSpec()
```
3. 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 `