Skip to content

Commit

Permalink
make Plotly module
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastyBlacksmith committed Feb 2, 2024
1 parent 5c9be95 commit 76161a9
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ JLFzf = "0.1"
PrecompileTools = "1"
PGFPlotsX = "1"
Unzip = "0.1 - 0.2"
PythonPlot = "1"
PythonPlot = "1 - 1.0.2"
UnitfulLatexify = "1"
RecipesPipeline = "1"
LaTeXStrings = "1"
Expand Down
4 changes: 2 additions & 2 deletions ext/PlotsGastonExt/gaston.jl
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ function gaston_parse_axes_attrs(
tmin, tmax = axis_limits(sp, :x, false, false)
rmin, rmax = axis_limits(sp, :y, false, false)
rticks = get_ticks(sp, :y)
gaston_ticks = if (ttype = ticksType(rticks)) === :ticks
gaston_ticks = if (ttype = ticks_type(rticks)) === :ticks
string.(rticks)
elseif ttype === :ticks_and_labels
["'$l' $t" for (t, l) in zip(rticks...)]
Expand Down Expand Up @@ -594,7 +594,7 @@ function gaston_set_ticks!(axesconf, ticks, letter, I, maj_min, add)
push!(axesconf, "unset $(maj_min)$(letter)tics")
return
end
gaston_ticks = if (ttype = ticksType(ticks)) === :ticks
gaston_ticks = if (ttype = ticks_type(ticks)) === :ticks
tics = gaston_fix_ticks_overflow(ticks)
if maj_min == "m"
map(t -> "'' $t 1", tics) # see gnuplot manual 'Mxtics'
Expand Down
2 changes: 1 addition & 1 deletion ext/PlotsInspectDR/inspectdr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function _inspectdr_getaxisticks(ticks, gridlines, xfrm)
TickCustom = InspectDR.TickCustom
_xfrm(coord) = InspectDR.axis2aloc(Float64(coord), xfrm.spec) #Ensure Float64 - in case

ttype = ticksType(ticks)
ttype = ticks_type(ticks)
if ticks === :native
# keep current
elseif ttype === :ticks_and_labels
Expand Down
6 changes: 4 additions & 2 deletions ext/PlotsPlotlyJSExt/PlotsPlotlyJSExt.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module PlotsPlotlyJSExt

using PlotlyJS: PlotlyJS
using Plots: Plots, Plot, plotly_show_js, isplotnull, current
import Plots: _show, _display, closeall
using Plots.Commons
using Plots.Plotly
using Plots.PlotsPlots
import Plots: _show, _display, closeall, current, isplotnull

include("initialization.jl")
include("plotlyjs.jl")
Expand Down
12 changes: 6 additions & 6 deletions ext/PlotsPlotlyJSExt/initialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function __init__()
@info "Initializing $package_str backend in Plots; run `$str()` to activate it."
Plots._backendType[sym] = get_concrete_backend()
Plots._backendSymbol[T] = sym


Check warning on line 18 in ext/PlotsPlotlyJSExt/initialization.jl

View workflow job for this annotation

GitHub Actions / check

[JuliaFormatter] reported by reviewdog 🐶 Raw Output: ext/PlotsPlotlyJSExt/initialization.jl:18:-
push!(Plots._initialized_backends, sym)

Expand All @@ -25,11 +25,11 @@ end
Plots.backend_name(::T) = sym
Plots.backend_package_name(::T) = Plots.backend_package_name(sym)

const _plotlyjs_attrs = Plots._plotly_attrs
const _plotlyjs_seriestypes = Plots._plotly_seriestypes
const _plotlyjs_styles = Plots._plotly_styles
const _plotlyjs_markers = Plots._plotly_markers
const _plotlyjs_scales = Plots._plotly_scales
const _plotlyjs_attrs = Plots.Plotly._plotly_attrs
const _plotlyjs_seriestypes = Plots.Plotly._plotly_seriestypes
const _plotlyjs_styles = Plots.Plotly._plotly_styles
const _plotlyjs_markers = Plots.Plotly._plotly_markers
const _plotlyjs_scales = Plots.Plotly._plotly_scales

# -----------------------------------------------------------------------------
# Overload (dispatch) abstract `is_xxx_supported` and `supported_xxxs` methods
Expand Down
2 changes: 1 addition & 1 deletion ext/PlotsPythonPlotExt/PlotsPythonPlotExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ using Plots:
isscalar,
isvector,
supported_scales,
ticksType,
ticks_type,
legend_angle,
legend_anchor_index,
legend_pos_from_angle,
Expand Down
4 changes: 2 additions & 2 deletions ext/PlotsPythonPlotExt/pythonplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ function _py_set_ticks(sp, ax, ticks, letter)
return
end

tick_values, tick_labels = if (ttype = ticksType(ticks)) === :ticks
tick_values, tick_labels = if (ttype = ticks_type(ticks)) === :ticks
ticks, []
elseif ttype === :ticks_and_labels
ticks
Expand Down Expand Up @@ -1057,7 +1057,7 @@ function _before_layout_calcs(plt::Plot{PythonPlotBackend})
ticks = framestyle === :none ? nothing : get_ticks(sp, axis)

has_major_ticks = ticks !== :none && ticks !== nothing && ticks !== false
has_major_ticks &= if (ttype = ticksType(ticks)) === :ticks
has_major_ticks &= if (ttype = ticks_type(ticks)) === :ticks
length(ticks) > 0
elseif ttype === :ticks_and_labels
tcs, labs = ticks
Expand Down
4 changes: 2 additions & 2 deletions src/Commons/Commons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module Commons

export AVec, AMat, KW, AKW, TicksArgs
export PLOTS_SEED
export Plots, PLOTS_SEED
export _haligns, _valigns, _cbar_width
# Functions
export get_subplot,
Expand Down Expand Up @@ -122,7 +122,7 @@ all_alphas(arg) = true_or_all_true(
arg,
)
all_reals(arg) = true_or_all_true(a -> typeof(a) <: Real, arg)
allFunctions(arg) = true_or_all_true(a -> isa(a, Function), arg)
all_functionss(arg) = true_or_all_true(a -> isa(a, Function), arg)

# ---------------------------------------------------------------
include("attrs.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/Commons/attrs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ function process_fill_attr(plotattributes::AKW, arg)
plotattributes[:fillrange] = arg ? 0 : nothing

# fillrange function
elseif allFunctions(arg)
elseif all_functionss(arg)
plotattributes[:fillrange] = arg

# fillalpha
Expand Down
1 change: 1 addition & 0 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ include("output.jl")
include("shorthands.jl")
include("backends/web.jl")
include("backends/plotly.jl")
using .Plotly
include("init.jl")
include("users.jl")

Expand Down
12 changes: 6 additions & 6 deletions src/Strokes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Strokes

export stroke, brush, Stroke, Brush
using Plots.Colors: Colorant
using Plots.Commons: allAlphas, allReals, allStyles
using Plots.Commons: all_alphas, all_reals, all_styles
struct Stroke
width
color
Expand All @@ -24,7 +24,7 @@ function stroke(args...; alpha = nothing)
T = typeof(arg)

# if arg in _all_styles
if allStyles(arg)
if all_styles(arg)
style = arg
elseif T <: Colorant
color = arg
Expand All @@ -33,9 +33,9 @@ function stroke(args...; alpha = nothing)
color = parse(Colorant, string(arg))
catch
end
elseif allAlphas(arg)
elseif all_alphas(arg)
alpha = arg
elseif allReals(arg)
elseif all_reals(arg)
width = arg
else
@warn "Unused stroke arg: $arg ($(typeof(arg)))"
Expand Down Expand Up @@ -65,9 +65,9 @@ function brush(args...; alpha = nothing)
color = parse(Colorant, string(arg))
catch
end
elseif allAlphas(arg)
elseif all_alphas(arg)
alpha = arg
elseif allReals(arg)
elseif all_reals(arg)
size = arg
else
@warn "Unused brush arg: $arg ($(typeof(arg)))"
Expand Down
45 changes: 33 additions & 12 deletions src/backends/plotly.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
# https://plot.ly/javascript/getting-started
module Plotly

_plotly_framestyle(style::Symbol) =
if style in (:box, :axes, :zerolines, :grid, :none)
style
else
default_style = get((semi = :box, origin = :zerolines), style, :axes)
@warn "Framestyle :$style is not supported by Plotly and PlotlyJS. :$default_style was chosen instead."
default_style
end

# --------------------------------------------------------------------------------------
export PlotlyBackend, plotly_show_js, plotly_series, plotly_layout, embeddable_html

using UUIDs
using Statistics: mean
using Plots: bbox_to_pcts, labelfunc_tex, is_2tuple, ticks_type, recursive_merge
using Plots.Annotations
using Plots.Axes
using Plots.Colorbars
using Plots.Colors: Colorant
using Plots.Commons
using Plots.Fonts
using Plots.Fonts: PlotText
using Plots.PlotMeasures
using Plots.PlotsPlots
using Plots.PlotsSeries
using Plots.PlotUtils: PlotUtils, ColorGradient, rgba_string, rgb_string
using Plots.RecipesPipeline: RecipesPipeline
using Plots.Subplots
using Plots.Surfaces
using Plots.Ticks
import Plots: labelfunc, _show, _display, default_output_format
import Plots: backend_name, backend_package_name

struct PlotlyBackend <: Plots.AbstractBackend end
Plots._backendType[:plotly] = PlotlyBackend
Expand Down Expand Up @@ -169,7 +180,7 @@ const _plotly_markers = [
]
const _plotly_scales = [:identity, :log10]

defaultOutputFormat(plt::Plot{Plots.PlotlyBackend}) = "html"
default_output_format(plt::Plot{PlotlyBackend}) = "html"

for s in (:attr, :seriestype, :marker, :style, :scale)
f1 = Symbol("is_", s, "_supported")
Expand All @@ -192,6 +203,15 @@ function labelfunc(scale::Symbol, backend::PlotlyBackend)
end
end

_plotly_framestyle(style::Symbol) =
if style in (:box, :axes, :zerolines, :grid, :none)
style
else
default_style = get((semi = :box, origin = :zerolines), style, :axes)
@warn "Framestyle :$style is not supported by Plotly and PlotlyJS. :$default_style was chosen instead."
default_style
end

plotly_font(font::Font, color = font.color) = KW(
:family => font.family,
:size => round(Int, 1.4font.pointsize),
Expand Down Expand Up @@ -326,7 +346,7 @@ function plotly_axis(axis, sp, anchor = nothing, domain = nothing)
# ticks
if axis[:ticks] !== :native
ticks = get_ticks(sp, axis)
ttype = ticksType(ticks)
ttype = ticks_type(ticks)
if ttype === :ticks
ax[:tickmode] = "array"
ax[:tickvals] = ticks
Expand Down Expand Up @@ -1301,3 +1321,4 @@ _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{PlotlyBackend})
_show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend}) = write(io, embeddable_html(plt))

_display(plt::Plot{PlotlyBackend}) = standalone_html_window(plt)
end # module
4 changes: 2 additions & 2 deletions src/output.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct PlotsDisplay <: AbstractDisplay end

defaultOutputFormat(plt::Plot) = "png"
default_output_format(plt::Plot) = "png"

function png(plt::Plot, fn)
fn = addExtension(fn, "png")
Expand Down Expand Up @@ -142,7 +142,7 @@ function savefig(plt::Plot, fn) # fn might be an `AbstractString` or an `Abstrac
# get the extension
_, ext = splitext(fn)
ext = chop(ext, head = 1, tail = 0)
isempty(ext) && (ext = defaultOutputFormat(plt))
isempty(ext) && (ext = default_output_format(plt))

# save it
if haskey(_savemap, ext)
Expand Down
8 changes: 4 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,10 @@ isscalar(::Any) = false

is_2tuple(v) = typeof(v) <: Tuple && length(v) == 2

ticksType(ticks::AVec{<:Real}) = :ticks
ticksType(ticks::AVec{<:AbstractString}) = :labels
ticksType(ticks::Tuple{<:Union{AVec,Tuple},<:Union{AVec,Tuple}}) = :ticks_and_labels
ticksType(ticks) = :invalid
ticks_type(ticks::AVec{<:Real}) = :ticks
ticks_type(ticks::AVec{<:AbstractString}) = :labels
ticks_type(ticks::Tuple{<:Union{AVec,Tuple},<:Union{AVec,Tuple}}) = :ticks_and_labels
ticks_type(ticks) = :invalid

limsType(lims::Tuple{<:Real,<:Real}) = :limits
limsType(lims::Symbol) = lims === :auto ? :auto : :invalid
Expand Down
2 changes: 1 addition & 1 deletion test/test_output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ macro test_save(fmt)
end

with(:gr) do
@test Plots.defaultOutputFormat(plot()) == "png"
@test Plots.default_output_format(plot()) == "png"
@test Plots.addExtension("foo", "bar") == "foo.bar"

@test_save :png
Expand Down
10 changes: 5 additions & 5 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@
@test Plots.limsType(:auto) === :auto
@test Plots.limsType(NaN) === :invalid

@test Plots.ticksType([1, 2]) === :ticks
@test Plots.ticksType(["1", "2"]) === :labels
@test Plots.ticksType(([1, 2], ["1", "2"])) === :ticks_and_labels
@test Plots.ticksType(((1, 2), ("1", "2"))) === :ticks_and_labels
@test Plots.ticksType(:undefined) === :invalid
@test Plots.ticks_type([1, 2]) === :ticks
@test Plots.ticks_type(["1", "2"]) === :labels
@test Plots.ticks_type(([1, 2], ["1", "2"])) === :ticks_and_labels
@test Plots.ticks_type(((1, 2), ("1", "2"))) === :ticks_and_labels
@test Plots.ticks_type(:undefined) === :invalid

pl = plot(1:2, 1:2, 1:2, proj_type = :ortho)
@test Plots.isortho(first(pl.subplots))
Expand Down

0 comments on commit 76161a9

Please sign in to comment.