Skip to content

Commit 3dbee79

Browse files
committed
UnicodePlots: Add missing dummy figure to keep correct order in multiplots
1 parent 55d98ff commit 3dbee79

1 file changed

Lines changed: 55 additions & 6 deletions

File tree

ext/GridVisualizeUnicodePlotsExt.jl

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ using ColorSchemes: colorschemes, color
1616
initialize!(p, ::Type{UnicodePlotsType}) = nothing
1717

1818

19-
function reveal(p::GridVisualizer, ::Type{UnicodePlotsType})
19+
function reveal(p::GridVisualizer, TP::Type{UnicodePlotsType})
2020
layout = p.context[:layout]
2121
subplots = @views permutedims(p.subplots)[:]
2222

@@ -26,8 +26,14 @@ function reveal(p::GridVisualizer, ::Type{UnicodePlotsType})
2626
if :Term Symbol.(Base.loaded_modules_array())
2727
@warn "A GridVisualizer with multiple UnicodePlots requires 'Term.jl' to be loaded: add Term.jl to your environment."
2828
else
29-
figures = [subplot[:figure] for subplot in subplots if haskey(subplot, :figure)]
30-
grid_plot = UnicodePlots.gridplot(figures, layout = p.context[:layout], show_placeholder = true)
29+
# set missing figures
30+
for subplot in subplots
31+
if !haskey(subplot, :figure)
32+
missing_figure!(subplot, TP)
33+
end
34+
end
35+
36+
grid_plot = UnicodePlots.gridplot([subplot[:figure] for subplot in subplots], layout = p.context[:layout], show_placeholder = true)
3137
display(grid_plot)
3238
end
3339
end
@@ -55,7 +61,7 @@ function region_legend!(plt, title, y0, colors)
5561
return y0
5662
end
5763

58-
gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{3}}, grid) = @warn "3D gridplots are not implemented for the UnicodePlots backend"
64+
gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{3}}, grid) = missing_figure!(ctx, TP, "gridplot! (3D)", not_implemented = true)
5965

6066
function gridplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
6167
UnicodePlots = ctx[:Plotter]
@@ -518,7 +524,7 @@ function scalarplot!(
518524
return reveal(ctx, TP)
519525
end
520526

521-
scalarplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{3}}, grids, parentgrid, funcs) = @warn "3D scalarplot is not implemented for the UnicodePlots backend"
527+
scalarplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{3}}, grids, parentgrid, funcs) = missing_figure!(ctx, TP, "scalarplot! (3D)", not_implemented = true)
522528

523529

524530
# unicode arrows for vector plot
@@ -654,7 +660,50 @@ function vectorplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid, func
654660
return reveal(ctx, TP)
655661
end
656662

657-
streamplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid, func) = @warn "2D streamplot is not implemented for the UnicodePlots backend"
663+
streamplot!(ctx, TP::Type{UnicodePlotsType}, ::Type{Val{2}}, grid, func) = missing_figure!(ctx, TP, "streamplot! (2D)", not_implemented = true)
664+
665+
# placeholder for missing/not implemented
666+
function missing_figure!(ctx, TP::Type{UnicodePlotsType}, caller_text = ""; not_implemented = false)
667+
668+
UnicodePlots = ctx[:Plotter]
669+
670+
layout = ctx[:layout]
671+
resolution = ctx[:size] ./ (12, 24) ./ (layout[2], layout[1])
672+
673+
# we need an integer resolution
674+
resolution = @. Int(round(resolution))
658675

676+
# create UnicodePlots.Canvas
677+
CanvasType = UnicodePlots.BrailleCanvas # should this be a changeable parameter ?
678+
canvas = CanvasType(
679+
resolution[2], resolution[1] # number of rows and columns (characters)
680+
)
681+
682+
if not_implemented
683+
@warn "$caller_text is not implemented for the UnicodePlots backend"
684+
UnicodePlots.annotate!(
685+
canvas,
686+
0.5, 0.4,
687+
"[not implemented]",
688+
UnicodePlots.ansi_color(:normal),
689+
false
690+
)
691+
end
692+
693+
UnicodePlots.annotate!(
694+
canvas,
695+
0.5,
696+
not_implemented ? 0.6 : 0.5,
697+
caller_text,
698+
UnicodePlots.ansi_color(:normal),
699+
false
700+
)
701+
702+
plt = UnicodePlots.Plot(canvas; title = ctx[:title], border = :dotted)
703+
704+
# plot
705+
ctx[:figure] = plt
706+
return reveal(ctx, TP)
707+
end
659708

660709
end # module

0 commit comments

Comments
 (0)