Can I use Kandy also completely and flawless in a ...
# datascience
e
Can I use Kandy also completely and flawless in a normal kt file? I have this function, which works fine in a Kotlin notebook, but NOT in a kt file:
Copy code
import org.jetbrains.kotlinx.dataframe.DataFrame
import org.jetbrains.kotlinx.kandy.dsl.plot
import org.jetbrains.kotlinx.kandy.letsplot.layers.line
import org.jetbrains.kotlinx.kandy.util.color.Color


fun visualizeCustomerNumber(df: DataFrame<*>) {
    df.plot {
        line {
            x("createdAt") {
                axis {
                    name = "Date"
                }
            }
            y("customerNumber") {
                axis {
                    name = "Customer Number"
                }
            }
            color = Color.RED
        }
        layout {
            title = "Tomorrow Customer Number"
        }
    }
}
The keywords: |axis, name, layout, title| are recognized by IntelliJ The import in the notebook on the other hand works perfectly with: %use kandy This is the version I use in my build.gradle.kts: dependencies { implementation("org.jetbrains.kotlinxkandy lets plot0.8.0-RC1") }
a
Hello.
layout {}
is an extension function and should be imported:
Copy code
import org.jetbrains.kotlinx.kandy.letsplot.feature.layout
👍 2
e
Thx for your answer and your support! "layout" and "title" are now recognized by the IDE. It also recognizes "axis", but as soon as I write the { after it, “axis” is highlighted in red again. I had to use a different syntax for defining the names of the axes:
Copy code
fun visualizeCustomerNumber(df: DataFrame<*>) {
    df.plot {
        line {
            x("createdAt") {
                axis.name = "Date"
            }
            y("customerNumber") {
                axis.name = "Customer Number"
            }
            color = Color.RED
        }
        layout {
            title = "Tomorrow Customer Number"
        }
    }
}
Then it works. This means I have to use a different syntax depending on whether I am working in a notebook or a kt file. Why is that?
a
Oh, you need
import org.jetbrains.kotlinx.kandy.util.context.invoke
. For some reason, IDEA doesn’t recognise it 😞.
👍 1
e
Cool, this works. "axis" and "name" are now recognized. The only thing I still had to do was set the column names in quotes, which I didn't have to do in the notebook. Thanks for your support ✨
a
Yes, when you’re working in a notebook, a dataframe extension properties are generated for each column. To make this work and in IDEA, you can add a DataFrame plugin (and set the data schema). See kotlin dataframe docs
👍 1