@@ -5,15 +5,47 @@ Extension module for UnicodePlots.jl
55"""
66module GridVisualizeUnicodePlotsExt
77
8- import GridVisualize: initialize!, gridplot!, scalarplot!, bregion_cmap, region_cmap
9- using GridVisualize: UnicodePlotsType
8+ import GridVisualize: initialize!, gridplot!, scalarplot!, bregion_cmap, region_cmap, reveal
9+ using GridVisualize: UnicodePlotsType, GridVisualizer, SubVisualizer
1010using UnicodePlots: UnicodePlots
1111using ExtendableGrids: Coordinates, simplexgrid, ON_CELLS, ON_FACES, ON_EDGES, CellNodes, FaceNodes, BFaceNodes, CellGeometries, CellRegions, BFaceRegions, num_cells, num_nodes, local_celledgenodes, num_bfaceregions, num_cellregions, num_targets, interpolate!
1212using Colors: Colors, RGB, RGBA
1313
1414initialize! (p, :: Type{UnicodePlotsType} ) = nothing
1515
16- function gridplot! (ctx, :: Type{UnicodePlotsType} , :: Type{Val{2}} , grid)
16+
17+ function reveal (p:: GridVisualizer , :: Type{UnicodePlotsType} )
18+ return p. context[:figure ]
19+ end
20+
21+
22+ function reveal (ctx:: SubVisualizer , TP:: Type{UnicodePlotsType} )
23+ if ctx[:show ] || ctx[:reveal ]
24+ return ctx[:figure ]
25+ end
26+ return nothing
27+ end
28+
29+
30+ function region_legend! (canvas, title, x, y, colors)
31+ # legend by annotate!
32+ for (i, char) in enumerate (title)
33+ UnicodePlots. char_point! (canvas, x + i - 1 , y, char, UInt32 (0 ), false )
34+ end
35+ startx = x + length (title)
36+ for r in 1 : length (colors)
37+ red, green, blue = UInt32 .(colors[r])
38+ uint_color = (red << 16 ) | (green << 8 ) | blue
39+ reg_string = " $r "
40+ for char in reg_string
41+ startx += 1
42+ UnicodePlots. char_point! (canvas, startx, y, char, uint_color, false )
43+ end
44+ end
45+ return
46+ end
47+
48+ function gridplot! (ctx, TP:: Type{UnicodePlotsType} , :: Type{Val{2}} , grid)
1749 UnicodePlots = ctx[:Plotter ]
1850
1951 # find bounding box
@@ -157,30 +189,12 @@ function gridplot!(ctx, ::Type{UnicodePlotsType}, ::Type{Val{2}}, grid)
157189 UnicodePlots. annotate! (canvas, ex[1 ] - 1.5 * padding, ey[2 ], " $(ey[2 ]) " , UInt32 (0 ), false ; halign = :left )
158190
159191 # plot
160- plot = UnicodePlots. Plot (canvas; title = ctx[:title ])
161- return plot
162- end
163-
164- function region_legend! (canvas, title, x, y, colors)
165- # legend by annotate!
166- for (i, char) in enumerate (title)
167- UnicodePlots. char_point! (canvas, x + i - 1 , y, char, UInt32 (0 ), false )
168- end
169- startx = x + length (title)
170- for r in 1 : length (colors)
171- red, green, blue = UInt32 .(colors[r])
172- uint_color = (red << 16 ) | (green << 8 ) | blue
173- reg_string = " $r "
174- for char in reg_string
175- startx += 1
176- UnicodePlots. char_point! (canvas, startx, y, char, uint_color, false )
177- end
178- end
179- return
192+ ctx[:figure ] = UnicodePlots. Plot (canvas; title = ctx[:title ])
193+ return reveal (ctx, TP)
180194end
181195
182196
183- function gridplot! (ctx, :: Type{UnicodePlotsType} , :: Type{Val{1}} , grid)
197+ function gridplot! (ctx, TP :: Type{UnicodePlotsType} , :: Type{Val{1}} , grid)
184198 UnicodePlots = ctx[:Plotter ]
185199
186200 # find bounding box
@@ -273,14 +287,15 @@ function gridplot!(ctx, ::Type{UnicodePlotsType}, ::Type{Val{1}}, grid)
273287 UnicodePlots. annotate! (canvas, ex[2 ], 0.1 , " $(ex[2 ]) " , UInt32 (0 ), false )
274288
275289 # plot
276- plot = UnicodePlots. Plot (canvas; title = ctx[:title ])
277- return plot
290+ ctx[:figure ] = UnicodePlots. Plot (canvas; title = ctx[:title ])
291+
292+ return reveal (ctx, TP)
278293end
279294
280295
281296function scalarplot! (
282297 ctx,
283- :: Type{UnicodePlotsType} ,
298+ TP :: Type{UnicodePlotsType} ,
284299 :: Type{Val{1}} ,
285300 grids,
286301 parentgrid,
@@ -307,14 +322,15 @@ function scalarplot!(
307322 UnicodePlots. lineplot! (plt, coord[1 , :], func; name = ctx[:label ])
308323 end
309324 end
325+ ctx[:figure ] = plt
310326
311- return plt
327+ return reveal (ctx, TP)
312328end
313329
314330
315331function scalarplot! (
316332 ctx,
317- :: Type{UnicodePlotsType} ,
333+ TP :: Type{UnicodePlotsType} ,
318334 :: Type{Val{2}} ,
319335 grids,
320336 parentgrid,
@@ -343,8 +359,7 @@ function scalarplot!(
343359 I = zeros (Float64, num_nodes (xgrid_plot))
344360 interpolate! (I, xgrid_plot, func, grids[1 ]; eps = 1.0e-14 , not_in_domain_value = NaN , trybrute = true )
345361
346- plt = ctx[:clear ] ? nothing : ctx[:figure ]
347- plt = UnicodePlots. heatmap (
362+ ctx[:figure ] = UnicodePlots. heatmap (
348363 reshape (I, (resolution[1 ], resolution[2 ]))' ,
349364 xlabel = " x" ,
350365 ylabel = " y" ,
@@ -356,7 +371,7 @@ function scalarplot!(
356371 colormap = colormap,
357372 )
358373
359- return plt
374+ return reveal (ctx, TP)
360375end
361376
362377end # module
0 commit comments