kotlingradleintellij-idealets-plot

Kotlin: how to import Lets Plot in IDEA


I am new to Jetbrains IDEA editor. I have a console application project. How can I add Let's Plot library/package/what-do-you-call-it so I can plot some functions and export them to say SVG files?

I found this question helpful, however it does not show how to import it, only how to use it: Kotlin lets-plot: minimal example


Solution

  • In IntelliJ: File -> New Project -> Gradle + GradleDSL=Kotlin

    Gradle - build.gradle.kts

    plugins {
        kotlin("jvm") version "1.9.20"
    }
    
    group = "org.mygroup"
    version = "1.0-SNAPSHOT"
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
    
        implementation("org.jetbrains.lets-plot:lets-plot-kotlin-jvm:4.5.0")
        implementation("org.jetbrains.lets-plot:lets-plot-image-export:4.1.0")
    
        testImplementation(kotlin("test"))
    }
    
    tasks.test {
        useJUnitPlatform()
    }
    
    kotlin {
        jvmToolchain(17)
    }
    

    Sample driver

    import org.jetbrains.letsPlot.export.ggsave
    import org.jetbrains.letsPlot.geom.geomPoint
    import org.jetbrains.letsPlot.letsPlot
    
    fun main() {
        val xs = listOf(0,  0.5, 1, 2)
        val ys = listOf(0, 0.25, 1, 4)
        val data = mapOf<String, Any>("x" to xs, "y" to ys)
    
        val fig = letsPlot(data) + geomPoint(
            color = "dark-green",
            size = 4.0
        ) { x = "x"; y = "y" }
    
        // save .svg file in the current dir
        ggsave(plot = fig, filename = "fig.svg", path = ".")
    }