SciProgCentre / plotly.kt

An interactive Kotlin wrapper for plotly visualization tools
https://sciprogcentre.github.io/plotly.kt/
Apache License 2.0
147 stars 21 forks source link

Using Plotly.js Events #95

Closed Joseph-Hui closed 11 months ago

Joseph-Hui commented 1 year ago

I am trying to use the Plotly Event to control the behaviours of the hover and click on the data points of the charts. According to the description in here, the plot div object has an .on() method for customising the behaviours when the Plotly events are received. However, I cannot see the .on() in the corresponding Kotlin wrapper. Besides, I tried to use the js() to directly call the .on() on the Javascript object and it doesn't work neither. What could the problem be?

    val plot = Plotly.plot {
        scatter {
            x(1, 2, 3, 4)
            y(10, 15, 13, 17)
            mode = ScatterMode.markers
            type = TraceType.scatter
//            hoverinfo = "none"
        }
    }
    val serialized = plot.toJsonString()
    js("plot.on('plotly_click', function() {alert('You clicked this Plotly chart!');})")  // not work

    val deserialized = Plot(Json.decodeFromString(MetaSerializer, serialized as String).toMutableMeta())
    js("deserialized.on('plotly_click', function() {alert('You clicked this Plotly chart!');})") // not work

    element.append.plotDiv(plot = deserialized)

Moreover, I noticed in the source code, the version of Plotly.js used is 1.54.6. While the latest version of Plotly.js is 2.24.1. Would this be the problem?

SPC-code commented 1 year ago

Thank you for raising the issue. Indeed, we do not support backward event propagation at the moment, because it requires more complicated machinery in terms of communication. It will be easier to add it to VisionForge integration, which support backward event flow.

Evaluating events should be possible in js version, but you should use div used in polo, to the plot object. I will investigate it and get back to you.

We've moved to 2.x version of Plotly on dev branch, but it should not affect the problem in question.

Joseph-Hui commented 1 year ago

Yes, I am using Plotly,kt in a Kotlin/JS project. I come up with a temporary solution by defining the plotDiv object as dynamic, and then I can call the .on() method without compilation problem. I have posted the solution in here. I think this can be easily be fixed by including the .on() method in the wrapper.

Thank you very much for your response!

SPC-code commented 1 year ago

I will try to add this ability in the next release. Maybe we can also add JVM event reaction in VisionForge.

SPC-code commented 1 year ago

I've added API to work with Plotly events in JS. It is not stable yet because it is not that easy to work with those events.

SPC-code commented 1 year ago

https://github.com/SciProgCentre/plotly.kt/blob/3789fb6fcbf0db566268d2fb882969b74b8282b9/examples/js-demo/src/main/kotlin/space/kscience/plotly/jsdemo/main.kt#L137-L139

SPC-code commented 11 months ago

Released in 0.6.0