Kandy with Gradle
Edit pageLast modified: 03 February 2025Using the Kandy library is possible in Kotlin projects on the JVM managed by Gradle.
Getting started with Kandy on Gradle
Install IntelliJ IDEA
From the main menu, select File | New | Project.
In the panel on the left, select New Project.
Choose Kotlin as the programming language and Gradle as the build system.
Click Create button.
Navigate to your Gradle build file, which could either be build.gradle.kts (for Kotlin DSL) or build.gradle (for Groovy DSL), and add the Kandy library as a dependency:
dependencies {
implementation("org.jetbrains.kotlinx:kandy-lets-plot:0.8.0")
}
dependencies {
implementation 'org.jetbrains.kotlinx:kandy-lets-plot:0.8.0'
}
You now have access to the Kandy library in your Kotlin project.
To also use statistical functions with Kandy, add the following to your build script:
repositories {
maven("https://packages.jetbrains.team/maven/p/kds/kotlin-ds-maven")
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlin-statistics-jvm:0.4.0")
}
repositories {
maven {
url 'https://packages.jetbrains.team/maven/p/kds/kotlin-ds-maven'
}
}
dependencies {
implementation 'org.jetbrains.kotlinx:kotlin-statistics-jvm:0.4.0'
}
warning
Note that the actual version may differ from the one specified here.
Plotting a Simple Example
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
import org.jetbrains.kotlinx.kandy.dsl.plot
import org.jetbrains.kotlinx.kandy.letsplot.export.save
import org.jetbrains.kotlinx.kandy.letsplot.layers.bars
fun main() {
// Create a DataFrame with data on average temperatures in various cities
val averageTemperature = dataFrameOf(
"city" to listOf("New York", "London", "Berlin", "Yerevan", "Tokyo"),
"average temperature" to listOf(12.5, 11.0, 9.6, 11.5, 16.0)
)
// Construct a plot using the data from the DataFrame
averageTemperature.plot {
// Add bars to the plot
// Each bar represents the average temperature in a city
bars {
x("city") // Set the cities' data on the X-axis
y("average temperature") { // Set the temperatures' data on the Y-axis
axis.name = "Average Temperature (°C)" // Assign a name to the Y-axis
}
}
// Set the title of the plot
layout.title = "Kandy Getting Started Example"
}.save("Started Example.png") // Save plot as PNG image
}
This supplementary schema outlines the key elements of Kandy's DSL, providing a quick reference to assist you in building your visualizations.
plot
For more examples, please see the Examples section.