Hi! More Chart.js hurdles... io.kvision.chart.Data...
# kvision
c
Hi! More Chart.js hurdles... io.kvision.chart.DataSets has a member minBarLegth. I think that should be minBar_Length_? Either way it doesn't seem to work. Can I work around this and pass DataSets options in a non-typed way?
r
In K/JS you can always use
dynamic
typing.
Copy code
val something = SomeObject()
something.asDynamic()["some_property"] = "some value"
you can use KVision's
obj {}
helper function to create plain JS objects
c
asDynamic() sounds like a nice trick 🙂 I'll try that.
r
But ... looking at chart module code I'm not sure it will help you ...
c
😞
r
I think you could try accessing native ChartJs instance and modify the data.
Copy code
chart.jsChart?.data = ...
chart.jsChart?.update()
You can use full dynamic typing here
And please open an issue about the typo in
minBarLength
.
c
Will do. OK, I'll try that strategy.
Probably a stupid question, but I'm struggling with the Kotlin to JS mapping. myChart.jsChart?.data?.datasets[0]?["minBarLength"] = x Is what I'm naively trying, but that doesn't compile. What's the trick of accessing a JS Array element?
r
?.get(0)
if the object is of nullable type
c
So datasets?.get(0)["minBarLength"] ? Throws some weird StringBuilder compilation error :-(
r
let me check the code ...
That's because this is not an array of dynamics. It's declared as an array of
DeepPartial
which is typealiased to
Any
. That's where
asDynamic()
will help - just use
jsChart?.data?.datasets?.get(0).asDynamic()["minBarLength"]
c
Aha, I see. That compiles but throws a not-so-helpful "null has no properties" error at runtime. Hmmm... This Kotlin to JS stuff surely is black magic to me 😄 So I'm sorry for my lack of skill here 🙂
r
I've tested this code with the bar chart from the showcase example app and it seems to work for me.
This is my code:
Copy code
val myChart = chart(
    Configuration(
        ChartType.BAR,
        listOf(
            DataSets(
                data = listOf(6, 12, 19, 13, 7),
                backgroundColor = listOf(
                    Color.hex(0x3e95cd),
                    Color.hex(0x8e5ea2),
                    Color.hex(0x3cba9f),
                    Color.hex(0xe8c3b9),
                    Color.hex(0xc45850)
                )
            )
        ),
        listOf(
            tr("Africa"),
            tr("Asia"),
            tr("Europe"),
            tr("Latin America"),
            tr("North America")
        ),
        ChartOptions(
            plugins = PluginsOptions(
                legend = LegendOptions(display = false),
                title = TitleOptions(display = false)
            ),
            scales = mapOf("y" to ChartScales(suggestedMin = 0, suggestedMax = 20))
        )
    )
)

button("Test") {
    onClick {
        myChart.jsChart?.data?.datasets?.get(0).asDynamic()["minBarLength"] = 100
        myChart.jsChart?.update()
    }
}
c
Aha, I think the problem is maybe that I'm trying to set the minBarLength property during "initialization", i.e. right after I've created the chart object? While you're setting it in an event handler. Maybe the jsChart hasn't been properly constructed at the time I'm trying to modify it?
Otherwise my code is essentially identical.
r
For this use after insert hook:
Copy code
chart(
    Configuration(
        ChartType.BAR,
        listOf(
            DataSets(
                data = listOf(6, 12, 19, 13, 7),
                backgroundColor = listOf(
                    Color.hex(0x3e95cd),
                    Color.hex(0x8e5ea2),
                    Color.hex(0x3cba9f),
                    Color.hex(0xe8c3b9),
                    Color.hex(0xc45850)
                )
            )
        ),
        listOf(
            tr("Africa"),
            tr("Asia"),
            tr("Europe"),
            tr("Latin America"),
            tr("North America")
        ),
        ChartOptions(
            plugins = PluginsOptions(
                legend = LegendOptions(display = false),
                title = TitleOptions(display = false)
            ),
            scales = mapOf("y" to ChartScales(suggestedMin = 0, suggestedMax = 20))
        )
    )
) {
    addAfterInsertHook {
        jsChart?.data?.datasets?.get(0).asDynamic()["minBarLength"] = 200
        jsChart?.update()
    }
}
c
👍 Great! That did the trick...!
TNX