JetBrains / lets-plot-kotlin

Grammar of Graphics for Kotlin
https://lets-plot.org/kotlin/
MIT License
419 stars 36 forks source link

Plot not displaying in SwingNode #152

Closed dosier closed 10 months ago

dosier commented 1 year ago

I am trying to display a plot in a JavaFX application. My idea was to nest a JPanel inside a SwingNode and add this to a JavaFX node. I have adapted the example shown here: https://github.com/JetBrains/lets-plot-kotlin/blob/master/demo/jvm-javafx/src/main/kotlin/minimalDemo/Main.kt

My current solution is:

fun createSwingNode(offers: List<MarketOffer.Floor>): SwingNode {
    val plots = createPlots(offers)
    val selectedPlotKey = plots.keys.first()
    val controller = Controller(
        plots,
        selectedPlotKey,
        false
    )
    val swingNode = SwingNode()
    SwingUtilities.invokeLater {
        swingNode.content = controller.createPlotPanel()
    }
    return swingNode
}

Which I then use as follows:

vbox.replaceChildren(Label("Loading historical data for ${order.name}..."))
scope.launch {
    val offers = order.getOfferHistory().sortedBy { it.time }
    withContext(Dispatchers.JavaFx) {
        vbox.replaceChildren(createSwingNode(offers))

However, nothing shows up. Am I missing something here?

alshan commented 1 year ago

Hi @dosier , I've just added a minimal example of Lets-Plot in JavaFX application. It is a stripped off version of other minimal demos:

https://github.com/alshan/lets-plot-mini-apps/tree/main/jvm-javafx-app

The app works fine. Try to work up from here by adding controls and so on to figure out at what point it stops working.