diff --git a/Project.toml b/Project.toml index a8832a11..5c6dd75a 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Plots" uuid = "90851b43-1319-4523-8ba0-be51ac244080" authors = ["Heptazhou "] -version = "1.40.2" +version = "1.40.3" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/src/Plots.jl b/src/Plots.jl index 49485323..be7e0a74 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -7,11 +7,11 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m @eval Base.Experimental.@max_methods 1 end -using LaTeXStrings: LaTeXStrings using OrderedCollections: OrderedDict using Pkg, Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random using PrecompileTools, Reexport, RelocatableFolders using Base.Meta +@reexport using LaTeXStrings @reexport using RecipesBase @reexport using PlotThemes @reexport using PlotUtils diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 7473fdcc..18a9a9bd 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -191,7 +191,7 @@ function plotly_polaraxis(sp::Subplot, axis::Axis) ax end -function plotly_layout(plt::Plot, responsive::Bool = true) +function plotly_layout(plt::Plot; responsive::Bool = true, _...) plotattributes_out = KW() w, h = plt[:size] @@ -339,11 +339,6 @@ function plotly_layout(plt::Plot, responsive::Bool = true) plotattributes_out = recursive_merge(plotattributes_out, plt.attr[:extra_plot_kwargs]) end -# https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js -function plotly_config(plt::Plot, responsive::Bool = true) - KW([:responsive => responsive, :showTips => false]) -end - function plotly_add_legend!(plotattributes_out::KW, sp::Subplot) plotattributes_out[:showlegend] = sp[:legend_position] !== :none legend_position = plotly_legend_pos(sp[:legend_position]) @@ -1058,8 +1053,8 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt), 4) # ---------------------------------------------------------------- -html_head(plt::Plot{PlotlyBackend}) = plotly_html_head(plt) -html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) +html_head(plt::Plot{PlotlyBackend}; kw...) = plotly_html_head(plt; kw...) +html_body(plt::Plot{PlotlyBackend}; kw...) = plotly_html_body(plt; kw...) plotly_url() = if _use_local_dependencies[] @@ -1068,43 +1063,7 @@ plotly_url() = "https://cdn.plot.ly/$_plotly_min_js_filename" end -function plotly_html_head(plt::Plot) - # https://cdnjs.com/libraries/mathjax - # https://cdnjs.com/libraries/plotly.js - mathjax = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg-full.min.js" - plotly_js = "https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.32.0/plotly.min.js" - - script(src::String) = """\t\n""" - script(mathjax) * script(plotly_js) -end - -function plotly_html_body(plt, style = nothing) - id = uuid4() - """ -
- - - - """ -end - -function js_body(plt::Plot, id) - f = sort! ∘ OrderedDict - s = JSON.json(["$id", - plotly_series(plt) .|> f, - plotly_layout(plt) |> f, - plotly_config(plt) |> f, - ], 4) - """ - Plotly.newPlot( - $(strip(∈("[\t\n]"), s)) - ) - """ -end +include("plotly_html.jl") plotly_show_js(io::IO, plot::Plot) = JSON.print(io, Dict(:data => plotly_series(plot), :layout => plotly_layout(plot))) diff --git a/src/backends/plotly_html.jl b/src/backends/plotly_html.jl new file mode 100644 index 00000000..7e5da619 --- /dev/null +++ b/src/backends/plotly_html.jl @@ -0,0 +1,60 @@ +# Copyright (C) 2024 Heptazhou +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +# https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js +function plotly_config(plt::Plot; responsive::Bool = true, _...) + KW([:responsive => responsive, :showTips => false]) +end + +function plotly_html_head(plt::Plot; _...) + # https://cdnjs.com/libraries/mathjax + # https://cdnjs.com/libraries/plotly.js + mathjax = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-svg-full.min.js" + plotly_js = "https://cdnjs.cloudflare.com/ajax/libs/plotly.js/2.32.0/plotly.min.js" + + script(src::String) = """\t\n""" + script(mathjax) * script(plotly_js) +end + +function plotly_html_body(plt::Plot; kw...) + id = uuid4() + """ +
+
+
+ + + + """ +end + +function js_body(plt::Plot, id; kw...) + f = sort! ∘ OrderedDict + s = JSON.json(["$id", + plotly_series(plt) .|> f, + plotly_layout(plt; kw...) |> f, + plotly_config(plt; kw...) |> f, + ], 4) + """ + Plotly.newPlot( + $(strip(∈("[\t\n]"), s)) + ) + """ +end + diff --git a/src/backends/web.jl b/src/backends/web.jl index 710c46ec..753b350f 100644 --- a/src/backends/web.jl +++ b/src/backends/web.jl @@ -3,31 +3,7 @@ # CREDIT: parts of this implementation were inspired by @joshday's PlotlyLocal.jl -standalone_html(plt::AbstractPlot; - title::AbstractString = get(plt.attr, :window_title, "Plots.jl")) = - """ - - - - - - - - $title - - - $(html_head(plt) |> strip) - - - $(html_body(plt) |> strip) - - - """ - -embeddable_html(plt::AbstractPlot) = html_head(plt) * html_body(plt) +include("web_html.jl") function open_browser_window(filename::AbstractString) @static if Sys.isapple() diff --git a/src/backends/web_html.jl b/src/backends/web_html.jl new file mode 100644 index 00000000..e56be7b4 --- /dev/null +++ b/src/backends/web_html.jl @@ -0,0 +1,41 @@ +# Copyright (C) 2024 Heptazhou +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +standalone_html(plt::AbstractPlot; + title::AbstractString = get(plt.attr, :window_title, "Plots.jl"), kw...) = + """ + + + + + + + + $title + + + $(html_head(plt; kw...) |> strip) + + + $(html_body(plt; kw...) |> strip) + + + """ + +embeddable_html(plt::AbstractPlot; kw...) = html_head(plt; kw...) * html_body(plt; kw...) +