Skip to content

Commit

Permalink
height and width adjustment added to ggsave
Browse files Browse the repository at this point in the history
  • Loading branch information
rdboyes committed May 26, 2024
1 parent e64346c commit 837ff92
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
23 changes: 23 additions & 0 deletions src/draw.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,33 @@ function draw_ggplot(plot::GGPlot)
end
end

function draw_ggplot(plot::GGPlot, size::Tuple)
axis = Makie.SpecApi.Axis(plot)
legend = build_legend(plot)

if isnothing(legend)
Makie.plot(
Makie.SpecApi.GridLayout(
axis
), figure=(;size=size)
)
else
Makie.plot(
Makie.SpecApi.GridLayout(
[axis legend]
), figure=(;size=size)
)
end
end

function draw_ggplot(plot_grid::GGPlotGrid)
Makie.plot(plot_grid.grid)
end

function draw_ggplot(plot_grid::GGPlotGrid, size::Tuple)
Makie.plot(plot_grid.grid, figure=(;size=size))
end

try_convert(::Type{Any}, v, ::Any, ::Any) = v

function try_convert(T::Type, v::S, arg, fname) where {S}
Expand Down
39 changes: 22 additions & 17 deletions src/ggsave.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
function ggsave(path::String, plot::GGPlot; scale = 2)
save(path,
with_theme(plot.theme) do
draw_ggplot(plot)
end;
px_per_unit = scale)
end
function ggsave(path::String, plot::Union{GGPlot, GGPlotGrid};
scale = 2, height = nothing, width = nothing)

function ggsave(plot::GGPlot, path::String; scale = 2)
save(path,
with_theme(plot.theme) do
draw_ggplot(plot)
end;
px_per_unit = scale)
end
if xor(isnothing(height), isnothing(width))
throw("Specify either: both height and width OR neither height nor width.")
end

function ggsave(plot::GGPlotGrid, path::String; scale = 2)
save(path, draw_ggplot(plot), px_per_unit = scale)
# does not support different themes per plot in a grid
theme = plot isa GGPlotGrid ? plot.plots[1].theme : plot.theme

if !isnothing(height)
save(path,
with_theme(theme) do
draw_ggplot(plot, (width, height))
end; px_per_unit = scale)
else
save(path,
with_theme(theme) do
draw_ggplot(plot)
end; px_per_unit = scale)
end
end

ggsave(path::String, plot::GGPlotGrid; scale = 2) = ggsave(plot, path; scale=scale)
function ggsave(plot::GGPlot, path::String; scale = 2, height = nothing, width = nothing)
ggsave(path, plot; scale=scale, height=height, width=width)
end

0 comments on commit 837ff92

Please sign in to comment.