Is PlotlyJS.add_hline!() on y2-axis possible?

Is it possible under PlotlyJS to add hlines on “y#” (e.g. “y2”)?
Here my sample code:

using PlotlyJS
# ---
function plot_debug_hline()
    scatter1 = PlotlyJS.scatter(;y = [ 1,      2,    3,    4], name= "Y1")
    scatter2 = PlotlyJS.scatter(;y = [+2.5, +1.5,  0.5, -1.5], name= "Y2",  yaxis= "y2")
    plt_traces = [scatter1, scatter2]
    plt_layout = PlotlyJS.Layout(
        title_text       = "Debug add_hline!()",
        xaxis_title_text = "xaxis title",
        yaxis_title_text = "yaxis title",
        yaxis2 = PlotlyJS.attr(
            title      = "yaxis2 title",
            overlaying = "y",
            side       = "right",
            ),            
        )
    # ---
    _fig_obj = PlotlyJS.Plot(plt_traces, plt_layout)
    PlotlyJS.add_hline!(_fig_obj, 2;   line_color = "blue", line_dash = "dash", line_width = 1)
    PlotlyJS.add_hline!(_fig_obj, 1.5; line_color = "red",  line_dash = "dash", line_width = 1, yaxis = "y2")
    # PlotlyJS.add_hline!(_fig_obj, 1,5; line_color = "green", line_dash = "dash", line_width = 1, yref = "y2") # "yref" does not work :-(
    # ---
    return _fig_obj
end
fig_obj = plot_debug_hline()

As a workaround you might use the low level function: PlotlyJS.hline().
The only annoying thing is, I have not figured out how to retrieve the x-axis range:

using PlotlyJS
# ---
function plot_hline_on_axis_y2()
    _x_range = [1, 4]
    scatter1 = PlotlyJS.scatter(;y = [ 1,      2,    3,    4], name= "Y1")
    scatter2 = PlotlyJS.scatter(;y = [+2.5, +1.5,  0.5, -1.5], name= "Y2",  yaxis= "y2")
    plt_traces = [scatter1, scatter2]
    plt_layout = PlotlyJS.Layout(
        title_text       = "Add hline on axis y2!",
        xaxis_title_text = "xaxis title",
        xaxis_range      = _x_range,
        yaxis_title_text = "yaxis title",
        yaxis2 = PlotlyJS.attr(
            title      = "yaxis2 title",
            overlaying = "y",
            side       = "right",
            ),            
        )
    # ---
    _fig_obj = PlotlyJS.Plot(plt_traces, plt_layout)
    PlotlyJS.relayout!(_fig_obj, shapes = PlotlyJS.hline([1.5], 0, _x_range; line_color = "red", line_dash = "dash", yref = "y2",) ,)
    PlotlyJS.add_hline!(_fig_obj, 2;   line_color = "blue", line_dash = "dash", line_width = 1)
    # ---
    return _fig_obj
end
fig_obj = plot_hline_on_axis_y2()
1 Like