Change each trace meeting condition in PlotyJS plot

The title states it all. Here an example with the Julia code without the requested part.

using DataFrames
using PlotlyJS

df = DataFrame(
    x = 1:5,
    a = rand(5),
    b_calc = rand(5),
    c = rand(5),
    d_calc = rand(5)
)

fig = plot()
for tag in [:a, :b_calc, :c, :d_calc]
    add_trace!(fig, scatter(x=df.x, y=df[!, tag], name=tag))
end

In Python a possible example of the requested part would be

# corresponding Python code of what I want to achieve in Julia
# adapt line style when the trace name ends with "_calc"
fig.for_each_trace(
    lambda trace:
        trace.update(
            line_dash="dash",
        ) if trace.name.endswith("_calc") else (),
)

Unfortunately I couldn’t find an equivalent of for_each_trace in the corresponding Julia documentation parts. I had a look at

Can this be achieved without such an equivalent command which is the reason it doesn’t exist?

for (k,it) in enumerate(fig.plot.data)
    if endswith(String(it[:name]), "_calc")
        restyle!(fig, k, line_dash="dash")
    end
end
1 Like

@stephancb, awesome. I would have never come to fig.plot.data. Even can’t remember to have seen this anywhere. Many thanks !!

I had figured it out with

julia> fieldnames(typeof(fig))
(:plot, :scope, :window)

julia> fieldnames(typeof(fig.plot))
(:data, :layout, :frames, :divid, :config)

Just in case, we can also drill down by typing in the REPL: fig. followed by <TAB> <TAB>, and so on.

Or even better, use the new Eyeball package with the command: eye(fig)

I am new to Julia so I am very thankful for your hints, guys :+1: