From 837ff9209d932dfc35661e18a4de5c196b2364e8 Mon Sep 17 00:00:00 2001 From: Randy Boyes Date: Sun, 26 May 2024 14:53:10 -0400 Subject: [PATCH] height and width adjustment added to ggsave --- src/draw.jl | 23 +++++++++++++++++++++++ src/ggsave.jl | 39 ++++++++++++++++++++++----------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/draw.jl b/src/draw.jl index c02524f..d75fb26 100644 --- a/src/draw.jl +++ b/src/draw.jl @@ -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} diff --git a/src/ggsave.jl b/src/ggsave.jl index 32f5796..f34d015 100644 --- a/src/ggsave.jl +++ b/src/ggsave.jl @@ -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 \ No newline at end of file