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

FillColor of Shape is defined as val #96

Closed Joseph-Hui closed 11 months ago

Joseph-Hui commented 11 months ago

I am trying out to add a Shape in a scatter plot, and the fillcolor property is defined as val. I suppose it could be changed and, probably, it should be var instead?

https://github.com/SciProgCentre/plotly.kt/blob/9f4e7608d91962dd708c72f41f6a9aafe7f3a44d/plotlykt-core/src/commonMain/kotlin/space/kscience/plotly/models/Shape.kt#L143

Thank you very much!

SPC-code commented 11 months ago

@Joseph-Hui Color is an accessor. It is used not as color = "red" but as color("red") (for example here). It is done this way to be able to accept different types of color definition (like strings, numbers and number sets).

The shape example is here. The example is a bit outdated, but it should give you the general idea. shape is an extension function on Plot. Or you can use layout.figure function. It could look like this:

Plotly.plot{
  scatter{
  }
  shape{
   //use it here
  }
  layout{
    figure{
       //or use it here with the same effect for more classic Plotly behavior
    }
  }
}

Since layout block is inside plot, you can use shape in it as well. It is a bit confusing; we could probably fix it in the next major release.

Joseph-Hui commented 11 months ago

Thanks @SPC-code, I learn something new. I try the following to show a blue rectangle. The red line is shown, but not the blue rectangle shape. What could be the problem?

        scatter {
            x(1, 2, 3, 4)
//            y(12, 5, 2, 12)
            y0 = 12.asValue()
            dy = 0
            mode = ScatterMode.`lines+markers`
            type = TraceType.scatter
            // hoverinfo = "none"
            marker {
                color("red")
            }
        }
        shape {
            type = ShapeType.rect
            xref = "x"
            yref = "y"
            fillcolor("blue")
            opacity = 0.3

            y0 = Value.of(9)
            y1 = Value.of(15)
            x0 = Value.of(0)
            x1 = Value.of(6)
            line {
                color("red")
                dash = Dash.dash
            } 

        }
        layout {
            title = "Line and Scatter Plot"
        }
    }
SPC-code commented 11 months ago

@Joseph-Hui it seems like this is a bug. It generates an element instead of array. The workaround is to add additional empty shape block. like shape{}. We will fix it in the next release.

Joseph-Hui commented 11 months ago

Thanks @SPC-code! It works! However, I want to set xref = "paper". In that case, if I add an empty shape{} block, it will show a blank shape at the "negative infinity of x". My workaround of your workaround is to add two identical shape blocks.

SPC-code commented 11 months ago

I've opened a separate issue for the bug: #97. I will close this issue as resolved.