diff --git a/docs/make.jl b/docs/make.jl index db9dac92..330e0521 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -2,6 +2,8 @@ using Documenter using PlotlyJS using PlotlyBase +PlotlyJS.set_default_renderer(PlotlyJS.DOCS) + const THIS_DIR = dirname(@__FILE__) # used in examples @@ -9,19 +11,19 @@ using Distributions, HTTP, DataFrames, RDatasets, Colors, CSV, JSON using Random, Dates, LinearAlgebra, DelimitedFiles # to override display_dict below -import Documenter.Utilities: display_dict, limitstringmime -using Base64:stringmime - -function display_dict(p::PlotlyBase.Plot) - out = Dict{MIME,Any}() - # Always generate text/plain - out[MIME"text/plain"()] = limitstringmime(MIME"text/plain"(), p) - svg_m = MIME"image/svg+xml"() - out[svg_m] = stringmime(svg_m, p) - out -end +# import Documenter.Utilities: display_dict, limitstringmime +# using Base64:stringmime + +# function display_dict(p::PlotlyBase.Plot) +# out = Dict{MIME,Any}() +# # Always generate text/plain +# out[MIME"text/plain"()] = limitstringmime(MIME"text/plain"(), p) +# svg_m = MIME"image/svg+xml"() +# out[svg_m] = stringmime(svg_m, p) +# out +# end -display_dict(p::PlotlyJS.SyncPlot) = display_dict(p.plot) +# display_dict(p::PlotlyJS.SyncPlot) = display_dict(p.plot) ## handle examples # Walk through each example in a file and get the markdown from `single_example` @@ -101,7 +103,7 @@ makedocs( sitename="PlotlyJS", format=Documenter.HTML( assets=[ - asset("https://cdn.plot.ly/plotly-1.54.7.js") + "include_plotlyjs.js" ] ), modules=[PlotlyJS, PlotlyBase], diff --git a/docs/src/assets/include_plotlyjs.js b/docs/src/assets/include_plotlyjs.js new file mode 100644 index 00000000..dd69c2aa --- /dev/null +++ b/docs/src/assets/include_plotlyjs.js @@ -0,0 +1,12 @@ +if (typeof require !== "undefined") { + console.log("Trying to load plotly.js via requirejs"); + require.undef("plotly"); + requirejs.config({ + paths: { + plotly: ["https://cdn.plot.ly/plotly-2.3.0.min"], + }, + }); + require(["plotly"], function (Plotly) { + window._Plotly = Plotly; + }); +} diff --git a/docs/src/examples/subplots.md b/docs/src/examples/subplots.md index 998aa902..094a7e4d 100644 --- a/docs/src/examples/subplots.md +++ b/docs/src/examples/subplots.md @@ -19,7 +19,7 @@ subplots1() function subplots2() p1 = linescatter1() p2 = linescatter2() - p = [p1, p2] + p = [p1; p2] p end subplots2() @@ -42,7 +42,7 @@ subplots3() ```@example subplots function subplots_withcomprehension() - hcat([plot(scatter(x = 1:5, y = rand(5))) for i in 1:3]...) + hcat([plot(scatter(x=1:5, y=rand(5))) for i in 1:3]...) end subplots_withcomprehension() ``` @@ -50,10 +50,10 @@ subplots_withcomprehension() ```@example subplots function subplots_withsharedaxes() data = [ - scatter(x=1:3, y=2:4), - scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"), - scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"), - scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4") + scatter(x=1:3, y=2:4), + scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"), + scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"), + scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4") ] layout = Layout( xaxis_domain=[0, 0.45], @@ -61,10 +61,108 @@ function subplots_withsharedaxes() xaxis4=attr(domain=[0.55, 1.0], anchor="y4"), xaxis2_domain=[0.55, 1], yaxis3_domain=[0.55, 1], - yaxis4=attr(domain=[0.55, 1], anchor="x4") + yaxis4=attr(domain=[0.55, 1], anchor="x4") ) plot(data, layout) end subplots_withsharedaxes() ``` +```@example subplots +function with_make_subplots1() + + # The `shared_xaxes` argument to `make_subplots` can be used to link the x + # axes of subplots in the resulting figure. The `vertical_spacing` argument + # is used to control the vertical spacing between rows in the subplot grid. + + # Here is an example that creates a figure with 3 vertically stacked + # subplots with linked x axes. A small vertical spacing value is used to + # reduce the spacing between subplot rows. + + p = make_subplots(rows=3, cols=1, shared_xaxes=true, vertical_spacing=0.02) + add_trace!(p, scatter(x=0:2, y=10:12), row=3, col=1) + add_trace!(p, scatter(x=2:4, y=100:10:120), row=2, col=1) + add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=1, col=1) + relayout!(p, title_text="Stacked Subplots with Shared X-Axes") + p +end +with_make_subplots1() +``` + +```@example subplots +function with_make_subplots2() + # The `shared_yaxes` argument to `make_subplots` can be used to link the y + # axes of subplots in the resulting figure. + + # Here is an example that creates a figure with a 2 x 2 subplot grid, where + # the y axes of each row are linked. + + p = make_subplots(rows=3, cols=2, shared_yaxes=true) + add_trace!(p, scatter(x=0:2, y=10:12), row=1, col=1) + add_trace!(p, scatter(x=20:10:40, y=1:3), row=1, col=2) + add_trace!(p, scatter(x=3:5, y=600:100:800), row=2, col=1) + add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=2, col=2) + relayout!(p, title_text="Multiple Subplots with Shared Y-Axes") + p +end +with_make_subplots2() +``` + +```@example subplots +function with_make_subplots3() + # The `specs` argument to `make_subplots` is used to configure per-subplot + # options. `specs` must be a `Matrix` with dimensions that match those + # provided as the `rows` and `cols` arguments. The elements of `specs` may + # either be `missing`, indicating no subplot should be initialized starting + # with this grid cell, or an instance of `Spec` containing subplot options. + # The `colspan` subplot option specifies the number of grid columns that the + # subplot starting in the given cell should occupy. If unspecified, + # `colspan` defaults to 1. + + # Here is an example that creates a 2 by 2 subplot grid containing 3 + # subplots. The subplot `specs` element for position (2, 1) has a `colspan` + # value of 2, causing it to span the full figure width. The subplot `specs` + # element f or position (2, 2) is `None` because no subplot begins at this + # location in the grid. + p = make_subplots( + rows=2, cols=2, + specs=[Spec() Spec(); Spec(colspan=2) missing], + subplot_titles=["First Subplot" "Second Subplot"; "Third Subplot" missing] + ) + + add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=2) + add_trace!(p, scatter(x=[1, 2, 3], y=[2, 1, 2]), row=2, col=1) + + relayout!(p, showlegend=false, title_text="Specs with Subplot Title") + p +end +with_make_subplots3() +``` + +```@example subplots +function with_make_subplots4() + # Here is an example that uses the `rowspan` and `colspan` subplot options + # to create a custom subplot layout with subplots of mixed sizes. + p = make_subplots( + rows=5, cols=2, + specs=[Spec() Spec(rowspan=2) + Spec() missing + Spec(rowspan=2, colspan=2) missing + missing missing + Spec() Spec()] + ) + + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2) + + relayout!(p, height=600, width=600, title_text="specs examples") + p +end +with_make_subplots4() +``` + diff --git a/docs/src/examples/violin.md b/docs/src/examples/violin.md index 7191e078..c793f2a9 100644 --- a/docs/src/examples/violin.md +++ b/docs/src/examples/violin.md @@ -129,7 +129,7 @@ violin_side_by_side() ``` ```@example violin -function violin_styled() +function violin_style() y1 = vcat(abs.(20 .* rand(100)), rand(UInt16, 300) .* 500 ./ typemax(UInt16)) y2 = [25.261999999999997, 66.5419, 98.2114, 0.09070629 ] box = attr(fillcolor="black", line_color="black", width=0.01) @@ -157,6 +157,6 @@ function violin_styled() ) plot([trace1, trace2], layout) end -violin_styled() +violin_style() ``` diff --git a/examples/subplots.jl b/examples/subplots.jl index 0883695e..3d11594e 100644 --- a/examples/subplots.jl +++ b/examples/subplots.jl @@ -10,7 +10,7 @@ end function subplots2() p1 = linescatter1() p2 = linescatter2() - p = [p1, p2] + p = [p1; p2] p end @@ -29,16 +29,16 @@ end function subplots_withcomprehension() - hcat([plot(scatter(x = 1:5, y = rand(5))) for i in 1:3]...) + hcat([plot(scatter(x=1:5, y=rand(5))) for i in 1:3]...) end function subplots_withsharedaxes() data = [ - scatter(x=1:3, y=2:4), - scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"), - scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"), - scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4") + scatter(x=1:3, y=2:4), + scatter(x=20:10:40, y=fill(5, 3), xaxis="x2", yaxis="y"), + scatter(x=2:4, y=600:100:800, xaxis="x", yaxis="y3"), + scatter(x=4000:1000:6000, y=7000:1000:9000, xaxis="x4", yaxis="y4") ] layout = Layout( xaxis_domain=[0, 0.45], @@ -46,7 +46,93 @@ function subplots_withsharedaxes() xaxis4=attr(domain=[0.55, 1.0], anchor="y4"), xaxis2_domain=[0.55, 1], yaxis3_domain=[0.55, 1], - yaxis4=attr(domain=[0.55, 1], anchor="x4") + yaxis4=attr(domain=[0.55, 1], anchor="x4") ) plot(data, layout) end + +function with_make_subplots1() + + # The `shared_xaxes` argument to `make_subplots` can be used to link the x + # axes of subplots in the resulting figure. The `vertical_spacing` argument + # is used to control the vertical spacing between rows in the subplot grid. + + # Here is an example that creates a figure with 3 vertically stacked + # subplots with linked x axes. A small vertical spacing value is used to + # reduce the spacing between subplot rows. + + p = make_subplots(rows=3, cols=1, shared_xaxes=true, vertical_spacing=0.02) + add_trace!(p, scatter(x=0:2, y=10:12), row=3, col=1) + add_trace!(p, scatter(x=2:4, y=100:10:120), row=2, col=1) + add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=1, col=1) + relayout!(p, title_text="Stacked Subplots with Shared X-Axes") + p +end + +function with_make_subplots2() + # The `shared_yaxes` argument to `make_subplots` can be used to link the y + # axes of subplots in the resulting figure. + + # Here is an example that creates a figure with a 2 x 2 subplot grid, where + # the y axes of each row are linked. + + p = make_subplots(rows=3, cols=2, shared_yaxes=true) + add_trace!(p, scatter(x=0:2, y=10:12), row=1, col=1) + add_trace!(p, scatter(x=20:10:40, y=1:3), row=1, col=2) + add_trace!(p, scatter(x=3:5, y=600:100:800), row=2, col=1) + add_trace!(p, scatter(x=3:5, y=1000:100:1200), row=2, col=2) + relayout!(p, title_text="Multiple Subplots with Shared Y-Axes") + p +end + +function with_make_subplots3() + # The `specs` argument to `make_subplots` is used to configure per-subplot + # options. `specs` must be a `Matrix` with dimensions that match those + # provided as the `rows` and `cols` arguments. The elements of `specs` may + # either be `missing`, indicating no subplot should be initialized starting + # with this grid cell, or an instance of `Spec` containing subplot options. + # The `colspan` subplot option specifies the number of grid columns that the + # subplot starting in the given cell should occupy. If unspecified, + # `colspan` defaults to 1. + + # Here is an example that creates a 2 by 2 subplot grid containing 3 + # subplots. The subplot `specs` element for position (2, 1) has a `colspan` + # value of 2, causing it to span the full figure width. The subplot `specs` + # element f or position (2, 2) is `None` because no subplot begins at this + # location in the grid. + p = make_subplots( + rows=2, cols=2, + specs=[Spec() Spec(); Spec(colspan=2) missing], + subplot_titles=["First Subplot" "Second Subplot"; "Third Subplot" missing] + ) + + add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2]), row=1, col=2) + add_trace!(p, scatter(x=[1, 2, 3], y=[2, 1, 2]), row=2, col=1) + + relayout!(p, showlegend=false, title_text="Specs with Subplot Title") + p +end + +function with_make_subplots4() + # Here is an example that uses the `rowspan` and `colspan` subplot options + # to create a custom subplot layout with subplots of mixed sizes. + p = make_subplots( + rows=5, cols=2, + specs=[Spec() Spec(rowspan=2) + Spec() missing + Spec(rowspan=2, colspan=2) missing + missing missing + Spec() Spec()] + ) + + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,1)"), row=1, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(1,2)"), row=1, col=2) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(2,1)"), row=2, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(3,1)"), row=3, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,1)"), row=5, col=1) + add_trace!(p, scatter(x=[1, 2], y=[1, 2], name="(5,2)"), row=5, col=2) + + relayout!(p, height=600, width=600, title_text="specs examples") + p +end diff --git a/src/display.jl b/src/display.jl index e458cf7c..a88fa120 100644 --- a/src/display.jl +++ b/src/display.jl @@ -16,7 +16,7 @@ end function Base.show(io::IO, mm::MIME"text/html", p::SyncPlot) # if we are rendering docs -- short circuit and display html if get_renderer() == DOCS - return show(io, mm, p.plot, full_html=false, include_plotlyjs="require") + return show(io, mm, p.plot, full_html=false, include_plotlyjs="require-loaded") end show(io, mm, p.scope) end