Skip to content

Commit 8d5d41a

Browse files
committed
readme docs
1 parent 6c489df commit 8d5d41a

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,38 @@ save(page, "myplot.html")
6262

6363
- See `?Plot` for details on the `Plot` object.
6464
- See `?PlotlyLight.src!` for details on how javascript gets loaded.
65+
66+
## Defaults
67+
68+
A common workflow is to create multiple plots with a similar style. Rather then setting nearly identical layouts for multiple plots, you can set default values for a variety of items. HTML defaults (`class`/`style`/`parent_class`/`parent_style`) are chosen to make the plot reactive to the browser window size.
69+
70+
```julia
71+
module Defaults
72+
src = Ref(:cdn) # How plotly gets loaded. see ?PlotlyLight.src!
73+
class = Ref("") # class of the <div> the plot is inside of.
74+
style = Ref("height: 100%;") # style of the <div> the plot is inside of.
75+
parent_class = Ref("") # class of the plot's parent <div>.
76+
parent_style = Ref("height: 100vh;") # style of the plot's parent <div>.
77+
config = Ref(Config(displaylogo=false, responsive=true))
78+
layout = Ref(Config())
79+
end
80+
```
81+
82+
Default values can be set e.g.
83+
84+
```julia
85+
PlotlyLight.Defaults.layout[] = Config(title="Default Title")
86+
```
87+
88+
## Themes/Templates
89+
90+
The themes available in [Plotly's python package](https://plotly.com/python/templates/) are also made available in PlotlyLight.jl. They can be set via:
91+
92+
```julia
93+
layout = Config(template = PlotlyLight.template("plotly_dark"))
94+
95+
# or clobber the default `layout.template`
96+
PlotlyLight.template!("plotly_dark)
97+
```
98+
99+
See `PlotlyLight.templates` for a list of theme/template options.

src/PlotlyLight.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,27 @@ src_opts = [:cdn, :local, :standalone, :none]
5252
src!(x::Symbol) = (x in src_opts || error("src must be one of: $src_opts"); Defaults.src[] = x)
5353

5454
#-----------------------------------------------------------------------------# template!
55-
function template!(t = :plotly_white)
55+
"""
56+
template(t)
57+
58+
Load the template `t`, which must be one of:
59+
60+
```
61+
$(join(templates, "\n"))
62+
```
63+
"""
64+
function template(t)
5665
string(t) in templates || error("$t not found. Options are one of: $(join(templates, ", ")).")
57-
tmp = open(io -> JSON3.read(io, Config), joinpath(templates_dir, string(t) * ".json"))
58-
Defaults.layout[].template = tmp
66+
open(io -> JSON3.read(io, Config), joinpath(templates_dir, string(t) * ".json"))
67+
end
68+
69+
"""
70+
template!(t)
71+
72+
Replace the `template` key of the default layout with `t`. See also : `PlotlyLight.template`.
73+
"""
74+
function template!(t)
75+
Defaults.layout[].template = template(t)
5976
end
6077

6178
#-----------------------------------------------------------------------------# Plot
@@ -88,7 +105,10 @@ Base.@kwdef mutable struct Plot
88105
end
89106
function Plot(traces, layout=Defaults.layout[], config=Defaults.config[]; kw...)
90107
data = traces isa Config ? [traces] : traces
91-
Plot(; kw..., data, layout, config)
108+
Plot(; kw..., data,
109+
layout = merge!(layout, Defaults.layout[]),
110+
config = merge!(config, Defaults.config[])
111+
)
92112
end
93113

94114
#-----------------------------------------------------------------------------# Display

0 commit comments

Comments
 (0)