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

Support for multiple Y axes #92

Open luispollo opened 1 year ago

luispollo commented 1 year ago

Other variants of Plotly (JS, Python) support plotting traces on multiple Y axes. It'd be great if that feature was ported to the Kotlin wrapper.

SPC-code commented 1 year ago

I did not know about that feature. I will add it to new release. For now, you can use custom fields like this:

import space.kscience.dataforge.meta.set
import space.kscience.dataforge.meta.spec
import space.kscience.dataforge.names.asName
import space.kscience.plotly.Plotly
import space.kscience.plotly.layout
import space.kscience.plotly.makeFile
import space.kscience.plotly.models.Axis
import space.kscience.plotly.models.Layout
import space.kscience.plotly.scatter

fun Layout.yaxis(index: Int, block: Axis.()->Unit){
    require(index>=2){"Secondary axis index must be 2 or more"}
    val axisSpec by spec(Axis,"yaxis$index".asName())
    axisSpec.apply(block)
}

fun main() {
    Plotly.plot {

        scatter{
            x(1, 2, 3)
            y(40, 50, 60)
            name = "yaxis data"
        }

        scatter{
            x(2, 3, 4)
            y(4, 5, 6)
            name = "yaxis2 data"
            set("yaxis","y2")
        }

        layout {
            title = "Double Y Axis Example"
            yaxis {
                title = "yaxis title"
            }
            yaxis(2) {
                title {
                    text = "yaxis2 title"
                    font {
                        color("rgb(148, 103, 189)")
                    }
                }
                tickfont {
                    color("rgb(148, 103, 189)")
                }
                set("overlaying", "y")
                set("side","right")
            }
        }
    }.makeFile()
}