David Herman
04/20/2025, 8:12 PMDavid Herman
04/20/2025, 8:12 PMRyan Scott
04/20/2025, 8:13 PMDavid Herman
04/20/2025, 8:13 PMRyan Scott
04/20/2025, 8:13 PMChart(
/* chart data */
)
Ryan Scott
04/20/2025, 8:14 PMRyan Scott
04/20/2025, 8:14 PMRyan Scott
04/20/2025, 8:16 PMkobweb {
app {
index {
description.set("Wagemix AI")
head.add {
val chartjs = routePrefix.prependTo("/chart.js")
script { src = "$chartjs/dist/chart.js" }
}
}
}
}
/* ... */
kotlin {
sourceSets {
jsMain.dependencies {
implementation(npm("chart.js", version = "4.4.9"))
}
}
}
And I've symlinked the node module:
ryan[wagemix] (main)$ readlink site/src/jsMain/resources/public/chart.js
../../../../../build/js/node_modules/chart.js/
Ryan Scott
04/20/2025, 8:17 PMpackage bindings.chartjs
import org.w3c.dom.Element
import kotlin.js.Json
external class Chart(element: Element, options: Json)
David Herman
04/20/2025, 8:17 PMRyan Scott
04/20/2025, 8:18 PMRyan Scott
04/20/2025, 8:21 PMDavid Herman
04/20/2025, 8:21 PMDavid Herman
04/20/2025, 8:21 PMRyan Scott
04/20/2025, 8:22 PMRyan Scott
04/20/2025, 8:22 PMRyan Scott
04/20/2025, 8:22 PMDavid Herman
04/20/2025, 8:22 PMRyan Scott
04/20/2025, 8:22 PMDavid Herman
04/20/2025, 8:23 PMRyan Scott
04/20/2025, 8:23 PMDavid Herman
04/20/2025, 8:23 PMDavid Herman
04/20/2025, 8:23 PMElement
but instead HtmlElementCanvas
but not sureRyan Scott
04/20/2025, 8:24 PMDavid Herman
04/20/2025, 8:25 PMRyan Scott
04/20/2025, 8:46 PM@file:JsModule("chart.js")
@file:JsNonModule
package bindings.chartjs
import org.w3c.dom.HTMLCanvasElement
import kotlin.js.Json
external class Chart(element: HTMLCanvasElement, options: Json) {
fun destroy()
}
Seems to allow the Chart
class to be found.David Herman
04/20/2025, 9:05 PMDavid Herman
04/20/2025, 9:05 PMRyan Scott
04/20/2025, 9:37 PMDavid Herman
04/20/2025, 9:37 PMRyan Scott
04/20/2025, 9:41 PMDavid Herman
04/20/2025, 9:43 PMdynamic
a lot.Ryan Scott
04/22/2025, 1:34 PMsealed class
to work quite well.
2. js: Create external interfaces that map directly to the JS objects. Here is where you set anything that is a union type to dynamic
. Here also, you're going to want to use var
properties instead of val
unless you know you're working specifically with immutable data inside of Javascript.
3. js: Create a function that map your common interfaces into the JS extern interfaces so that you can create the JS objects from the common code. Use that really convenient builder function from https://github.com/hfhbd/chartJS-compose/blob/main/src/main/kotlin/index.module_chart.js.kt
4. If necessary, map things back the other way (in my case, I don't have to do this)
It's laborious, but especially for kobweb, it can be SUPER useful to have compatibility with these really nice javascript libraries.David Herman
04/22/2025, 4:30 PMDavid Herman
04/22/2025, 4:31 PMfun internal external