@@ -4,7 +4,6 @@ using Random
44using JSON3
55using EasyConfig
66using Cobweb
7- using Downloads
87
98export Plot, Config
109
@@ -13,10 +12,31 @@ function __init__()
1312 ! (isfile (plotlyjs)) && @warn " Cannot find plotly.js. PlotlyLight should be built again."
1413end
1514
15+ # -----------------------------------------------------------------------------# defaults
16+ module Defaults
17+ using EasyConfig: Config
18+
19+ src = Ref (:cdn )
20+ class = Ref (" " )
21+ style = Ref (" height: 100%;" )
22+ parent_class = Ref (" " )
23+ parent_style = Ref (" height: 100vh;" )
24+ config = Ref (Config (displaylogo= false , responsive= true ))
25+ layout = Ref (Config ())
26+
27+ function reset! ()
28+ src[] = :cdn
29+ class[] = " "
30+ style[] = " height: 100%;"
31+ parent_class[] = " "
32+ parent_style[] = " height: 100vh;"
33+ config[] = Config (displaylogo= false , responsive= true )
34+ layout[] = Config ()
35+ end
36+ end
37+
1638# -----------------------------------------------------------------------------# src
1739src_opts = [:cdn , :local , :standalone , :none ]
18- plotlysrc = Ref (:cdn )
19-
2040"""
2141 src!(x::Symbol) # `x` must be one of: $src_opts
2242
@@ -25,42 +45,19 @@ plotlysrc = Ref(:cdn)
2545- `:standalone` → Write JS into the HTML file directly (can be shared and viewed offline).
2646- `:none` → For when inserting into a page with Plotly.js already included.
2747"""
28- src! (x:: Symbol ) = (x in src_opts || error (" src must be one of: $src_opts " ); plotlysrc[] = x)
29-
48+ src! (x:: Symbol ) = (x in src_opts || error (" src must be one of: $src_opts " ); Defaults. src[] = x)
3049
3150# -----------------------------------------------------------------------------# Plot
3251"""
33- Plot(data, layout, config; kw... )
52+ Plot(data, layout, config)
3453
3554- A Plotly.js plot with components `data`, `layout`, and `config`.
55+ - `data = Config()`: A `Config` (single trace) or `Vector{Config}` (multiple traces).
56+ - `layout = Config()`.
57+ - `config = Config(displaylogo=false, responsive=true)`.
3658- Each of the three components are converted to JSON via `JSON3.write`.
3759- See the Plotly Javascript docs here: https://plotly.com/javascript/.
3860
39- ### Arguments
40- - `data = Config()`: A `Config` (single trace) or `Vector{Config}` (multiple traces).
41- - `layout = Config()`.
42- - `config = Config(displaylogo=false, responsive=true)`.
43-
44- ### Keyword Arguments
45-
46- Defaults are chosen so that the plot will responsively fill the page. Keywords are best understood at looking at how the `Plot` gets written into HTML.
47-
48- ```html
49- <div class="\$ parent_class" style="\$ parent_style" id="parent-of-\$ id">
50- <div class="\$ class" style="\$ style" id="\$ id"></div>
51- </div>
52-
53- \$ (see ?PlotlyLight.src! which shows how plotly.js script is inserted)
54-
55- <script>
56- data = \$ (JSON3.write(data))
57- layout = \$ (JSON3.write(layout))
58- config = \$ (JSON3.write(config))
59- Plotly.newPlot("\$ id", data, layout, config)
60- \$ js
61- </script>
62- ```
63-
6461### Example
6562
6663 p = Plot(Config(x=1:10, y=randn(10)))
@@ -69,16 +66,12 @@ Defaults are chosen so that the plot will responsively fill the page. Keywords
6966"""
7067Base. @kwdef mutable struct Plot
7168 data:: Vector{Config} = Config[]
72- layout:: Config = Config ()
73- config:: Config = Config (displaylogo= false , responsive= true )
74- id:: String = randstring (10 ) # id of graphDiv
75- class:: String = " " # class of graphDiv
76- style:: String = " height: 100%" # style of graphDiv
77- parent_class:: String = " " # class of graphDiv's parent div
78- parent_style:: String = " height: 100vh" # style of graphDiv's parent div
69+ layout:: Config = Defaults. layout[]
70+ config:: Config = Defaults. config[]
71+ id:: String = randstring (10 ) # id of graphDiv
7972 js:: Cobweb.Javascript = Cobweb. Javascript (" console.log('plot created!')" )
8073end
81- function Plot (traces, layout= Config () , config= Config (displaylogo = false , responsive = true ) ; kw... )
74+ function Plot (traces, layout= Defaults . layout[] , config= Defaults . config[] ; kw... )
8275 data = traces isa Config ? [traces] : traces
8376 Plot (; kw... , data, layout, config)
8477end
8780Base. display (:: Cobweb.CobwebDisplay , o:: Plot ) = display (Cobweb. CobwebDisplay (), Cobweb. Page (o))
8881
8982function Base. show (io:: IO , M:: MIME"text/html" , o:: Plot )
90- src = plotlysrc[]
83+ (; class, style, parent_class, parent_style) = Defaults
84+ parent_style = if get (io, :is_pluto , false )
85+ s = replace (parent_style[], r" height.*;" => " " )
86+ " height: 400px;" * s
87+ else
88+ parent_style[]
89+ end
90+ src = Defaults. src[]
9191 src in [:cdn , :standalone , :none , :local ] || error (" `src` must be :cdn, :standalone, :none, or :local" )
92- println (io, " <div class=\" " , o . parent_class, " \" style=\" " , o . parent_style, " \" id=\" " , " parent-of-" , o. id, " \" >" )
93- println (io, " <div class=\" " , o . class, " \" style=\" " , o . style, " \" id=\" " , o. id, " \" ></div>" )
92+ println (io, " <div class=\" " , parent_class[] , " \" style=\" " , parent_style, " \" id=\" " , " parent-of-" , o. id, " \" >" )
93+ println (io, " <div class=\" " , class[] , " \" style=\" " , style[] , " \" id=\" " , o. id, " \" ></div>" )
9494 println (io, " </div>" )
9595
9696 if src === :cdn
0 commit comments