From 1e9691a7c08a0863f30c13137b7802a1046028cb Mon Sep 17 00:00:00 2001 From: hhaensel <31985040+hhaensel@users.noreply.github.com> Date: Thu, 21 May 2020 12:32:25 +0200 Subject: [PATCH] Add support for mathjax (latex) and extra_plot_kwargs in plotly backend (#2719) * support mathjax header in plotly backend * support extra_plot_kwargs in plotly backend * correct local file parameter to `file:///` * corrections proposed by @BeastyBlacksmith * include extra_kwargs for subplots * don't merge subplot extra_kwargs Co-authored-by: Helmut Haensel Co-authored-by: Simon Christ --- src/backends/plotly.jl | 11 ++++++++--- src/utils.jl | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 6839dc4e..0d39c424 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -336,7 +336,7 @@ function plotly_layout(plt::Plot) plotattributes_out[:hovermode] = "none" end - plotattributes_out + plotattributes_out = recursive_merge(plotattributes_out, plt.attr[:extra_plot_kwargs]) end function plotly_layout_json(plt::Plot) @@ -810,9 +810,14 @@ html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt) const ijulia_initialized = Ref(false) function plotly_html_head(plt::Plot) - local_file = ("file://" * plotly_local_file_path) + local_file = ("file:///" * plotly_local_file_path) plotly = use_local_dependencies[] ? local_file : "https://cdn.plot.ly/plotly-latest.min.js" + + include_mathjax = get(plt[:extra_plot_kwargs], :include_mathjax, "") + mathjax_file = include_mathjax != "cdn" ? ("file://" * include_mathjax) : "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML" + mathjax_head = include_mathjax == "" ? "" : "\n\t\t" + if isijulia() && !ijulia_initialized[] # using requirejs seems to be key to load a js depency in IJulia! # https://requirejs.org/docs/start.html @@ -826,7 +831,7 @@ function plotly_html_head(plt::Plot) """) ijulia_initialized[] = true end - return "" + return "$mathjax_head" end function plotly_html_body(plt, style = nothing) diff --git a/src/utils.jl b/src/utils.jl index d24ed3a7..5bc022d4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -302,6 +302,11 @@ Base.convert(::Type{Vector{T}}, rng::AbstractRange{S}) where {T<:Real,S<:Real} = Base.merge(a::AbstractVector, b::AbstractVector) = sort(unique(vcat(a,b))) +# recursively merge kw-dicts, e.g. for merging extra_kwargs / extra_plot_kwargs in plotly) +recursive_merge(x::AbstractDict...) = merge(recursive_merge, x...) +# if values are not AbstractDicts, take the last definition (as does merge) +recursive_merge(x...) = x[end] + nanpush!(a::AbstractVector, b) = (push!(a, NaN); push!(a, b)) nanappend!(a::AbstractVector, b) = (push!(a, NaN); append!(a, b))