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

Histogram not working properly, according to example #70

Closed lobis closed 3 years ago

lobis commented 3 years ago

I believe there is an issue with the histogram examples (or perhaps with histograms itself). I have created a simple js histogram rendering using https://github.com/mipt-npm/plotly.kt/blob/master/examples/src/main/kotlin/histogram/BasicHistogram.kt as a reference. The code is the following: (version 0.3.1)

    document.addEventListener("DOMContentLoaded", {
        val element = document.getElementById("test") as? HTMLElement
            ?: error("Element with id 'test' not found on page")

        console.log("element loaded")

        val rnd = Random
        val values = List(500) { rnd.nextDouble() }

        element.plot {

            histogram {
                x.numbers = values
                name = "Random data"
            }

            layout {
                bargap = 0.1
                title {
                    text = "Basic Histogram"
                    font {
                        size = 20
                        color("black")
                    }
                }
                xaxis {
                    title {
                        text = "Value"
                        font {
                            size = 16
                        }
                    }
                }
                yaxis {
                    title {
                        text = "Count"
                        font {
                            size = 16
                        }
                    }
                }
            }
        }
    })

I believe this should produce a good looking histogram however what is produced is the following:

image

Also there is another issue in the examples, the usage of Random, which according to the documentation (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/) should be called like Random.nextDouble() instead of Random().nextDouble() as it is written in the examples.

altavir commented 3 years ago

Thanks, I've reproduced it on JS (works perfectly fine on the JVM). For some reason, it ignores the trace type parameter when used directly from JS.

The Random is not the issue. Random() function creates a new generator with a random seed whereas Random singleton (companion object) represents the default generator that is created on start. In this context, there is no difference.

lobis commented 3 years ago

Thanks, I've reproduced it on JS (works perfectly fine on the JVM). For some reason, it ignores the trace type parameter when used directly from JS.

The Random is not the issue. Random() function creates a new generator with a random seed whereas Random singleton (companion object) represents the default generator that is created on start. In this context, there is no difference.

For some reason I couldn't get the Random() to work on my plot, I had to change it to Random as you see on the code in order to work. Probably some issue with dependencies on my side. (I imported import kotlin.random.Random to access Random).

altavir commented 3 years ago

Just for educational purposes. The problem is that I used the direct dynamic conversion of Kotlon structures to Json and the enum class is converted into structure instead of value. Like this: image I will fix that shortly.

altavir commented 3 years ago

Fixed and will be available in 0.4.0

altavir commented 3 years ago

No, we need to migrate to a new version because there are some breaking changes and the package version change (kscience->space.kscience). I've published the 0.4.0-dev-2 to the dev repository. So you can use right now via

reposirories{
  maven("https://repo.kotlin.link")
}

depdendencies{
  implementation("space.kscience:plotlykt-core:0.4.0-dev-2")
}

There are breaking changes in package names, so be sure to check examples in dev branch.