Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Heptazhou committed May 24, 2024
1 parent 5d1bf1e commit 1f747a5
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Plots"
uuid = "90851b43-1319-4523-8ba0-be51ac244080"
authors = ["Heptazhou <zhou at 0h7z dot com>"]
version = "1.40.2"
version = "1.40.3"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
2 changes: 1 addition & 1 deletion src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 4 additions & 45 deletions src/backends/plotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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[]
Expand All @@ -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<script src="$src"></script>\n"""
script(mathjax) * script(plotly_js)
end

function plotly_html_body(plt, style = nothing)
id = uuid4()
"""
<div id="$id"></div>
<!-- beautify ignore:start -->
<script>
/* beautify ignore:start */
$(js_body(plt, id) |> strip)
/* beautify ignore:end */
</script>
<!-- beautify ignore:end -->
"""
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)))
Expand Down
60 changes: 60 additions & 0 deletions src/backends/plotly_html.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (C) 2024 Heptazhou <[email protected]>
#
# 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://www.gnu.org/licenses/>.

# 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<script src="$src"></script>\n"""
script(mathjax) * script(plotly_js)
end

function plotly_html_body(plt::Plot; kw...)
id = uuid4()
"""
<main>
<div id="$id"></div>
</main>
<!-- beautify ignore:start -->
<script>
/* beautify ignore:start */
$(js_body(plt, id; kw...) |> strip)
/* beautify ignore:end */
</script>
<!-- beautify ignore:end -->
"""
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

26 changes: 1 addition & 25 deletions src/backends/web.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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")) =
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="color-scheme" content="dark light" />
<meta name="referrer" content="no-referrer" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>$title</title>
<link rel="dns-prefetch" href="https://cdnjs.cloudflare.com/" />
<style>
html, body { display: flex; height: 100%; overflow: hidden; }
html, body, body > div { flex: auto; margin: 0; padding: 0; }
</style>
$(html_head(plt) |> strip)
</head>
<body>
$(html_body(plt) |> strip)
</body>
</html>
"""

embeddable_html(plt::AbstractPlot) = html_head(plt) * html_body(plt)
include("web_html.jl")

function open_browser_window(filename::AbstractString)
@static if Sys.isapple()
Expand Down
41 changes: 41 additions & 0 deletions src/backends/web_html.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (C) 2024 Heptazhou <[email protected]>
#
# 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://www.gnu.org/licenses/>.

standalone_html(plt::AbstractPlot;
title::AbstractString = get(plt.attr, :window_title, "Plots.jl"), kw...) =
"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="color-scheme" content="dark light" />
<meta name="referrer" content="no-referrer" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>$title</title>
<link rel="dns-prefetch" href="https://cdnjs.cloudflare.com/" />
<style>
html, body, main { display: flex; height: 100%; overflow: hidden; }
html, body, main, main > div { flex: auto; margin: 0; padding: 0; }
</style>
$(html_head(plt; kw...) |> strip)
</head>
<body>
$(html_body(plt; kw...) |> strip)
</body>
</html>
"""

embeddable_html(plt::AbstractPlot; kw...) = html_head(plt; kw...) * html_body(plt; kw...)

0 comments on commit 1f747a5

Please sign in to comment.