Search code examples
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 "2.0.0"
    }
    
    group = "org.mygroup"
    version = "1.0-SNAPSHOT"
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
    
        implementation("org.jetbrains.lets-plot:lets-plot-kotlin-jvm:4.7.3")
        implementation("org.jetbrains.lets-plot:lets-plot-image-export:4.3.3")
    
        testImplementation(kotlin("test"))
    }
    
    tasks.test {
        useJUnitPlatform()
    }
    
    kotlin {
        jvmToolchain(17)
    }
    

    Sample driver

    import org.jetbrains.letsPlot.export.ggsave
    import org.jetbrains.letsPlot.geom.geomLine
    import org.jetbrains.letsPlot.letsPlot
    
    fun main() {
        val data = mapOf<String, Any>(
            "x" to listOf(0, 0.5, 1, 2),
            "y" to listOf(0, 0.25, 1, 4)
        )
    
        val plot = letsPlot(data) +
                geomLine(color = "blue", size = 2.0) { x = "x"; y = "y" }
    
        // save the .pdf file in the current dir
        ggsave(plot = plot, filename = "plot.png", path = ".")
    }
    

    enter image description here